dart 自定义下拉按钮与图标抖动

mftmpeh8  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(96)

我想把按钮做成这样:
https://i.stack.imgur.com/UWAhX.png
如何在图标内添加图标。特别是在图标按钮的左侧。
这是我的代码和视图:
https://i.stack.imgur.com/K6HSx.jpg
https://i.stack.imgur.com/mvGHQ.jpg

String? dropdownValue;

  List<String> listPilihanstatus = [
    'Mitsubishi',
    'Yamaha',
    'Honda',
    'Nissan',
    'Hyundai',
  ];

DropdownButtonFormField<String>(
  hint: const Text('Select your vehicle'),
  value: dropdownValue,
  decoration: const InputDecoration(
    border: OutlineInputBorder(
      borderRadius: BorderRadius.all(Radius.circular(10.0)),
    ),
  ),
  onChanged: (String? newValue) {
    setState(() {
      dropdownValue = newValue!;
    });
  },
  items: listPilihanstatus.map<DropdownMenuItem<String>>((String value) {
    return DropdownMenuItem<String>(
      value: value,
      child: Text(value),
    );
  }).toList(),
),

字符串

pftdvrlh

pftdvrlh1#

将下拉菜单项中的子项从text替换为ListTile,如果你需要每个项都有默认图标,你必须将String列表更改为List<Map<String,dynamic>>,如下所示:

List<Map<String, dynamic>> listPilihanstatus = [
  {'name': 'Mitsubishi', 'icon': Icons.safety_check},
  {'name': 'Yamaha', 'icon': Icons.access_alarm_outlined},
  {'name': 'Honda', 'icon': Icons.account_balance_sharp},
  {'name': 'Nissan', 'icon': Icons.sailing},
  {'name': 'Hyundai', 'icon': Icons.macro_off},
];

字符串
代码如下:

DropdownButtonFormField<String>(
                hint: const Text('Select your vehicle'),
                value: dropdownValue,
                decoration: const InputDecoration(
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.all(Radius.circular(10.0)),
                  ),
                ),
                onChanged: (String? newValue) {
                  setState(() {
                    dropdownValue = newValue!;
                  });
                },
                items: listPilihanstatus
                    .map<DropdownMenuItem<String>>((e) => DropdownMenuItem(
                          child:ListTile(
                            title: Text(e['name']),
                            trailing: Icon(e['icon']),
                          ),
                          value: e['name'],
                        ))
                    .toList()),

相关问题