【问题标题】:DropdownButton can't update it's value(null)DropdownButton 无法更新它的值(null)
【发布时间】:2019-07-01 12:16:28
【问题描述】:

整页代码很长,但我的 DropdownButton 小部件代码是这样的。

问题是,

首先:我无法更新我的 selectedCity,它没有得到更新。此外,打印函数调用 null,因为我的 cityList 数据类似于 [new york, paris, london] 等...

second:flutter 不会将焦点从任何 TextField 完全更改为 DropdownButton。我的意思是,单击 TextField,然后单击 DropdownButton,但在单击按钮后焦点恢复到该 TextField。是 Flutter 的默认动作吗?

  List<dynamic> _cityList;
  String _selectedCity;

  @override
  Widget build(BuildContext context) {
    return DropdownButton(
      value: _selectedCity,
      style: TextStyle(
        fontSize: 11,
        color: textColor,
      ),
      items: _cityList.map((city) {
        return DropdownMenuItem<String>(
          child: Padding(
            padding: const EdgeInsets.only(left: 4),
            child: Text(city),
          ),
        );
      }).toList(),
      onChanged: (String value) {
        setState(() {
          _selectedCity = value;
          print(_selectedCity);
        });
      },
      isExpanded: true,
    );
  }

编辑:从 DropdownMenuItem 中选择项目后重置 FocusNode 的解决方案是在 setstate 中添加这一行,如下所示:

这个:FocusScope.of(context).requestFocus(new FocusNode());

到这里:onChanged:(){setSate((){here}}

【问题讨论】:

  • 您的解决方案修复了我在过去 3 小时内一直在寻找的东西。谢谢。

标签: dart flutter


【解决方案1】:

希望对你有所帮助。我稍微修改了你的代码

 List<dynamic> _cityList;
 String _selectedCity;

它将显示下拉按钮,当您单击它并选择打印中显示的任何值时

 @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      body: ListView(
        children: [
          Column(
            children: <Widget>[
              DropdownButton<String>(
                items: _cityList.map((dynamic value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: new Text(value),
                  );
                }).toList(),
                onChanged: (value) {
                  setState(() {
                    _selectedCity = value;
                    print(_selectedCity);
                  });
                },
              ),
            ],
          ),
        ],
      ),
    );
  }

【讨论】:

  • 谢谢!小错误是没有将 value 参数添加到我的 DropdownMenuItem 中。其他都一样。
  • @mathronaut 如果以上代码对您有帮助,请采纳答案。
  • hai Sunny @Sunny,我有下拉菜单,当我在 onchanged 函数中打印值时,我该怎么办?
  • @RohmatulLaily 你需要检查 _cityList(Array List) 包含的数据不是空值。
【解决方案2】:

对于焦点问题,您应该使用focusNodes 一个带有下拉列表,另一个带有文本字段https://docs.flutter.io/flutter/widgets/FocusNode-class.html

【讨论】:

  • 感谢您的努力,解决方案是:将这个添加到DropdownMenuItem的setState中:FocusScope.of(context).requestFocus(FocusNode());
猜你喜欢
  • 2020-09-16
  • 2020-04-04
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 2020-12-29
  • 2014-08-17
  • 1970-01-01
  • 2021-08-21
相关资源
最近更新 更多