【发布时间】:2020-07-08 12:13:32
【问题描述】:
我有两个下拉列表,下拉 1 列出工作类别下拉 2 包含基于类别的列表工作。当我选择下拉 1 时,相应的数据应显示在下拉 2 中。
一切正常,但问题是当我更改下拉列表1的值时,下拉列表2每次都会显示以前的数据以及下拉列表的选定数据,API没有任何问题
Flexible(
flex: 0,
child: Padding(
padding: EdgeInsets.only(left: 10, right: 10, top: 15),
child: Container(
width: double.infinity,
height: 45,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
border: Border.all(color: Colors.blueGrey)),
child: DropdownButton(
isExpanded: true,
itemHeight: 50,
icon: Icon(Icons.arrow_drop_down),
iconSize: 40,
underline: SizedBox(),
hint: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Job Category",
style: TextStyle(fontSize: 15, color: Colors.black54),
),
),
value: _selectedmenu,
onChanged: (newValue) {
setState(() {
_selectedmenu = newValue;
print(_selectedmenu);
GetWorkCategories();
});
},
items: home_model.map((menu) {
return DropdownMenuItem(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: new Text(menu.english),
),
value: menu.id,
);
}).toList(),
),
),
),
),
Flexible(
flex: 0,
child: Padding(
padding: EdgeInsets.only(left: 10, right: 10, top: 15),
child: Container(
width: double.infinity,
height: 45,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
border: Border.all(color: Colors.blueGrey)),
child: DropdownButton(
isExpanded: true,
itemHeight: 50,
icon: Icon(Icons.arrow_drop_down),
iconSize: 40,
underline: SizedBox(),
hint: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
AppLocalization.of(context)
.getTranslatedValues('job_text'),
style: TextStyle(fontSize: 15, color: Colors.black54),
),
),
value: _selectedjobcategory,
onChanged: (newValue) {
setState(() {
_selectedjobcategory = newValue;
});
},
items: job_category_model.map((jobs) {
return DropdownMenuItem(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(jobs.english),
),
value: jobs.english,
);
}).toList(),
),
),
),
),
下拉2功能
Future<String> GetWorkCategories() async {
Future auth_token = SharedPrefrence().getToken();
auth_token.then((data) async {
token = data;
var response = await http.post(Urls.CATEGORIES,
headers: {"Content-Type": "application/json", "Authorization": token},
body: json.encode({"menuId": _selectedmenu}));
print("response menu " + response.body.toString());
Map<String, dynamic> value = json.decode(response.body);
var message = value['status'];
try {
if (response.statusCode == 200) {
try {
if (message == true) {
var menu_list = value['doc'];
job_category_model.clear();
for (int i = 0; i < menu_list.length; i++) {
var data = menu_list[i];
job_category_model.add(CategoryModel.fromJson(data));
}
setState(() {
print("UI Updated");
});
} else {
final snackBar = SnackBar(content: Text(message));
_scaffoldKey.currentState.showSnackBar(snackBar);
}
} catch (e) {
e.toString();
}
} else {
final snackBar = SnackBar(content: Text(message));
_scaffoldKey.currentState.showSnackBar(snackBar);
}
} catch (e) {
e.toString();
}
});
}
【问题讨论】: