【问题标题】:Drop down problem after onChanged in Flutter/dart在 Flutter/dart 中 onChanged 后下拉问题
【发布时间】:2019-07-06 12:53:12
【问题描述】:

我正在处理下拉菜单。在菜单项中,我有一行在左侧显示图标,在右侧显示文本。

问题:

当我从下拉列表中选择项目时,它同时显示图标和文本,但我只需要显示文本。在调试中我打印出值,它只显示正确的文本。

注意:如果我从行中删除图标,那么它工作正常。

调试控制台

菜单项

选择项目后,图标和文本都显示,但我只想显示文本

下面是代码:

class _MyHomePageState extends State<MyHomePage>  {
  String _mySelection;
  List<Map> _myJson = [

  {"id":1,"name":"All List"},  
  {"id":2,"name":"Default"},
  {"id":3,"name":"Personal"},
  {"id":4,"name":"Shopping"},
  {"id":5,"name":"Wishlist"},
  {"id":6,"name":"Work"},
  {"id":7,"name":"Finished"}];
  @override
  void initState(){
    super.initState();
  }
Icon actionIcon =  Icon(Icons.search);
 Widget appBarTitle =  Text("AppBar Title");
  @override
  Widget build(BuildContext context) { 
    return Scaffold(

      appBar: AppBar( 

        title: Theme(
              data: Theme.of(context).copyWith(
                canvasColor: Theme.of(context).primaryColor
              ),
              child: DropdownButton(
              items: _myJson.map((item) {
                return  DropdownMenuItem(

                  child: Row(
                    children: <Widget>[
                        Padding(
                          padding: EdgeInsets.symmetric(horizontal: 10.0),
                          child: Icon(Icons.menu,size: 13),
                        ),
                      Text(
                        item['name'],
                        style: TextStyle(color: Colors.white, fontSize: 17,)
                      ),
                    ],
                  ),
                  value: item['name']
                );
              }).toList(),

              onChanged: (newvalue) {  
                setState(() {
                  _mySelection = newvalue;
                  print(_mySelection); 
                }); 
              },

              hint:  Text(_myJson[0]["name"],style: TextStyle(color: Colors.white,fontSize: 17)),

              value:_mySelection,
              ), // Your Dropdown Code Here,
            ),

           actions: <Widget>[

             IconButton(
              icon: Icon(Icons.search, color: Colors.white),onPressed:null,

        ),]


      ),
      body: null,


    );
  }//end of Widget build 


}//end of _MyHomePageState

【问题讨论】:

    标签: drop-down-menu dart flutter row flutter-layout


    【解决方案1】:

    通过注释或删除这行代码来尝试此操作并再次检查。

    child: Icon(Icons.menu,size: 13),

    【讨论】:

      【解决方案2】:

      试试这个

      Scaffold(
      
        appBar: AppBar( 
      
          title: Theme(
                data: Theme.of(context).copyWith(
                  canvasColor: Theme.of(context).primaryColor
                ),
                child: DropdownButton(
                items: _myJson.map((item) {
                  return  DropdownMenuItem(
      
                    child: Row(
                      children: <Widget>[                       
                           Padding(
                            padding: EdgeInsets.symmetric(horizontal: 10.0),
                            child: Text(
                          item['name'],
                          style: TextStyle(color: Colors.white, fontSize: 17,)
                        ), ),
                      ],
                    ),
                    value: item['name']
                  );
                }).toList(),
      
                onChanged: (newvalue) {  
                  setState(() {
                    _mySelection = newvalue;
                    print(_mySelection); 
                  }); 
                },
      
                hint:  Text(_myJson[0]["name"],style: TextStyle(color: Colors.white,fontSize: 17)),
      
                value:_mySelection,
                ), // Your Dropdown Code Here,
              ),
      
             actions: <Widget>[
      
               IconButton(
                icon: Icon(Icons.search, color: Colors.white),onPressed:null,
      
          ),]
      
      
        ),
        body: null,
      
      
      );
      

      【讨论】:

        猜你喜欢
        • 2018-07-06
        • 2021-07-10
        • 1970-01-01
        • 2020-05-04
        • 2019-05-07
        • 2021-12-19
        • 1970-01-01
        • 2020-10-10
        • 1970-01-01
        相关资源
        最近更新 更多