美文网首页Flutter
Flutter下拉菜单DropdownButton

Flutter下拉菜单DropdownButton

作者: 倪大头 | 来源:发表于2019-07-22 09:38 被阅读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,
        ),
    ),
),

相关文章

网友评论

    本文标题:Flutter下拉菜单DropdownButton

    本文链接:https://www.haomeiwen.com/subject/qkqilctx.html