Search code examples
flutterdropdownunselect

flutter: dropdown item programmatically unselect problem


I use two dropdown menu here.

Both data come from API. And second's one data depends on first dropdown items. Thats mean when I select an item from first menu then data will come on second dropdown's. And It's changing dynamically.

I face a problem here. When I change a items from first dropdown, second dropdown show me a error. Like this -

Before

enter image description here

After Change City value

enter image description here

Here is my Code for two dropdown

   // 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;
                });
              },
            ),

Here is 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,
          ),
        ));   } }

That's why I want to unselect second's on dropdown menu items programmatically, but not find a solution. Please some one help me.


Solution

  • set _weight value null on city change