flutter - 调用setState时如何强制flutter重建特定的小部件

标签 flutter dart widget setstate

我有一个stateful widget类。它的build方法中有一个listView。在列表 View 中,我有两个dropDownButtonFormField。最初,仅显示第一个下拉列表。在to中选择一个值后,通过调用set state绘制第二个下拉列表。第二个下拉按钮取决于第一个按钮的选择。

现在代码可以正常工作了。在第一个下拉按钮中选择值之后,第二个下拉按钮将出现,其中包含与第一个选定值相对应的选项。但是,从第二个按钮中选择一个选项并更改第一个按钮的选项时,会发生错误。

每当第一个下拉菜单的选定选项更改时,如何强制Flutter重新绘制第二个下拉菜单?

这是应用程序正在运行并崩溃的地方:

app giving error

码:

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

最佳答案

运行$ flutter upgrade起作用。

关于flutter - 调用setState时如何强制flutter重建特定的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62183364/

相关文章:

dart - dart 可以替代 php 服务器端脚本吗?

flutter - 使用 flutter_local_notifications 时,如何在 Flutter 中将 Future<Null> 转换为 Future<dynamic>?

flutter - 如何在 flutter 中更改 snackbar 的不透明度?

flutter - 如何使某物变恒定?

flutter - Dart 包版本如何工作以及我应该如何版本我的 Flutter 插件?

flutter - 基于 bool 值的排序列表

widget - 在静态调用的方法中使用小部件

javascript - 注册按钮在我的 Launchrock 小部件中不起作用

python - 如何对 Django Select 小部件中的选择进行分组?

flutter - Hive Flutter 用法