简体   繁体   English

Flutter / dart中的onChanged后出现下拉问题

[英]Drop down problem after onChanged in Flutter/dart

I am working on drop down. 我正在下拉。 In menu item I have row in which I display icon on left side and on right side I show text. 在菜单项中,我有一行,其中在左侧显示图标,在右侧显示文本。

Problem: 问题:

When I select item from drop down it show icon and text both but I need to show the text only.In debug I print out value it display only text which is correct. 当我从下拉菜单中选择项目时,它同时显示图标和文本,但是我只需要显示文本。在调试中,我打印出值,它仅显示正确的文本。

Note: If I remove icon from row then it working fine. 注意:如果我从行中删除图标,则它工作正常。

Debug Console 调试控制台

Menu Items 菜单项

After select item both Icon and Text are display but I want to show text only 选择项目后,图标和文本均显示,但我只想显示文本

Below is Code: 下面是代码:

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

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

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

Try this 尝试这个

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,


);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM