简体   繁体   English

如何获取下拉选择的菜单项文本?

[英]How to get the dropdown selected menu item text?

I'm able to print the index value of the seleted dropdown menu item, but I need the text to be printed of the selected dropdown menu.我可以打印所选下拉菜单项的索引值,但我需要打印所选下拉菜单的文本。

This is my dropdown这是我的下拉菜单

String dro = ''; int _articleStatusValue = 1;

DropdownButton(
     value: _articleStatusValue,
     items: [
        DropdownMenuItem(
          child: Text('Delivered',style: TextStyle(color: Colors.black54),
           ),
           value: 1,
         ),
         DropdownMenuItem(
           child: Text('Not Delivered',style: TextStyle(color: Colors.black54),),
            value: 2,
           ),
         ],
         onChanged: (value) {
              setState(() {
                  _articleStatusValue = value;
                  dro = _articleStatusValue.toString();
                  print(dro);
             });
          },
),

But if i print, I'm getting the index valueof the selected但是如果我打印,我会得到所选的索引值

try this one:试试这个:

String dro = ''; int _articleStatusValue = 1;

DropdownButton(
     value: _articleStatusValue,
     items: [
        DropdownMenuItem(
          child: Text('Delivered',style: TextStyle(color: Colors.black54),
           ),
           value: 'Delivered',
         ),
         DropdownMenuItem(
           child: Text('Not Delivered',style: TextStyle(color: Colors.black54),),
            value: 'Not Delivered',
           ),
         ],
         onChanged: (value) {
              setState(() {
                  _articleStatusValue = value;
                  dro = _articleStatusValue.toString();
                  print(dro);
             });
          },
),

if you want to have index try this one:如果你想有索引试试这个:

String dro = ''; int _articleStatusValue = 1;

DropdownButton(
     value: _articleStatusValue,
     items: [
        DropdownMenuItem(
          child: Text('Delivered',style: TextStyle(color: Colors.black54),
           ),
           value: 1,
         ),
         DropdownMenuItem(
           child: Text('Not Delivered',style: TextStyle(color: Colors.black54),),
            value: 2,
           ),
         ],
         onChanged: (value) {
                 if(value == 1){
                    setState(() {
                      articleStatusValue = 'Delivered';
                      dro = _articleStatusValue.toString();
                      print(dro);
                   });
                 } else  if(value == 2){
                   setState(() {
                      articleStatusValue = 'Not Delivered';
                      dro = _articleStatusValue.toString();
                      print(dro);
                   });
                 }
          },
),

Thanks to @Mohammad_Asef I got the dropdown successfully as asked but did some corrections to his answer感谢@Mohammad_Asef,我按要求成功获得了下拉菜单,但对他的回答做了一些更正

String deliveredStatusText = ''; int _articleStatusValue = 1; 

DropdownButton(
      value: _articleStatusValue,
      items: [
        DropdownMenuItem(
          child: Text('Delivered',style: TextStyle(color: Colors.black54),),
          value: 1,),
        DropdownMenuItem(
          child: Text('Not Delivered',style: TextStyle(color: Colors.black54),),
          value: 2,),],
      onChanged: (value) {
        if(value == 1){
          setState(() {
            _articleStatusValue = 1;
            deliveredStatusText = 'Delivered';
            print(deliveredStatusText);
          });
        } else  if(value == 2){
             setState(() {
                _articleStatusValue = 2;
                deliveredStatusText = 'Not Delivered';
                print(deliveredStatusText);
              });
           }
       },
),

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

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