flutter 如何自定义DropDownButton

sbdsn5lh  于 2023-02-13  发布在  Flutter
关注(0)|答案(1)|浏览(161)

如何自定义DropDownButton的外观?

我想做几件事,使下拉看起来像一个文本输入:
1.删除黄色框内的底线
1.缩进“使用电子邮件”,使其与“电子邮件地址”左对齐
1.使下拉框与文本框大小相同(默认大小)
1.将向下箭头放在下拉菜单右侧附近
1.添加边框

Container(
            width: double.infinity,
            decoration: BoxDecoration(
              color: Colors.yellow[600],
              borderRadius: BorderRadius.circular(4),
            ),
          child: DropdownButton(
            value: DropDownValue,
            icon: const Icon(Icons.keyboard_arrow_down),
            items: DropDownItems.map((String DropDownItems) {
              return DropdownMenuItem(
                value: DropDownItems,
                child: Text(DropDownItems),
              );
            }).toList(),
            onChanged: (String? newValue) {
              setState(() {
                DropDownValue = newValue!;
                if (DropDownValue == 'Use Email') {
                  RegisterType = 'Email';
                } else {
                  RegisterType = 'Mobile';
                }
              });
            },
          ),
        ),
        SizedBox(height: 10),
        TextField(
          decoration: InputDecoration(
              border: OutlineInputBorder(), labelText: RegisterType),
          controller: _EmailAddressController,
        ),
ddrv8njm

ddrv8njm1#

请尝试以下代码:

Container(
      width: double.infinity,
      decoration: BoxDecoration(
        color: Colors.yellow[600],
        border: Border.all(color: Colors.grey),
        borderRadius: BorderRadius.circular(4),
      ),
      child: DropdownButtonHideUnderline(
        child: ButtonTheme(
          alignedDropdown: true,
          child: DropdownButton(
            value: DropDownValue,
            icon: const Icon(Icons.keyboard_arrow_down),
            items: DropDownItems.map((String DropDownItems) {
              return DropdownMenuItem(
                value: DropDownItems,
                child: Text(DropDownItems),
              );
            }).toList(),
            onChanged: (String? newValue) {
              setState(() {
                DropDownValue = newValue!;
                if (DropDownValue == 'Use Email') {
                  RegisterType = 'Email';
                } else {
                  RegisterType = 'Mobile';
                }
              });
            },
          ),
        ),
      ),
    ),
    SizedBox(height: 10),
    TextField(
      decoration: InputDecoration(
          border: OutlineInputBorder(), labelText: RegisterType),
      controller: _EmailAddressController,
    ),

相关问题