我已经创建了下面的DropDown小部件,以自定义我使用的时间

import 'package:flutter/material.dart';
import 'package:universalteam/Components/AppColors.dart';

Widget CustomDropdown({
  required List<String> items,
  required String hint,
  double borderRadius = 16,
  double width = double.infinity,
  double height = 60,
  double left = 0,
  double right = 0,
  double top = 0,
  double bottom = 0,
  double fontSize = 16,
  Color color = AppColors.black,
  Color containerBackgroundColor = AppColors.RegisterPageTextFieldColor,
  Color textFieldBackgroundColor = AppColors.RegisterPageTextFieldColor,
  double contentPaddingLeft = 12,
  double contentPaddingRight = 0,
  TextAlign textalign = TextAlign.left,
  bool isRequired = false,
  void Function(String?)? onChanged,
}) {
  String? _selectedItem;

  return Visibility(
    visible: true,
    child: Container(
      width: width,
      height: height,
      padding: EdgeInsets.only(
        left: left,
        right: right,
        top: top,
        bottom: bottom,
      ),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
        color: containerBackgroundColor,
      ),
      child: Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
          color: textFieldBackgroundColor,
        ),
        child: DropdownButtonFormField<String>(
          value: _selectedItem,
          style: TextStyle(
            fontSize: fontSize,
            fontFamily: "Poppins",
            fontWeight: FontWeight.w400,
            color: AppColors.darkGray,
          ),
          decoration: InputDecoration(
            label: RichText(
              softWrap: true,
              text: TextSpan(
                text: hint,
                style: TextStyle(
                  color: AppColors.darkGray,
                ),
                children: [
                  isRequired == true
                      ? TextSpan(
                          text: ' * ',
                          style: TextStyle(
                            color: AppColors.darkMagenta,
                            fontWeight: FontWeight.bold,
                          ),
                        )
                      : TextSpan(text: '')
                ],
              ),
            ),
            floatingLabelBehavior: FloatingLabelBehavior.never,
            border: InputBorder.none,
            contentPadding: EdgeInsets.only(
              left: contentPaddingLeft,
              right: contentPaddingRight,
            ),
          ),
          onChanged: (String? newValue) {
            _selectedItem = newValue;
            onChanged?.call(newValue);
          },
          items: items.map<DropdownMenuItem<String>>((String value) {
            return DropdownMenuItem<String>(
              value: value,
              child: Text(
                value,
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Color(0xFF5F5F60),
                  fontSize: 13,
                  fontFamily: 'Poppins',
                  fontWeight: FontWeight.w400,
                  height: 0.12,
                ),
              ),
            );
          }).toList(),
          icon: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Icon(
                Icons.keyboard_arrow_down,
                size: 25,
                color: AppColors.darkMagenta,
              ),
              SizedBox(width: 17)
            ],
          ),
        ),
      ),
    ),
  );
}

然而,当我调用它时,图标并不是垂直居中的.我一直在改变周围的一切,想找到一种方法,但却找不到.还有,是否有一种方法来自定义如何显示的浏览器选项?

它看起来是这样的:

Dropdown button

推荐答案

InputDecorationlabel的存在导致下拉图标被稍微推到底部.要在保持图标居中的同时使用label,请将图标设置为InputDecorationsuffixIcon.

DropdownButtonFormField<String>(
  decoration: InputDecoration(
    suffixIcon: Icon( // (1) Put the Icon widget in this `suffixIcon` parameter
      Icons.keyboard_arrow_down,
      size: 25,
      color: AppColors.darkMagenta,
    ),
    // ...
  ),
  icon: SizedBox(), // (2) Set the dropdown icon to a blank widget
  // ...
)

Flutter相关问答推荐

如何增加容器高度?

Flutter 翼doctor 显示的错误,我似乎不能修复.问题:系统32、Android工具链、Windows 10 SDK

出现异常.RangeError(RangeError(index):无效值:有效值范围为空:0).分度误差

如何在Flutter 中共享选定文本

上传的图像不能在FireBase中预览.S档案类型为空白,摆动

Flutter为什么不;没有ListView.iterable()构造函数?

构建方法中的性能声明小部件

Flutter:如何在 Future 函数上添加网络判断和 showDialog

Dart:未捕获错误:RangeError(索引):索引超出范围:索引应小于 7:7

Riverpod 注册多少个 providers

Flutter中如何实现带有变化图标的浮动操作按钮?

Flutter - firebase_auth_web 5.4.0 依赖于 intl ^0.17.0 但我必须使用更高版本 (intl ^0.18.0)

我的第一个 flutter 项目出错,启动时为空值?

在提供程序包更改通知程序中构建倒计时时钟

改变步进器的线条 colored颜色 Flutter

如何扫描来自给定链接或按钮的重定向中的所有 URL?

Flutter:制作自定义小部件的最佳做法是什么?

Flutter :RangeError(索引):无效值:不在包含范围内0..3:4使用IndexedListView.builder

参数类型MaterialPageRoute不能分配给参数类型String

如何将 pushNamed 与 Dismissible Widget 一起使用并查看背景透明第一页