【问题标题】:How to force flutter to rebuild a specific widget when setState is called如何在调用 setState 时强制颤振重建特定的小部件
【发布时间】:2020-09-22 18:32:44
【问题描述】:

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

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

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

这是运行和崩溃的应用程序:

代码:

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

【问题讨论】:

  • 可以分享_getProvinceDropDown()_getCityDropDown()的代码吗?
  • @SanjaySharma 发布

标签: flutter dart widget setstate


【解决方案1】:

运行 $ flutter upgrade 工作正常。

【讨论】:

  • 你能发布一个参考你是如何想出他的吗?
  • @zonksoft 我在朋友的 android studio 上运行了相同的代码,它工作正常。所以我对我的进行了颤振升级,之后它就起作用了
【解决方案2】:

代替_selectedCity = null,试试_selectedCity = _getCities().first;

【讨论】:

    猜你喜欢
    • 2019-01-14
    • 2021-03-18
    • 2020-09-07
    • 1970-01-01
    • 2022-01-11
    • 2020-08-05
    • 2020-04-09
    • 1970-01-01
    • 2019-11-29
    相关资源
    最近更新 更多