0

如何在不使用下拉按钮的情况下点击按钮时制作下拉列表#flutter在此处输入图像描述

new DropdownButton(
  value: _selectedLocation,
  onChanged: (String newValue) {
    setState(() {
      _selectedLocation = newValue;
     });
},
items: _locations.map((String location) {
  return new DropdownMenuItem<String>(
     child: new Text(location),
  );
}).toList(),
4

1 回答 1

0

试试下面的代码希望它对你有帮助。你必须dropdown_below这里使用,请参考我的回答这里也一样

创建您的列表:

 List mobileList = [
    {'no': 1, 'mobile': 'Apple'},
    {'no': 2, 'mobile': 'Google'},
    {'no': 3, 'mobile': 'Samsung'},
    {'no': 4, 'mobile': 'Sony'},
    {'no': 5, 'mobile': 'LG'},
  ];

一个变量并列出我们的价值

List<DropdownMenuItem<Object?>> _dropdownTestItems = [];
  var selectedmobile;

创建 initState() 和 dispose() 方法:

  @override
  void initState() {
    _dropdownTestItems = buildDropdownTestItems(mobileList);
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

在下拉列表中添加您选择的性别值

  List<DropdownMenuItem<Object?>> buildDropdownTestItems(List mobileList) {
    List<DropdownMenuItem<Object?>> items = [];
    for (var i in mobileList) {
      items.add(
        DropdownMenuItem(
          value: i,
          child: Text(
            i['mobile'],
            style: TextStyle(color: Colors.black),
          ),
        ),
      );
    }
    return items;
  }

您的小部件:

  Padding(
        padding: const EdgeInsets.all(8.0),
        child: DropdownBelow(
          itemWidth: 150,
          itemTextstyle: TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w400,
              color: Colors.black),
          boxTextstyle: TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w400,
              color: Colors.white54),
          boxPadding: EdgeInsets.fromLTRB(13, 12, 13, 12),
          boxWidth: 150,
          boxHeight: 45,
          boxDecoration: BoxDecoration(
            color: Colors.transparent,
            border: Border.all(
              width: 1,
              color: Colors.black,
            ),
          ),
          icon: Icon(
            Icons.arrow_downward,
            color: Colors.black,
          ),
          hint: Text(
            'Select Mobile',
            style: TextStyle(
              color: Colors.black,
            ),
          ),
          value: selectedmobile,
          items: _dropdownTestItems,
          onChanged: (selectedTest) {
            setState(() {
              selectedmobile = selectedTest;
            });
          },
        ),
      ),
   

您的结果屏幕->图片

于 2022-02-27T10:04:12.583 回答