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

时间:2019-02-12 19:36:24

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

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

问题:

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

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

调试控制台

菜单项

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

下面是代码:

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

尝试一下

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,


);