【问题标题】:Dropdown is not keeping the value selected下拉菜单未保留选定的值
【发布时间】:2021-07-07 10:49:01
【问题描述】:

我正在处理一个下拉菜单,并且我在变量中有值,但我无法显示所选值,而是始终打开一个静态值。

到目前为止,我是这样做的:

class DropDownWidget extends State {
  String dropdownValue = 'Activities';
  String holder = '';

  List<String> postType = ['Activities', 'Sell/buy', 'New Friends', 'City Recommendations', 'Post'];

  void getDropDownItem() {
    setState(() {
      holder = dropdownValue;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 100,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/img/blue.png'),
          fit: BoxFit.cover,
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
      
                      Container(
           
            
            child: DropdownButton<String>(
              value: dropdownValue,
              icon: Icon(
                Icons.keyboard_arrow_down_outlined,
                color: TheBaseColors.lightRed,
              ),
              iconSize: 30,
              elevation: 16,
              style: TextStyle(color: TheBaseColors.lightRed, fontSize: 18),
              onChanged: (String data) {
                setState(() {
                  dropdownValue = data;
                });
                switch (data) {
                  case 'Activities':
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => CreateActivity()),
                    );
                    break;
                  case 'Sell/buy':
                    Navigator.push(
                      context,
                    
                }
              },
              items: postType.map<DropdownMenuItem<String>>((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
            ),
          ),
        ],
      ),
    );
  }
}

我已经将dropdow的值保存在一个变量中,然后在SetState中设置,但是如何每次都显示选中的item呢?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    试试这个

    String dropdownValue = 'Activities';
    DropdownButton<String>(
        value: dropdownValue,
        icon: const Icon(Icons.arrow_downward),
        iconSize: 24,
        elevation: 16,
        style: const TextStyle(color: Colors.deepPurple),
        underline: Container(
          height: 2,
          color: Colors.deepPurpleAccent,
        ),
        onChanged: (String? newValue) {
          setState(() {
            dropdownValue = newValue!;
          });
        },
        items: <String>['Activities', 'Sell/buy', 'New Friends', 'City Recommendations', 'Post']
            .map<DropdownMenuItem<String>>((String value) {
          return DropdownMenuItem<String>(
            value: value,
            child: Text(value),
          );
        }).toList(),
      ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多