简体   繁体   English

如何在 Flutter 中制作多级可靠下拉列表?

[英]How can I make a multi level dependable dropdown List in Flutter?

I have been working on having two dropdown buttons that are meant for the user to easily pick the car model and its respective make.我一直在努力设计两个下拉按钮,以便用户轻松选择汽车 model 及其各自的品牌。 Below is the code snippet of the static lists that I have.下面是我拥有的 static 列表的代码片段。 Any feedback is highly appreciated.任何反馈都非常感谢。

final List<String> carModel = [
    'Audi',
    'BMW',
    'Chevrolet',
    'Chrysler',
    'Daihatsu',
    'Ford',
    'Hino',
    'Honda',
    'Isuzu',
    'Jaguar',
    'Jeep',
    'Landrover',
    'Lexus',
    'Mazda',
    'Mercedes',
    'Mitsubishi',
    'Nissan',
    'Peugeot',
    'Porsche',
    'Subaru',
    'Suzuki',
    'Toyota',
    'UD',
    'Volkswagen',
    'Volvo'
  ];
  final List<String> audiMake = [
    'A3',
    'A4',
    'A5',
    'A6',
    'A7',
    'A8',
    'Q3',
    'Q5',
    'Q7',
    'Q8',
    'R8',
    'TT',
  ];
  final List<String> bmwMake = [
    '1 Series',
    '2 Series',
    '3 Series',
    '4 Series',
    '5 Series',
    '6 Series',
    '7 Series',
    '8 Series',
    'M2',
    'M3',
    'M4',
    'M5',
    'M6',
    'X1',
    'X2',
    'X3',
    'X4',
    'X5',
    'X6',
    'X7',
    'Z4',
    'i3',
    'i8',
  ];

Here is the code that I have been working in bringing the functionality of the dropdown buttons to work:这是我一直在努力使下拉按钮的功能起作用的代码:

 DropdownButton<String>(
                value: selectedCarModel,
                items: carModel.map((e) {
                  return DropdownMenuItem<String>(
                    value: e,
                    child: Text('$e'),
                  );
                }).toList(),
                onChanged: (val) {
                  setState(() {
                    selectedCarModel = val!;
                  });
                }),
            const SizedBox(
              height: 10,
            ),
            DropdownButton<String>(
                value: selectedCarMake,
                items: carMake.map((e) {
                  return DropdownMenuItem<String>(
                    value: e,
                    child: Text('$e'),
                  );
                }).toList(),
                onChanged: (val) {

                  carMake = val == 'Audi' ? audiMake : bmwMake;
                  setState(() {
                    selectedCarMake = val!;
                  });
                }),

The concept using multi dropdown button will be second dropdown always depend on 1st dropdown value.使用多下拉按钮的概念将是第二个下拉菜单始终取决于第一个下拉菜单值。 It is necessary to make dropdown button nullable.有必要使下拉按钮可以为空。 So if the 1st dropdown button change, Second dropdown button must have null, else it will provide error based if 1st dropdown button selected something and second dropdown button doesn't contain that value.因此,如果第一个下拉按钮更改,第二个下拉按钮必须具有 null,否则如果第一个下拉按钮选择了某些内容并且第二个下拉按钮不包含该值,它将提供错误。 OK code works better than my explanations. OK 代码比我的解释更有效。

Let's simplify the process using Map.让我们使用 Map 简化流程。

class MultiLevelDropDownExample extends StatefulWidget {
  const MultiLevelDropDownExample({Key? key}) : super(key: key);

  @override
  State<MultiLevelDropDownExample> createState() =>
      _MultiLevelDropDownExampleState();
}

class _MultiLevelDropDownExampleState extends State<MultiLevelDropDownExample> {
  final List<String> audiMake = [
    'A3',
    'A4',
  ];
  final List<String> bmwMake = [
    '1 Series',
    '2 Series',
  ];

  String? selectedCardModel;
  String? selectedMake;

  late Map<String, List<String>> dataset = {
    'Audi': audiMake,
    'BMW': bmwMake,
  };

  onCarModelChanged(String? value) {
    //dont change second dropdown if 1st item didnt change
    if (value != selectedCardModel) selectedMake = null;
    setState(() {
      selectedCardModel = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          DropdownButton<String?>(
              value: selectedCardModel,
              items: dataset.keys.map((e) {
                return DropdownMenuItem<String?>(
                  value: e,
                  child: Text('$e'),
                );
              }).toList(),
              onChanged: onCarModelChanged),
          const SizedBox(
            height: 10,
          ),
          DropdownButton<String?>(
              value: selectedMake,
              items: (dataset[selectedCardModel] ?? []).map((e) {
                return DropdownMenuItem<String?>(
                  value: e,
                  child: Text('$e'),
                );
              }).toList(),
              onChanged: (val) {
                setState(() {
                  selectedMake = val!;
                });
              }),
          //
        ],
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM