Flutter下拉菜单DropdownButton
2019-07-22 本文已影响1人
倪大头
class BusinessPage extends StatefulWidget {
@override
_BusinessPageState createState() => _BusinessPageState();
}
class _BusinessPageState extends State<BusinessPage> {
List<DropdownMenuItem<String>> sortItems = [];
String _selectedSort = '排序';
sortItems.add(DropdownMenuItem(value: '排序', child: Text('排序')));
sortItems.add(DropdownMenuItem(value: '价格降序', child: Text('价格降序')));
sortItems.add(DropdownMenuItem(value: '价格升序', child: Text('价格升序')));
@override
Widget build(BuildContext context) {
return Scaffold(body: getList());
}
getList() {
return DropdownButton(
value: _selectedSort,
items: sortItems,
onChanged: changedSort,
);
}
DropdownButton的 value 参数一定要是包含在 items 参数中的选项,否则会报如下错误:
flutter: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 608 pos 15: 'items == null ||
flutter: items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value ==
flutter: value).length == 1': is not true.
image.png
不想要下划线就再套一层:
child: Container(
alignment: Alignment.center,
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: _selectedSort,
items: sortItems,
onChanged: changedSort,
),
),
),