Flutter 颤振:以编程方式取消选择问题的下拉项

Flutter 颤振:以编程方式取消选择问题的下拉项,flutter,dropdown,programmatically,unselect,Flutter,Dropdown,Programmatically,Unselect,我在这里使用两个下拉菜单 这两个数据都来自API。第二个数据取决于第一个下拉项。这意味着当我从第一个菜单中选择一个项目时,数据将出现在第二个下拉菜单中。而且它是动态变化的 我在这里面临一个问题。当我从第一个下拉列表更改项目时,第二个下拉列表显示错误。 像这样- 以前 改变城市价值后 这是我的两个下拉列表的代码 // Choose City CustomDropDownMenu( items: allCity.map((list) {

我在这里使用两个下拉菜单

这两个数据都来自API。第二个数据取决于第一个下拉项。这意味着当我从第一个菜单中选择一个项目时,数据将出现在第二个下拉菜单中。而且它是动态变化的

我在这里面临一个问题。当我从第一个下拉列表更改项目时,第二个下拉列表显示错误。 像这样-

以前

改变城市价值后

这是我的两个下拉列表的代码

   // Choose City
            CustomDropDownMenu(
              items: allCity.map((list) {
                return DropdownMenuItem(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 20.0),
                    child: Text(
                      "${list["name"]}",
                      style: TextStyle(),
                    ),
                  ),
                  onTap: () {
                    setState(() {
                      isVisible = false;
                    });

                    getWeight(list["id"]).then((value) => {
                          setState(() {}),
                        });
                    print(list["id"]);
                    FocusScope.of(context).unfocus();
                    print(weightList.length);
                  },
                  value: list["id"].toString(),
                );
              }).toList(),
              value: _city,
              hint: "Choose City",
              onChanged: (value) {
                setState(() {
                  this._city = value;
                });
              },
            ),
// Weight
 CustomDropDownMenu(
              hint: "Select Weight",
              value: _weight,
              items: [
                DropdownMenuItem(child: Text("Select Weight")),
                ...weightList.map((list) {
                  return DropdownMenuItem(
                    child: Padding(
                      padding: const EdgeInsets.only(left: 20.0),
                      child: Text(
                        "${list["weight"]}",
                        style: TextStyle(),
                      ),
                    ),
                    onTap: () {
                      FocusScope.of(context).unfocus();
                    },
                    value: list["id"].toString(),
                  );
                }).toList()
              ],
              onChanged: (value) {
                setState(() {
                  _weight = value;
                });
              },
            ),
这里是
CustomDropDown()
class

.. class CustomDropDownMenu extends StatelessWidget {   final String hint;   final dynamic value;

  final Function onChanged;   final Function onSaved;   final List<DropdownMenuItem<dynamic>> items;

  const CustomDropDownMenu({
    Key key,
    this.hint,
    this.onChanged,
    this.onSaved,
    this.items,
    this.value,   }) : super(key: key);   @override   Widget build(BuildContext context) {
    double _width = MediaQuery.of(context).size.width;
    return Container(
        decoration: BoxDecoration(
          shape: BoxShape.rectangle,
          borderRadius: BorderRadius.all(
            Radius.circular(60),
          ),
        ),
        child: Card(
          shape: StadiumBorder(),
          elevation: 5,
          child: DropdownButtonFormField(
            style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 18.0,
                color: Colors.black),
            hint: Padding(
              padding: const EdgeInsets.only(left: 20.0),
              child: Text(
                hint,
                textAlign: TextAlign.end,
              ),
            ),
            decoration: InputDecoration(
              enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.transparent),
              ),
            ),
            value: value,
            onChanged: onChanged,
            onSaved: onSaved,
            items: items,
          ),
        ));   } }
。。类CustomDropDownMenu扩展无状态小部件{最终字符串提示;最终动态值;
更改最终功能;保存最终功能;最终列表项;
常量自定义下拉菜单({
关键点,
这个提示,,
一旦改变了,
这是我的储蓄,
这个项目,,
this.value,}):super(key:key);@override小部件构建(BuildContext){
double _width=MediaQuery.of(context).size.width;
返回容器(
装饰:盒子装饰(
形状:BoxShape.rectangle,
borderRadius:borderRadius.all(
圆形半径(60),
),
),
孩子:卡片(
形状:StadiumBorder(),
标高:5,
子项:DropdownButtonFormField(
样式:TextStyle(
fontWeight:fontWeight.bold,
字体大小:18.0,
颜色:颜色。黑色),
提示:填充(
填充:仅限常量边集(左:20.0),
子:文本(
提示,,
textAlign:textAlign.end,
),
),
装饰:输入装饰(
enabledBorder:UnderlineInputBorder(
borderSide:borderSide(颜色:颜色。透明),
),
),
价值:价值,
一旦改变:一旦改变,
已保存:已保存,
项目:项目,,
),
));   } }

这就是为什么我想以编程方式取消选择下拉菜单项上的第二个,但找不到解决方案。请找人来帮我。

设置城市变化的权重值
空值
将您的CustomDropDownMenu转换为有状态小部件并实现以下代码:

  @override
 void didUpdateWidget(covariant AppDropDown oldWidget) {
   super.didUpdateWidget(oldWidget);
   if (!listEquals(oldWidget.items, widget.items)) {
      value = null;
   }
}