Flutter下拉数据选择插件drop_down_data的使用

Flutter下拉数据选择插件drop_down_data的使用

Drop_Down_Data

Pub GitHub stars

Description

下拉列表允许用户进行单选或多选,并且可以在数据中搜索或通过标识符进行选择。

📖 安装

要使用此插件,在您的pubspec.yaml文件中添加drop_down_data依赖项。 以下步骤将帮助您在Flutter项目中添加此库作为依赖项。

  1. 运行 flutter pub add drop_down_data
  2. 或者手动将drop_down_data添加到pubspec.yaml文件中:
dependencies:
  drop_down_data: ^1.0.5
  1. 在您的代码中导入该包:
import 'package:drop_down_data/drop_down_data.dart';

📸 截图

Example Video

👀 使用

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

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

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

class _MyHomePageState extends State<MyHomePage> {
  List<DataDropDown> listData = [];

  [@override](/user/override)
  void initState() {
    super.initState();
    List<Map<String, dynamic>> listMap = [
      {"id": "1", "name": "agenda"},
      {"id": "2", "name": "contact"}
    ];
    listData = List<DataDropDown>.from(listMap.map((model) => DataDropDown.fromJson(model)));
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    ThemeData theme = Theme.of(context);
    TextTheme textTheme = theme.textTheme;
    ColorScheme colorScheme = theme.colorScheme;

    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("Selection", style: textTheme.bodyLarge!.copyWith(color: colorScheme.onSurface),),
            Text("selectionID: $selectionID"),
            Text("selectionValue: $selectionValue"),
            DropDownData(
              tooltip: "show menu",
              title: selectionValue != "" ? " " : "selection",
              value: selectionValue,
              uuidValue: selectionID,
              addFirstEmpty: true,
              viewNumber: true,
              research: false,
              researchCallback: (String val) { setState(() {}); },
              listData: listData,
              selectionCallback: (DataDropDown selection){
                if (kDebugMode) {
                  print(selection);
                  print(selection.id);
                  print(selection.name);
                }
                selectionValue = selection.name ?? "";
                selectionID = selection.id ?? "";
                setState(() { });
              }),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


drop_down_data 是一个用于 Flutter 的下拉数据选择插件,它可以帮助你轻松地创建一个带有数据选择功能的下拉菜单。以下是如何使用 drop_down_data 插件的简单步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  drop_down_data: ^1.0.0  # 请确保使用最新版本

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

2. 导入插件

在你的 Dart 文件中导入 drop_down_data 插件:

import 'package:drop_down_data/drop_down_data.dart';

3. 使用 DropDownData 插件

假设你有一个数据列表,你可以使用 DropDownData 来创建一个下拉选择菜单。

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

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

  final List<String> items = [
    'Item 1',
    'Item 2',
    'Item 3',
    'Item 4',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DropDownData Example'),
      ),
      body: Center(
        child: DropDownData(
          items: items,
          hint: 'Select an item',
          value: selectedValue,
          onChanged: (String? newValue) {
            setState(() {
              selectedValue = newValue;
            });
          },
        ),
      ),
    );
  }
}

4. 参数说明

  • items: 这是一个包含所有可选项的列表。
  • hint: 这是一个提示文本,当没有选择任何项时显示。
  • value: 这是当前选中的值。
  • onChanged: 这是一个回调函数,当用户选择了一个新项时会触发。

5. 运行应用

现在,你可以运行你的 Flutter 应用,并看到一个带有下拉选择菜单的页面。

6. 自定义样式

你可以通过传递更多的参数来自定义下拉菜单的样式,例如 dropdownColoriconiconSize 等。

DropDownData(
  items: items,
  hint: 'Select an item',
  value: selectedValue,
  onChanged: (String? newValue) {
    setState(() {
      selectedValue = newValue;
    });
  },
  dropdownColor: Colors.blue[100],
  icon: Icon(Icons.arrow_drop_down),
  iconSize: 24.0,
),

7. 处理复杂数据

如果你的数据是复杂对象,你可以使用 map 函数将对象转换为字符串列表,并在 onChanged 回调中处理选中的对象。

class MyItem {
  final String name;
  final int id;

  MyItem(this.name, this.id);
}

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

class _MyHomePageState extends State<MyHomePage> {
  MyItem? selectedItem;

  final List<MyItem> items = [
    MyItem('Item 1', 1),
    MyItem('Item 2', 2),
    MyItem('Item 3', 3),
    MyItem('Item 4', 4),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DropDownData Example'),
      ),
      body: Center(
        child: DropDownData(
          items: items.map((MyItem item) => item.name).toList(),
          hint: 'Select an item',
          value: selectedItem?.name,
          onChanged: (String? newValue) {
            setState(() {
              selectedItem = items.firstWhere((item) => item.name == newValue);
            });
          },
        ),
      ),
    );
  }
}

8. 处理空值

你可以通过设置 valuenull 来清除当前选中的值。

setState(() {
  selectedValue = null;
});
回到顶部