问题 如何在flutter中实现下拉列表?
我有一个我希望在Flutter中作为下拉列表实现的位置列表。我对这门语言很陌生。这就是我所做的。
new DropdownButton(
value: _selectedLocation,
onChanged: (String newValue) {
setState(() {
_selectedLocation = newValue;
});
},
items: _locations.map((String location) {
return new DropdownMenuItem<String>(
child: new Text(location),
);
}).toList(),
这是我的项目清单:
List<String> _locations = ['A', 'B', 'C', 'D'];
我收到以下错误。
Another exception was thrown: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 468 pos 15: 'value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.
我假设的价值 _selectedLocation
变得空了。但我正在初始化它。
String _selectedLocation = 'Please choose a location';
5166
2018-03-14 08:46
起源
答案:
尝试这个
new DropdownButton<String>(
items: <String>['A', 'B', 'C', 'D'].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (_) {},
)
9
2018-03-14 10:02
答案:
尝试这个
new DropdownButton<String>(
items: <String>['A', 'B', 'C', 'D'].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (_) {},
)
9
2018-03-14 10:02
你需要添加 value: location
在你的代码中工作。检查 这个 出。
items: _locations.map((String location) {
return new DropdownMenuItem<String>(
child: new Text(location),
value: location,
);
}).toList(),
4
2018-03-14 10:10
将值放在items.then它将工作,
new DropdownButton<String>(
items:_dropitems.map((String val){
return DropdownMenuItem<String>(
value: val,
child: new Text(val),
);
}).toList(),
hint:Text(_SelectdType),
onChanged:(String val){
_SelectdType= val;
setState(() {});
})
1
2018-06-29 08:41
你必须考虑到这一点(来自DropdownButton docs):
“项目必须具有不同的值,如果值不为空,则必须
在他们中间。“
所以基本上你有这个字符串列表
List<String> _locations = ['A', 'B', 'C', 'D'];
Dropdown值属性中的值初始化如下:
String _selectedLocation = 'Please choose a location';
试试这个清单:
List<String> _locations = ['Please choose a location', 'A', 'B', 'C', 'D'];
那应该工作:)
如果您不想添加类似的字符串(在列表上下文中),请查看“提示”属性,您可以使用以下内容:
DropdownButton<int>(
items: locations.map((String val) {
return new DropdownMenuItem<String>(
value: val,
child: new Text(val),
);
}).toList(),
hint: Text("Please choose a location"),
onChanged: (newVal) {
_selectedLocation = newVal;
this.setState(() {});
});
0
2018-04-21 22:45
当我试图在下拉列表中显示动态字符串列表时,我遇到了与DropDownButton类似的问题。我最终创建了一个插件: flutter_search_panel。不是下拉插件,但您可以使用搜索功能显示项目。
使用以下代码来使用窗口小部件:
FlutterSearchPanel(
padding: EdgeInsets.all(10.0),
selected: 'a',
title: 'Demo Search Page',
data: ['This', 'is', 'a', 'test', 'array'],
icon: new Icon(Icons.label, color: Colors.black),
color: Colors.white,
textStyle: new TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20.0, decorationStyle: TextDecorationStyle.dotted),
onChanged: (value) {
print(value);
},
),
0
2017-08-30 13:39