Flutter自定义下拉选择插件customizable_dropdown的使用

Flutter 自定义下拉选择插件 customizable_dropdown 的使用

pub package

customizable_dropdown 是一个允许你自定义下拉列表项的下拉组件。

特性

  • 可以向下拉列表添加项目。
  • 支持可扩展的下拉列表。
  • 所有来自 ListView.separated 构造函数的参数都可用。

开始使用

在你的 pubspec.yaml 文件中添加包:

dependencies:
  customizable_dropdown: ^0.0.1

在 Dart 文件中导入库:

import 'package:customizable_dropdown/customizable_dropdown.dart';

CustomDropdown 替代 DropdownButton

结果如下:

In action

示例代码

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

void main() {
  runApp(const MaterialApp(home: MyApp()));
}

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

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<String> dropdownList = [
    'Walmart',
    'Amazon',
    'Apple Inc.',
    'CVS Health',
    'ExxonMobil',
    'UnitedHealth Group',
    'Berkshire Hathaway',
    'McKesson Corporation'
  ];

  [@override](/user/override)
  void initState() {
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('DropDown example app'),
      ),
      body: SizedBox(
        child: Padding(
          padding: const EdgeInsets.only(top: 50),
          child: Align(
            alignment: Alignment.topCenter,
            child: SizedBox(
              width: MediaQuery.of(context).size.width * 0.8,
              child: CustomizableDropdown(
                icon: const Icon(
                  Icons.keyboard_arrow_right,
                  color: Colors.black,
                ),
                titleAlign: TextAlign.center,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: const BorderRadius.all(Radius.circular(10)),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.5),
                      spreadRadius: 0,
                      blurRadius: 5,
                      offset: const Offset(1, 1), // 改变阴影的位置
                    ),
                  ],
                ),
                itemList: dropdownList,
                onSelectedItem: (sele) {},
                placeholder: const Text("List of largest companies"),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


当然,以下是一个关于如何使用Flutter中的customizable_dropdown插件的示例代码。这个插件允许你创建一个高度可定制的下拉选择菜单。

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

dependencies:
  flutter:
    sdk: flutter
  customizable_dropdown: ^x.y.z  # 请将x.y.z替换为最新版本号

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

接下来,你可以在你的Dart文件中使用CustomizableDropdown组件。以下是一个简单的示例:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Customizable Dropdown Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Customizable Dropdown Example'),
        ),
        body: Center(
          child: MyCustomDropdown(),
        ),
      ),
    );
  }
}

class MyCustomDropdown extends StatefulWidget {
  @override
  _MyCustomDropdownState createState() => _MyCustomDropdownState();
}

class _MyCustomDropdownState extends State<MyCustomDropdown> {
  String? selectedValue;

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

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: CustomizableDropdown(
        value: selectedValue,
        hint: Text('Select an item'),
        items: dropdownItems.map<DropdownMenuItem<String>>((String value) {
          return DropdownMenuItem<String>(
            value: value,
            child: Text(value),
          );
        }).toList(),
        onChanged: (newValue) {
          setState(() {
            selectedValue = newValue;
          });
        },
        dropdownDecoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.5),
              spreadRadius: 5,
              blurRadius: 7,
              offset: Offset(0, 3), // changes position of shadow
            ),
          ],
        ),
        dropdownButtonDecoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          color: Colors.blue,
        ),
        dropdownButtonUnderline: Container(
          height: 2,
          color: Colors.transparent,
        ),
        dropdownButtonIcon: Icon(
          Icons.arrow_drop_down,
          color: Colors.white,
        ),
        dropdownButtonColor: Colors.blue,
        dropdownButtonIconData: Icons.arrow_drop_down,
        dropdownButtonHint: Text(
          'Select',
          style: TextStyle(color: Colors.white),
        ),
        dropdownSearchDecoration: InputDecoration(
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(10),
          ),
          filled: true,
          fillColor: Colors.white,
          hintText: 'Search...',
          prefixIcon: Icon(Icons.search),
          suffixIcon: Icon(Icons.clear),
        ),
        isSearchable: true,
        searchController: TextEditingController(),
        searchHint: 'Search...',
        clearIcon: Icon(Icons.clear),
        searchStyle: TextStyle(color: Colors.black),
        clearButtonColor: Colors.grey,
      ),
    );
  }
}

在这个示例中,我们展示了如何配置CustomizableDropdown的各种参数:

  • value:当前选中的值。
  • hint:当下拉菜单未选中任何项时显示的提示文本。
  • items:下拉菜单的项列表。
  • onChanged:当选中项改变时的回调函数。
  • dropdownDecoration:下拉菜单的装饰,比如边框、颜色和阴影。
  • dropdownButtonDecoration:下拉按钮的装饰。
  • dropdownButtonUnderline:下拉按钮下划线的样式(在这个示例中设置为透明)。
  • dropdownButtonIcondropdownButtonIconData:下拉按钮图标的样式和数据。
  • dropdownButtonHint:下拉按钮的提示文本。
  • dropdownSearchDecoration:搜索输入框的装饰。
  • isSearchable:是否启用搜索功能。
  • searchController:搜索输入框的控制器。
  • searchHint:搜索输入框的提示文本。
  • clearIcon:清除图标的样式。
  • searchStyle:搜索输入框中文本的样式。
  • clearButtonColor:清除按钮的颜色。

这个示例展示了如何使用customizable_dropdown插件创建一个高度可定制的下拉选择菜单。你可以根据需要调整这些参数以满足你的UI设计需求。

回到顶部