Flutter下拉选择插件dropdown_select的使用

Flutter下拉选择插件dropdown_select的使用

dropdown_select 是一个带有 Material Design 样式的 Flutter 下拉选择小部件,支持单选或多选选项,并且支持工具提示以增强可用性。该插件是 dropdown_textfield 的分支版本,增加了许多社区所需的特性。

功能特点

  • 可搜索的下拉菜单
  • 单选与多选支持
  • Material Design 风格
  • 完全可定制的用户界面
  • 多选下拉项的工具提示

使用方法

导入插件

首先,在项目的 pubspec.yaml 文件中添加依赖:

dependencies:
  dropdown_select: ^版本号

然后运行以下命令安装依赖:

flutter pub get

示例代码

以下是一个完整的示例代码,展示如何在 Flutter 应用程序中使用 dropdown_select 插件。

import 'package:flutter/material.dart';
import 'package:dropdown_select/dropdown_select.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DropdownSelectExample(),
    );
  }
}

class DropdownSelectExample extends StatelessWidget {
  const DropdownSelectExample({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Dropdown Select Example'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 单选下拉框
            DropdownSelect(
              clearOption: true, // 是否显示清除按钮
              enableSearch: true, // 是否启用搜索功能
              clearIconProperty: IconProperty(color: Colors.green), // 清除按钮图标颜色
              searchTextStyle: const TextStyle(color: Colors.red), // 搜索框文本样式
              searchDecoration: const InputDecoration(
                hintText: "请输入自定义提示文字", // 搜索框提示文字
              ),
              validator: (value) {
                if (value == null) {
                  return "必填字段"; // 验证错误信息
                } else {
                  return null;
                }
              },
              dropDownItemCount: 6, // 下拉菜单最大显示项目数
              dropDownList: const [
                DropDownValueModel(name: 'name1', value: "value1"), // 下拉选项
                DropDownValueModel(
                    name: 'name2',
                    value: "value2",
                    toolTipMsg: "这是一个下拉按钮,我们可以从中选择唯一的值"), // 带有工具提示的选项
                DropDownValueModel(name: 'name3', value: "value3"),
                DropDownValueModel(
                    name: 'name4',
                    value: "value4",
                    toolTipMsg: "这是一个下拉按钮,我们可以从中选择唯一的值"),
                DropDownValueModel(name: 'name5', value: "value5"),
                DropDownValueModel(name: 'name6', value: "value6"),
                DropDownValueModel(name: 'name7', value: "value7"),
                DropDownValueModel(name: 'name8', value: "value8"),
              ],
              onChanged: (val) {
                print("Selected Value: $val"); // 选择事件回调
              },
            ),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


dropdown_select 是一个用于 Flutter 的下拉选择插件,它允许用户从下拉列表中选择一个选项。这个插件通常用于表单中,以便用户可以从预定义的选项中进行选择。

安装

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

dependencies:
  flutter:
    sdk: flutter
  dropdown_select: ^1.0.0  # 请使用最新版本

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

基本用法

以下是一个简单的示例,展示了如何使用 dropdown_select 插件:

import 'package:flutter/material.dart';
import 'package:dropdown_select/dropdown_select.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Dropdown Select Example'),
        ),
        body: Center(
          child: DropdownSelectExample(),
        ),
      ),
    );
  }
}

class DropdownSelectExample extends StatefulWidget {
  @override
  _DropdownSelectExampleState createState() => _DropdownSelectExampleState();
}

class _DropdownSelectExampleState extends State<DropdownSelectExample> {
  String? _selectedValue;

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

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: DropdownSelect<String>(
        items: _items,
        selectedItem: _selectedValue,
        onChanged: (value) {
          setState(() {
            _selectedValue = value;
          });
        },
        hint: 'Select an option',
      ),
    );
  }
}

参数说明

  • items: 这是一个包含所有选项的列表。列表中的每个元素都会显示在下拉菜单中。
  • selectedItem: 当前选中的值。如果用户没有选择任何值,可以为 null
  • onChanged: 当用户选择一个选项时,会调用这个回调函数。通常在这个回调中更新 selectedItem 的值。
  • hint: 当下拉菜单没有选中任何值时显示的提示文本。

自定义样式

你可以通过传递 style 参数来自定义下拉菜单的样式。例如:

DropdownSelect<String>(
  items: _items,
  selectedItem: _selectedValue,
  onChanged: (value) {
    setState(() {
      _selectedValue = value;
    });
  },
  hint: 'Select an option',
  style: TextStyle(
    color: Colors.blue,
    fontSize: 16,
  ),
),
回到顶部