简体   繁体   English

DropdownButton 无法更新它的值(空)

[英]DropdownButton can't update it's value(null)

The full page code is very long but my DropdownButton widget code like this.整页代码很长,但我的 DropdownButton 小部件代码是这样的。

The problems are,问题是,

first: I can't update my selectedCity, it doesn't get an update.第一:我无法更新我的 selectedCity,它没有得到更新。 Also, the print function calls null, since my cityList data is like [new york, paris, london] etc...此外,打印函数调用 null,因为我的 cityList 数据类似于 [new york, paris, london] 等...

second: flutter doesn't change focus from any TextField to DropdownButton fully.第二:flutter 不会完全将焦点从任何 TextField 更改为 DropdownButton。 I mean, clicked TextField, then DropdownButton but focus reverts to that TextField after the button click.我的意思是,单击 TextField,然后单击 DropdownButton,但在单击按钮后焦点恢复到该 TextField。 It is default action of Flutter?它是 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,
    );
  }

Edit: The solution of resetting FocusNode after selecting an item from DropdownMenuItem is adding this line inside of setstate like this:编辑:从 DropdownMenuItem 选择项目后重置 FocusNode 的解决方案是在 setstate 中添加这一行,如下所示:

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

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

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

I hope it will help you.我希望它会帮助你。 I have modified your code a little bit我稍微修改了你的代码

 List<dynamic> _cityList;
 String _selectedCity;

It will show the Dropdown Button and when you click on it and select any value showing in the print它将显示下拉按钮,当您单击它并选择打印中显示的任何值时

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 下拉按钮中的错误。 应该只有一项具有 [DropdownButton] 的值, - Error in DropdownButton. There should be exactly one item with [DropdownButton]'s value, DropdownButton '项目== null || 项目.isEmpty || 值 == null || items.where((下拉菜单项<t> item) { 返回 item.value == value;</t> - DropdownButton 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) { return item.value == value; Flutter DropDownButton 即使在更新时也不会显示选定的值 - Flutter DropDownButton won't display selected value even when I update it 应该只有一项具有 [DropdownButton] 的值 - There should be exactly one item with [DropdownButton]'s value DropdownButton 不反映值变化 - DropdownButton doesn't reflect value change 无法使用 Flutter 中的对象列表创建 DropdownButton - Can't create DropdownButton with List of objects in Flutter 无法设置 TimeofDay 的值,始终为 null - Can't set value for TimeofDay, it's always null 在 flutter 中使用下拉按钮时出现“[DropdownButton] 的值应该只有一个项目:Item1”错误 - "There should be exactly one item with [DropdownButton]'s value: Item1" error when using dropdownbutton in flutter Flutter - 依赖/多级 DropdownButton 有一个问题:应该只有一项具有 [DropdownButton] 的值:Ointments - Flutter - Dependent/Multilevel DropdownButton has an issue: There should be exactly one item with [DropdownButton]'s value: Ointments 如何解决 flutter dropdownButtonFormField 动态选择检查 dropdownButton 的值 - How to resolve flutter dropdownButtonFormField dynamic selection checking for dropdownButton's value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM