Flutter下拉选择插件dropdown_plus_plus的使用

Flutter下拉选择插件dropdown_plus_plus的使用

dropdown_plus_plus 是一个简单且易于使用的表单下拉组件,支持搜索、键盘导航、离线数据源、远程数据源以及自定义。

它是一个维护良好的 dropdown_plus 的分支版本,具有更多自定义选项。

开始使用

简单文本下拉选择

TextDropdownFormField(
  options: ["Male", "Female"],
  decoration: InputDecoration(
      border: OutlineInputBorder(),
      suffixIcon: Icon(Icons.arrow_drop_down),
      labelText: "Gender"),
  cursorColor: Colors.green,
  dropdownItemColor: Colors.red,
),

安装

pubspec.yaml 文件中添加以下依赖:

dropdown_plus_plus: <latest_version>

可定制的例子

final List<Map<String, dynamic>> _roles = [
  {"name": "Super Admin", "desc": "Having full access rights", "role": 1},
  {
    "name": "Admin",
    "desc": "Having full access rights of a Organization",
    "role": 2
  },
  {
    "name": "Manager",
    "desc": "Having Magenent access rights of a Organization",
    "role": 3
  },
  {
    "name": "Technician",
    "desc": "Having Technician Support access rights",
    "role": 4
  },
  {
    "name": "Customer Support",
    "desc": "Having Customer Support access rights",
    "role": 5
  },
  {"name": "User", "desc": "Having End User access rights", "role": 6},
];

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final List<Map<String, dynamic>> _roles = [
    {"name": "Super Admin", "desc": "Having full access rights", "role": 1},
    {
      "name": "Admin",
      "desc": "Having full access rights of a Organization",
      "role": 2
    },
    {
      "name": "Manager",
      "desc": "Having Magenent access rights of a Organization",
      "role": 3
    },
    {
      "name": "Technician",
      "desc": "Having Technician Support access rights",
      "role": 4
    },
    {
      "name": "Customer Support",
      "desc": "Having Customer Support access rights",
      "role": 5
    },
    {"name": "User", "desc": "Having End User access rights", "role": 6},
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Dropdown Plus Plus Demo'),
      ),
      body: Container(
        padding: EdgeInsets.all(16),
        child: Column(
          children: [
            TextDropdownFormField(
              options: ["Male", "Female"],
              decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  suffixIcon: Icon(Icons.arrow_drop_down),
                  labelText: "Gender"),
              cursorColor: Colors.green,
              dropdownItemColor: Colors.red,
            ),
            SizedBox(
              height: 16,
            ),
            DropdownFormField<Map<String, dynamic>>(
              onEmptyActionPressed: (String str) async {},
              dropdownItemSeparator: Divider(
                color: Colors.black,
                height: 1,
              ),
              decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  suffixIcon: Icon(Icons.arrow_drop_down),
                  labelText: "Access"),
              onSaved: (dynamic str) {},
              onChanged: (dynamic str) {},
              validator: null,
              displayItemFn: (dynamic item) => Text(
                (item ?? {})['name'] ?? '',
                style: TextStyle(fontSize: 16),
              ),
              findFn: (dynamic str) async => _roles,
              selectedFn: (dynamic item1, dynamic item2) {
                if (item1 != null && item2 != null) {
                  return item1['name'] == item2['name'];
                }
                return false;
              },
              filterFn: (dynamic item, str) =>
                  item['name'].toLowerCase().indexOf(str.toLowerCase()) >= 0,
              dropdownItemFn: (dynamic item, int position, bool focused,
                      bool selected, Function() onTap) =>
                  ListTile(
                title: Text(item['name']),
                subtitle: Text(
                  item['desc'] ?? '',
                ),
                tileColor:
                    focused ? Color.fromARGB(20, 0, 0, 0) : Colors.transparent,
                onTap: onTap,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

配置选项

final bool autoFocus;
final bool Function(dynamic item, String str)? filterFn;
final bool Function(dynamic item1, dynamic item2)? selectedFn;
final Future<List<dynamic>> Function(String str) findFn;

final ListTile Function(
  dynamic item,
  int position,
  bool focused,
  bool selected,
  Function() onTap,
) dropdownItemFn;

final Widget Function(dynamic item) displayItemFn;
final InputDecoration? decoration;
final Color? dropdownColor;
final Color? cursorColor;
final DropdownEditingController<T>? controller;
final void Function(dynamic item)? onChanged;
final void Function(dynamic)? onSaved;
final String? Function(T?)? validator;
final double? dropdownHeight;
final TextStyle? searchTextStyle;
final String emptyText;
final String emptyActionText;
final Future<void> Function(String)? onEmptyActionPressed;
final Widget? dropdownItemSeparator;

更多关于Flutter下拉选择插件dropdown_plus_plus的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter下拉选择插件dropdown_plus_plus的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


dropdown_plus_plus 是一个用于 Flutter 的下拉选择插件,它提供了丰富的自定义选项,帮助你创建漂亮且功能强大的下拉选择框。以下是使用 dropdown_plus_plus 的基本步骤和示例。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 dropdown_plus_plus 依赖:

dependencies:
  flutter:
    sdk: flutter
  dropdown_plus_plus: ^1.0.0  # 请查看最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入库

在使用 dropdown_plus_plus 的地方导入库:

import 'package:dropdown_plus_plus/dropdown_plus_plus.dart';

3. 基本使用

以下是一个简单的使用示例:

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String? _selectedValue;

  final List<String> _items = [
    'Option 1',
    'Option 2',
    'Option 3',
    'Option 4',
  ];

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Dropdown++ Example'),
      ),
      body: Center(
        child: DropdownPlusPlus(
          items: _items,
          onChanged: (value) {
            setState(() {
              _selectedValue = value;
            });
          },
          selectedValue: _selectedValue,
          hint: 'Select an option',
        ),
      ),
    );
  }
}

4. 自定义选项

dropdown_plus_plus 提供了多种自定义选项,以下是一些常见的自定义配置:

  • hint: 未选择任何选项时显示的提示文本。
  • selectedValue: 当前选中的值。
  • onChanged: 当用户选择某个选项时触发的回调函数。
  • items: 下拉菜单中的选项列表。
  • dropdownWidth: 下拉菜单的宽度。
  • dropdownHeight: 下拉菜单的最大高度。
  • borderRadius: 下拉菜单的圆角半径。
  • elevation: 下拉菜单的阴影。
  • itemHeight: 每个选项的高度。
  • itemPadding: 每个选项的内边距。
  • icon: 下拉菜单右侧的图标。
  • iconSize: 下拉菜单右侧图标的大小。
  • iconColor: 下拉菜单右侧图标的颜色。

5. 完整示例

以下是一个包含更多自定义选项的完整示例:

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String? _selectedValue;

  final List<String> _items = [
    'Option 1',
    'Option 2',
    'Option 3',
    'Option 4',
  ];

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Dropdown++ Example'),
      ),
      body: Center(
        child: DropdownPlusPlus(
          items: _items,
          onChanged: (value) {
            setState(() {
              _selectedValue = value;
            });
          },
          selectedValue: _selectedValue,
          hint: 'Select an option',
          dropdownWidth: 200,
          dropdownHeight: 200,
          borderRadius: 8,
          elevation: 5,
          itemHeight: 50,
          itemPadding: EdgeInsets.symmetric(horizontal: 16),
          icon: Icons.arrow_drop_down,
          iconSize: 24,
          iconColor: Colors.blue,
        ),
      ),
    );
  }
}
回到顶部