我有2个DropdownButtonFormField
我在那里可以选择汽车。我需要根据用户从 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);
});
},
),
The DropDown for the first button:
当然,因为我背后没有逻辑,所以我将其作为模型选择,无论我从汽车列表中选择什么,但我希望它只从您选择的汽车列表中映射出模型。