【问题标题】:flutter: dropdown item programmatically unselect problem颤振:下拉项目以编程方式取消选择问题
【发布时间】:2021-04-20 16:23:06
【问题描述】:

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

这两个数据都来自 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 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,
          ),
        ));   } }

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

【问题讨论】:

    标签: flutter dropdown programmatically unselect


    【解决方案1】:

    在城市变化时设置_weightnull

    【讨论】:

      【解决方案2】:

      将您的 CustomDropDownMenu 转换为有状态小部件并实现以下代码:

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

      }

      【讨论】:

        猜你喜欢
        • 2014-04-05
        • 1970-01-01
        • 2021-07-22
        • 2012-02-05
        • 1970-01-01
        • 2022-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多