[英]Drop down problem after onChanged in Flutter/dart
我正在下拉。 在菜單項中,我有一行,其中在左側顯示圖標,在右側顯示文本。
問題:
當我從下拉菜單中選擇項目時,它同時顯示圖標和文本,但是我只需要顯示文本。在調試中,我打印出值,它僅顯示正確的文本。
注意:如果我從行中刪除圖標,則它工作正常。
調試控制台
菜單項
選擇項目后,圖標和文本均顯示,但我只想顯示文本
下面是代碼:
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),
嘗試這個
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.