【问题标题】:Flutter how to set the value of the dropdown button programmaticallyFlutter 如何以编程方式设置下拉按钮的值
【发布时间】:2022-01-09 22:26:26
【问题描述】:

我是 Flutter 新手,需要帮助以编程方式设置 DropdownButton 的值。 该值来自文本字段。一旦我点击它,它会自动设置下拉按钮的值。

Widget _districtListContainer() {
    return Container(
        width: 360.0,
        child: new InputDecorator(
          decoration: InputDecoration(
              suffixIcon: new Icon(
                Icons.search,
                color: Colors.blue[700],
              ),
              labelText: 'Select District',
              labelStyle: TextStyle(fontSize: 12.0)),
          isEmpty: _selectedDistrict == null,
          child: new DropdownButtonHideUnderline(
            child: new DropdownButton<District>(
             
              value: _selectedDistrict,
              isDense: true,
              isExpanded: false,
              onChanged: (District newValue) {
                setState(() {
                  _selectedDistrict = newValue;
             
                });
              },
              items: _listDistrict?.map((District value) {
                    return new DropdownMenuItem<District>(
                      value: value,
                      child: new Text(
                        value.district != null ? value.district : '',
                        style: new TextStyle(fontSize: 11.0),
                      ),
                    );
                  })?.toList() ??
                  [],
            ),
          ),
        ),
        margin: EdgeInsets.only(bottom: 10.0));
  }

谢谢

【问题讨论】:

    标签: flutter flutter-dropdownbutton


    【解决方案1】:

    首先,将数据添加到列表中[] 从 TextFormfield 然后将列表检索到 DropDownButton 项中。

    另外,请确保下拉按钮列表显示 Textformfield 数据插入活动无法同时更新。

    【讨论】:

    • 实际上我想要做的是从另一个列表项中将一个值传递给 _selectedDistrict。任何样品都会有很大帮助。谢谢
    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 2014-09-20
    • 2014-09-03
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多