Flutter动画下拉按钮插件animated_dropdown_button的使用

Flutter 动画下拉按钮插件 animated_dropdown_button 的使用

简介

animated_dropdown_button 是一个带有有趣动画的简单下拉按钮。目前它只能列出字符串,并且可以通过 AnimatedDropdownButtonController 获取值。请适度使用。

开始使用

首先,你需要实例化一个 AnimatedDropdownButtonController,并传入一个字符串列表和初始值,该初始值必须存在于列表中。然后你可以实例化一个 AnimatedDropdownButton 并传入创建好的控制器。

你还可以使用 AnimatedSearchBox。只需先实例化一个 AnimatedSearchBoxControllerAnimatedSearchBox 完全可定制。

示例代码

以下是一个完整的示例代码,展示了如何使用 animated_dropdown_button 插件。

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

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

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

  // 这个小部件是你的应用程序的根。
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '动画下拉按钮演示',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: '动画下拉按钮演示'),
    );
  }
}

class AnimalModel extends AnimatedDropdownItem {
  final String name;

  AnimalModel(this.name);

  @override
  String get text => name;

  @override
  String get value => name;
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late final AnimatedSearchBoxController animatedSearchController;

  final AnimatedDropdownButtonController animalsController = AnimatedDropdownButtonController(
    items: [
      '猫',
      '狗',
      '鸟',
      '猫',
      '狗',
    ],
    initialValue: '狗',
  );

  @override
  void initState() {
    // 初始化状态
    super.initState();
    animatedSearchController = AnimatedSearchBoxController(
      items: animals,
      initialValue: AnimalModel(
        '猫',
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Container(
        color: const Color.fromARGB(255, 165, 149, 149),
        child: Center(
          child: SingleChildScrollView(
            child: Column(
              children: [
                const SizedBox(height: 50),
                AnimatedSearchBox(
                  width: 336,
                  controller: animatedSearchController,
                  backgroundColor: Colors.white,
                  hintText: '输入动物名称',
                ),
                const SizedBox(
                  height: 50,
                ),
                AnimatedDropdownButton(
                  controller: animalsController,
                  backgroundColor: Colors.red,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

// 动物模型列表
final List<AnimalModel> animals = [
  AnimalModel('猫'),
  AnimalModel('狗'),
  AnimalModel('鸟'),
  AnimalModel('驴'),
  AnimalModel('猴子'),
  AnimalModel('山羊'),
  AnimalModel('马'),
  AnimalModel('狮子'),
];

代码解释

  1. 导入库

    import 'package:animated_dropdown_button/animated_dropdown_button.dart';
    import 'package:flutter/material.dart';
    
  2. 主应用

    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: '动画下拉按钮演示',
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
            useMaterial3: true,
          ),
          home: const MyHomePage(title: '动画下拉按钮演示'),
        );
      }
    }
    
  3. 定义动物模型

    class AnimalModel extends AnimatedDropdownItem {
      final String name;
    
      AnimalModel(this.name);
    
      @override
      String get text => name;
    
      @override
      String get value => name;
    }
    
  4. 主页状态

    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key, required this.title});
    
      final String title;
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      late final AnimatedSearchBoxController animatedSearchController;
    
      final AnimatedDropdownButtonController animalsController = AnimatedDropdownButtonController(
        items: [
          '猫',
          '狗',
          '鸟',
          '猫',
          '狗',
        ],
        initialValue: '狗',
      );
    
      @override
      void initState() {
        super.initState();
        animatedSearchController = AnimatedSearchBoxController(
          items: animals,
          initialValue: AnimalModel(
            '猫',
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
            title: Text(widget.title),
          ),
          body: Container(
            color: const Color.fromARGB(255, 165, 149, 149),
            child: Center(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    const SizedBox(height: 50),
                    AnimatedSearchBox(
                      width: 336,
                      controller: animatedSearchController,
                      backgroundColor: Colors.white,
                      hintText: '输入动物名称',
                    ),
                    const SizedBox(
                      height: 50,
                    ),
                    AnimatedDropdownButton(
                      controller: animalsController,
                      backgroundColor: Colors.red,
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      }
    }
    
  5. 动物模型列表

    final List<AnimalModel> animals = [
      AnimalModel('猫'),
      AnimalModel('狗'),
      AnimalModel('鸟'),
      AnimalModel('驴'),
      AnimalModel('猴子'),
      AnimalModel('山羊'),
      AnimalModel('马'),
      AnimalModel('狮子'),
    ];
    

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

1 回复

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


animated_dropdown_button 是一个 Flutter 插件,用于创建带有动画效果的下拉按钮。它可以帮助你实现一个带有展开和收起动画的下拉菜单,提升用户体验。

安装

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

dependencies:
  flutter:
    sdk: flutter
  animated_dropdown_button: ^latest_version

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

基本用法

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

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Animated Dropdown Button Example'),
        ),
        body: Center(
          child: AnimatedDropdownButton(
            items: [
              DropdownMenuItem(child: Text('Item 1'), value: 'Item 1'),
              DropdownMenuItem(child: Text('Item 2'), value: 'Item 2'),
              DropdownMenuItem(child: Text('Item 3'), value: 'Item 3'),
            ],
            onChanged: (value) {
              print('Selected: $value');
            },
            child: Text('Select an item'),
          ),
        ),
      ),
    );
  }
}

参数说明

  • items: 下拉菜单中的选项列表,类型为 List<DropdownMenuItem<T>>
  • onChanged: 当用户选择一个选项时触发的回调函数。
  • child: 按钮的显示内容,通常是一个 Text 组件。
  • icon: 按钮右侧的图标,默认是一个向下的箭头。
  • dropdownWidth: 下拉菜单的宽度。
  • dropdownHeight: 下拉菜单的高度。
  • animationDuration: 展开和收起动画的持续时间。
  • animationCurve: 动画曲线,默认为 Curves.easeInOut

自定义样式

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

AnimatedDropdownButton(
  items: [
    DropdownMenuItem(child: Text('Item 1'), value: 'Item 1'),
    DropdownMenuItem(child: Text('Item 2'), value: 'Item 2'),
    DropdownMenuItem(child: Text('Item 3'), value: 'Item 3'),
  ],
  onChanged: (value) {
    print('Selected: $value');
  },
  child: Text('Select an item'),
  icon: Icon(Icons.arrow_drop_down),
  dropdownWidth: 200,
  dropdownHeight: 150,
  animationDuration: Duration(milliseconds: 300),
  animationCurve: Curves.easeInOut,
)
回到顶部