flutter - 重置下拉菜单颤动的值
问题描述
我有两个下拉列表,下拉 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();
}
});
}
解决方案
Morbius验证您API code
并确保在添加新数据行之前重置_selectedjobcategory = null;
并清除以前的数据。job_category_model.clear();
GetWorkCategories();
for
循环前:
...
var menu_list = value['doc'];
_selectedjobcategory = null;
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));
}
...
推荐阅读
- typescript - 从泛型类调用类的静态方法
- excel - customtab 和 contextcellmenu :按钮激活/取消激活
- c++ - 返回字符串中的最后一个元音
- workbox - 如何预缓存工作箱 NavigationRoute NetworkFirst 请求?
- windows - 如何检查 Windows 10 设备是否连接到公共或私人 WiFi?
- python - 使用请求时无法模仿 FormRequest.from_response
- mongodb - MongoDB 中的 explain.executionStats.executionTimeMillis 和 MariaDB 中的 SHOW PROFILES 之间的区别
- css - SeleniumScript 识别 css 元素
- integer - 在 HOL 中证明非负整数是有根据的。(或者只是他们的归纳原理。)
- python-3.x - 在 Streamlink 直播流上使用 OpenCV-Python?