下拉菜单颤振的重置值

时间:2020-07-08 12:13:32

标签: flutter dart

我有两个下拉列表,下拉列表1列出作业类别下拉列表2包含基于类别的列表作业。当我选择下拉列表1时,相应的数据应显示在下拉列表2中。

一切正常,但问题是,无论何时我更改dropdown1的值,然后下拉菜单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();
      }
    });
  }

1 个答案:

答案 0 :(得分:0)

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));
}
...