Flutter下拉搜索列表插件dropdown_search_list的使用

Flutter 下拉搜索列表插件 dropdown_search_list 的使用

特性

创建一个易于管理的可滚动搜索列表。该插件允许用户通过关键字字符串进行搜索,并使用模糊搜索来查找匹配项并显示结果。

使用方法

  1. pubspec.yaml 文件中添加依赖:
dependencies:
  dropdown_search_list: ^0.0.1

示例

lib/main.dart
import 'package:dropdown_searchable_list/home.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    BoxDecoration decoration = BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        border: Border.all(
          width: 4,
          color: Colors.black,
          style: BorderStyle.solid,
        ));

    List<String> items = [
      'Bicycling, Competitive',
      'Bicycling Uphill',
      'Bicycle Race',
      'Mountain Biking',
    ];

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(
        items: items,
        dropdownListButtonHeight: 50,
        dropdownListButtonWidth: 400,
        dropdownListButtonDecoration: decoration,
        dropdownListButtonInitialText: 'Select',
      ),
    );
  }
}
lib/list_state.dart
import 'package:flutter/cupertino.dart';

class ListState extends ChangeNotifier {
  late bool open;
  late String selection;
  late List<String> items;

  bool get getOpen {
    return open;
  }

  set setOpen(bool open) {
    this.open = open;
    notifyListeners();
  }

  String get getSelection {
    return selection;
  }

  set setSelection(String selection) {
    this.selection = selection;
    notifyListeners();
  }

  List<String> get getItems {
    return items;
  }

  set setItems(List<String> items) {
    this.items = items;
    notifyListeners();
  }

  set addItems(String item) {
    items.add(item);
    notifyListeners();
  }

  ListState(this.open, this.selection, this.items);
}
lib/home.dart
import 'package:dropdown_search_list/dropdown_search_list.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  final List<String> items;
  final double? dropdownListButtonHeight;
  final double? dropdownListButtonWidth;
  final BoxDecoration? dropdownListButtonDecoration;
  final Icon dropdownListButtonIcon;
  final String dropdownListButtonInitialText;

  const HomePage({
    Key? key,
    required this.items,
    this.dropdownListButtonHeight,
    this.dropdownListButtonWidth,
    this.dropdownListButtonDecoration,
    this.dropdownListButtonIcon = const Icon(Icons.arrow_downward),
    required this.dropdownListButtonInitialText,
  }) : super(key: key);

  [@override](/user/override)
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String selectedData = "Nothing selected";

  void updateText(String value) {
    setState(() {
      selectedData = value;
      print("Updating text data " + selectedData);
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Text(selectedData),
            Container(
              width: 300,
              height: 50,
              child: DropdownSearchList(
                items: widget.items,
                dropdownListButtonInitialText: "Please select any option",
                dropdownListButtonDecoration: widget.dropdownListButtonDecoration,
                dropdownListButtonWidth: widget.dropdownListButtonWidth,
                dropdownListButtonHeight: widget.dropdownListButtonHeight,
                dropdownListButtonIcon: Icon(Icons.arrow_drop_down),
                dropdownListWidth: 500,
                dropdownListShadowColor: Colors.grey,
                dropdownListColor: Colors.grey.shade200,
                onSelect: (value) => updateText(value),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


dropdown_search_list 是一个用于 Flutter 的下拉搜索列表插件,它允许用户从下拉列表中选择一个项目,并且支持搜索功能。这个插件非常适合在需要从大量选项中选择一个项目时使用。

安装

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

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

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

基本用法

以下是一个简单的示例,展示如何使用 dropdown_search_list

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Dropdown Search List Example'),
        ),
        body: Center(
          child: DropdownSearchList<String>(
            items: ['Apple', 'Banana', 'Orange', 'Mango', 'Grapes'],
            onChanged: (value) {
              print('Selected: $value');
            },
            hint: 'Select a fruit',
          ),
        ),
      ),
    );
  }
}

参数说明

  • items: 这是一个 List<T>,表示下拉列表中的选项。每个选项可以是任何类型,但通常使用 String 或自定义对象。

  • onChanged: 这是一个回调函数,当用户选择一个项目时触发。它接收选中的项目作为参数。

  • hint: 这是一个 String,用于在下拉列表未选中任何项目时显示的提示文本。

  • label: 这是一个 String,用于在下拉列表上方显示的标签文本。

  • searchHint: 这是一个 String,用于在搜索框中显示的提示文本。

  • showSearchBox: 这是一个 bool,用于控制是否显示搜索框。默认值为 true

  • dropdownBuilder: 这是一个 Widget Function(BuildContext context, T item),用于自定义下拉列表中每个项目的显示方式。

  • selectedItem: 这是一个 T,用于设置默认选中的项目。

自定义选项

如果你使用的是自定义对象而不是简单的字符串,你可以通过 dropdownBuilder 参数来自定义每个选项的显示方式。例如:

class Fruit {
  final String name;
  final String color;

  Fruit(this.name, this.color);
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Dropdown Search List Example'),
        ),
        body: Center(
          child: DropdownSearchList<Fruit>(
            items: [
              Fruit('Apple', 'Red'),
              Fruit('Banana', 'Yellow'),
              Fruit('Orange', 'Orange'),
              Fruit('Mango', 'Yellow'),
              Fruit('Grapes', 'Purple'),
            ],
            onChanged: (value) {
              print('Selected: ${value.name}');
            },
            hint: 'Select a fruit',
            dropdownBuilder: (context, item) {
              return ListTile(
                title: Text(item.name),
                subtitle: Text(item.color),
              );
            },
          ),
        ),
      ),
    );
  }
}
回到顶部