Flutter下拉搜索插件magic_dropdown_search的使用

Flutter 下拉搜索插件 magic_dropdown_search 的使用

Magic Dropdown Search 是一个 Flutter 包,它提供了从 API 动态获取数据的下拉搜索功能。

描述

该包允许你创建一个带选择功能的下拉搜索。它设计为易于使用且可自定义以满足你的需求。该包由 Dart 构建,并使用 Flutter 框架。

安装

要使用此包,将其作为依赖项添加到你的 pubspec.yaml 文件中:

dependencies:
  magic_dropdown_search: ^0.0.1

使用

以下是一个基本示例,展示如何使用 magic_dropdown_search 包:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Magic Dropdown Search Example'),
        ),
        body: Center(
          child: MagicDropdownSearch(
            label: 'Search',
            onChanged: (value) {
              print('Selected value: $value');
            },
            onChangedSearch: (searchTerm) async {
              // 替换为你自己的搜索逻辑
              return ['Item1', 'Item2', 'Item3'];
            },
            dropdownItems: ['Item1', 'Item2', 'Item3'],
            hint: 'Select an item',
            hintSearch: 'Search items',
            initValue: 'Item1',
            buttonWidth: 200,
            itemHeight: 50,
            dropdownHeight: 300,
            buttonHeight: 50,
            suffixIcon: Icon(Icons.search),
          ),
        ),
      ),
    );
  }
}

特性

  • 动态数据
  • 下拉框内的搜索功能
  • 从 API 加载数据
  • 必要的自定义选项

示例代码

以下是一个更详细的示例代码,展示了如何使用 MagicDropdownSearch 组件:

import 'package:flutter/material.dart';
import 'package:magic_dropdown_search/magic_dropdown_search.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: Scaffold(
        appBar: AppBar(
          title: const Text('Magic Dropdown Search Example'),
          backgroundColor: Colors.blue,
        ),
        body: Center(
          child: Column(
            children: [
              MagicDropdownSearch(
                // label: 'Search',
                onChanged: (value) {
                  print('Selected value: $value');
                },
                onChangedSearch: (searchTerm) async {
                  // 替换为你自己的搜索逻辑
                  return ['Item1', 'Item2', 'Item3'];
                },
                dropdownItems: const ['Item1', 'Item2', 'Item3'],
                // hint: 'Select an item',
                initValue: 'Item1',
                // buttonWidth: 200,
                itemHeight: 50,
                dropdownHeight: 200,
                // buttonHeight: 50,
                buttonDecoration: const InputDecoration(
                  hintText: 'Select an item',
                  labelText: 'Search',
                  prefixIcon: Icon(Icons.search),
                  labelStyle: TextStyle(color: Colors.blue),
                  errorStyle: TextStyle(color: Colors.red),
                  hintStyle: TextStyle(color: Colors.grey),
                ),
                dropdownBoxDecoration: DropDownBoxDecoration(
                  color: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10),
                    side: BorderSide(
                      color: Colors.blue,
                      width: 1,
                    ),
                  ),
                ),
              ),
              Spacer(),
              MagicDropdownSearch(
                // label: 'Search',
                onChanged: (value) {
                  print('Selected value: $value');
                },
                onChangedSearch: (searchTerm) async {
                  // 替换为你自己的搜索逻辑
                  return ['Item1 sssssssssssss', 'Item2', 'Item3'];
                },
                dropdownItems: const ['Item1 sssssssssssss', 'Item2', 'Item3'],
                // hint: 'Select an item',
                // buttonWidth: 200,
                itemHeight: 50,
                dropdownHeight: 200,
                // buttonHeight: 50,
                searchDecoration: const InputDecoration(
                  hintText: 'Search',
                  labelText: 'Search',
                  floatingLabelBehavior: FloatingLabelBehavior.never,
                  prefixIcon: Icon(Icons.search),
                  labelStyle: TextStyle(color: Colors.blue),
                  errorStyle: TextStyle(color: Colors.red),
                  hintStyle: TextStyle(color: Colors.grey),
                ),
                itemBuilder: (item, isSelected) {
                  return Container(
                    alignment: Alignment.centerLeft,
                    padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 5),
                    decoration: BoxDecoration(
                      color: isSelected ? Colors.red.withOpacity(0.1) : Colors.transparent,
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: Row(
                      children: [
                        if (isSelected) ...[
                          const Icon(
                            Icons.check,
                            size: 16,
                            color: Color(0xff111111),
                          ),
                          const SizedBox(width: 7.5),
                        ],
                        Expanded(
                          child: Text(
                            item,
                            maxLines: 3,
                            style: Theme.of(context).textTheme.labelMedium!.copyWith(
                                color: const Color(0xff111111), fontWeight: FontWeight.w400),
                          ),
                        ),
                      ],
                    ),
                  );
                },
                buttonDecoration: const InputDecoration(
                  hintText: 'Select an item',
                  labelText: 'Search',
                  prefixIcon: Icon(Icons.search),
                  labelStyle: TextStyle(color: Colors.blue),
                  errorStyle: TextStyle(color: Colors.red),
                  hintStyle: TextStyle(color: Colors.grey),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


当然,下面是一个关于如何在Flutter项目中使用magic_dropdown_search插件来实现下拉搜索功能的代码示例。magic_dropdown_search是一个功能强大的Flutter插件,它允许用户从下拉列表中选择项目,并支持搜索功能。

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

dependencies:
  flutter:
    sdk: flutter
  magic_dropdown_search: ^latest_version  # 请替换为最新的版本号

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

接下来,在你的Dart文件中,你可以按照以下方式使用MagicDropdownSearch

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

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

class _MyHomePageState extends State<MyHomePage> {
  // 示例数据
  final List<String> items = List<String>.generate(100, (i) => "Item $i");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Magic Dropdown Search Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            MagicDropdownSearch<String>(
              label: 'Select an item',
              items: items,
              normalHintText: 'Search...',
              onChanged: (newValue) {
                print('Selected: $newValue');
              },
              searchHintText: 'Search...',
              clearable: true,
              dropdownSearchDecoration: InputDecoration(
                prefixIcon: Icon(Icons.search),
                border: OutlineInputBorder(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中:

  1. 依赖导入:我们导入了magic_dropdown_search包。
  2. 数据准备:我们创建了一个包含100个字符串项的列表items
  3. UI构建:在MyHomePagebuild方法中,我们使用MagicDropdownSearch小部件来创建一个下拉搜索组件。
    • label属性用于显示组件的标签。
    • items属性接受一个字符串列表,这些字符串将显示在下拉列表中。
    • normalHintText属性设置搜索框在非搜索状态下的提示文本。
    • onChanged回调函数在用户选择一个项目时被调用,这里我们简单地打印出选中的值。
    • searchHintText属性设置搜索框在搜索状态下的提示文本。
    • clearable属性允许用户清除选中的值。
    • dropdownSearchDecoration属性允许我们自定义输入框的样式,包括前缀图标和边框。

这个示例展示了如何使用magic_dropdown_search插件来创建一个基本的下拉搜索功能。你可以根据需要进一步自定义和扩展这个组件。

回到顶部