【发布时间】:2020-09-22 18:32:44
【问题描述】:
我有一个stateful widget 课程。它的 build 方法中有一个 listView。在列表视图中,我有两个 dropDownButtonFormField。最初,仅显示第一个下拉菜单。在 to 中选择一个值后,通过调用 set state 绘制第二个下拉列表。第二个下拉按钮取决于第一个按钮的选择。
现在代码可以正常工作了。在第一个下拉按钮中选择值后,将出现第二个下拉按钮,其中包含与第一个选择的值相对应的选项。但是,选择“第二个按钮的选项”并更改第一个按钮的选项时出现错误。
每次更改第一个下拉菜单的选定选项时,如何强制颤振重绘第二个下拉菜单?
这是运行和崩溃的应用程序:
代码:
//called inside the list view childrens list
Widget showLocationDropDown() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
child: Container(
child: Column(
children: <Widget>[
_getProvinceDropDown(),
_selectedProvince == null ? Container(): _getCityDropDown(),
],
),
),
);
}
Widget _getCityDropDown() {
return Column(
children: <Widget>[
Container(
height: 20,
),
Row(
children: <Widget>[
Icon(Icons.location_city, color: Colors.grey),
Container(
width: 17.0,
),
Expanded(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButtonFormField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(color: Colors.grey))),
hint: Text('Please choose your city'),
value: _selectedCity,
onChanged: (newValue) {
setState(() {
_selectedCity = newValue;
});
},
items: _getCities().map((location) {
return DropdownMenuItem(
child: Text(location),
value: location,
);
}).toList(),
),
),
),
],
),
],
);
}
List<String> _getCities() {
switch (_selectedProvince) {
case ("Sindh"):
return _sindhCities;
case ("Punjab"):
return _punjabCities;
case ("Balouchistan"):
return _balouchCities;
case ("KPK"):
return _kpkCities;
}
List<String> deflt = ['Please Select Province'];
return deflt;
}
Widget _getProvinceDropDown() {
return Row(
children: <Widget>[
Icon(Icons.map, color: Colors.grey),
Container(
width: 17.0,
),
Expanded(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButtonFormField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(color: Colors.grey),
),
),
hint: Text('Please choose your province'),
value: _selectedProvince,
onChanged: (newValue) {
setState(() {
_selectedProvince = newValue;
_selectedCity = null;
stateChange = !stateChange;
});
},
items: _province.map((province) {
return DropdownMenuItem(
child: Text(province),
value: province,
);
}).toList(),
),
),
),
],
);
}
【问题讨论】:
-
可以分享
_getProvinceDropDown()和_getCityDropDown()的代码吗? -
@SanjaySharma 发布
标签: flutter dart widget setstate