简体   繁体   中英

How to force flutter to rebuild a specific widget when setState is called

I have a stateful widget class. It has a listView in its build method. Inside the list view I have two dropDownButtonFormField . Initially, only the first drop down is shown. After a value is selected in to, the second drop down is drawn by calling the set state . The second drop down button is dependent on the first button's choice.

Now the code works fine. After selecting the value in the first drop down button, the second drop down button appears with options corresponding to the first's selected value. But error occurs when a an option from the second button is selected and first button's option is changed.

How do I force flutter to redraw the second dropdown every time the first drop down's selected option is changed?

Here is the app running and crashing:

应用程序给出错误

Code:

//called inside the list view childrens list
Widget showLocationDropDown() {
    return Padding(
      padding: const EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
      child: Container(
        child: Column(
          children: <Widget>[
            _getProvinceDropDown(),
            _selectedProvince == null ? Container(): _getCityDropDown(),
          ],
        ),
      ),
    );
  }

 Widget _getCityDropDown() {
    return Column(
      children: <Widget>[
        Container(
          height: 20,
        ),
        Row(
          children: <Widget>[
            Icon(Icons.location_city, color: Colors.grey),
            Container(
              width: 17.0,
            ),
            Expanded(
              child: ButtonTheme(
                alignedDropdown: true,
                child: DropdownButtonFormField(
                  decoration: InputDecoration(
                      enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(25.0),
                          borderSide: BorderSide(color: Colors.grey))),
                  hint: Text('Please choose your city'),
                  value: _selectedCity,
                  onChanged: (newValue) {
                    setState(() {
                      _selectedCity = newValue;
                    });
                  },
                  items: _getCities().map((location) {
                    return DropdownMenuItem(
                      child: Text(location),
                      value: location,
                    );
                  }).toList(),
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  List<String> _getCities() {
    switch (_selectedProvince) {
      case ("Sindh"):
        return _sindhCities;

      case ("Punjab"):
        return _punjabCities;

      case ("Balouchistan"):
        return _balouchCities;

      case ("KPK"):
        return _kpkCities;
    }
    List<String> deflt = ['Please Select Province'];
    return deflt;
  }

  Widget _getProvinceDropDown() {
    return Row(
      children: <Widget>[
        Icon(Icons.map, color: Colors.grey),
        Container(
          width: 17.0,
        ),
        Expanded(
          child: ButtonTheme(
            alignedDropdown: true,
            child: DropdownButtonFormField(
              decoration: InputDecoration(
                enabledBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(25.0),
                  borderSide: BorderSide(color: Colors.grey),
                ),
              ),
              hint: Text('Please choose your province'),
              value: _selectedProvince,
              onChanged: (newValue) {
                setState(() {
                  _selectedProvince = newValue;
                  _selectedCity = null;
                  stateChange = !stateChange;
                });
              },
              items: _province.map((province) {
                return DropdownMenuItem(
                  child: Text(province),
                  value: province,
                );
              }).toList(),
            ),
          ),
        ),
      ],
    );
  }

Instead of _selectedCity = null, try _selectedCity = _getCities().first;

running $ flutter upgrade worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM