我有 2DropdownButtonFormField
秒,我有一些汽车可供选择。我需要根据用户从 DropdownButtonFormField 中的第一个选择中选择的汽车型号更改按钮的第二个选择(即如果用户在第一个选择中选择梅赛德斯,在下面的 DropdownButtonFormField 中,我只想显示梅赛德斯的型号而不是,比方说,奥迪)。
我怎样才能做到这一点?这是代码:
String _make, _model;
/// List of cars and models
List<String> carList = [
'Audi',
'BMW',
'Mercedes',
];
List<String> modelAudi = ['A6', 'A8', 'Q7',];
List<String> modelMercedes = ['E-Class', 'S-Class','Maybach'];
List<String> modelBMW = ['3-Series', 'X5', 'X7'];
/*two DropdownButtonFormFields, but the second one needs to match
it's car manufacturer selection from the carList selection
(i.e. if you select Audi, it must only show the modelAudi list (A6,
A8, Q7) in the second DropdownButtonFormField)
*/
DropdownButtonFormField<String>(
value: _make,
items: carList
.map((label) => DropdownMenuItem(
child: Text(label.toString()),
value: label,
))
.toList(),
onChanged: (value) {
setState(() {
_make = value;
print(value);
});
},
),
DropdownButtonFormField<String>(
value: _model,
/* here is where I need to implement logic
that maps out the model names that matches the car maker
*/
items: modelAudi
.map((label) => DropdownMenuItem(
child: Text(label.toString()),
value: label,
))
.toList(),
onChanged: (value) {
setState(() {
_model = value;
print(value);
});
},
),
当然,因为我没有背后的逻辑,所以无论我从汽车列表中选择什么,我都会将其作为模型选择,但我希望它只映射您选择的汽车列表中的模型。