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 參數(shù)一定要是包含在 items 參數(shù)中的選項(xiàng),否則會報(bào)如下錯誤:
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
如果需要一個未選擇狀態(tài)的默認(rèn)提示,就用hint屬性,同時把value屬性設(shè)為null:
hint: Text(
'請選擇',
style: TextStyle(
fontSize: 28.sp,
color: Utils.hexColor('999999'),
),
),
不想要下劃線就再套一層DropdownButtonHideUnderline:
child: Container(
alignment: Alignment.center,
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: _selectedSort,
items: sortItems,
onChanged: changedSort,
),
),
),
或者把underline屬性設(shè)置為一個空Container:
underline: Container(),
設(shè)為null是不行的