Flutter动画菜单按钮插件animated_fab_button_menu的使用

Flutter动画菜单按钮插件animated_fab_button_menu的使用

动画FAB菜单

Animated FAB Menu 插件允许你在 Flutter 应用程序中添加一个带有动画效果的浮动操作按钮(FAB),该按钮展开时会显示一系列菜单项。

特性

FabMenu 小部件通常放置在 Scaffold.floatingActionButton 参数中,替代原有的 FloatingActionButton 小部件。其位置由 fabAlignment 属性设置,因此你无需通过 Scaffold.floatingActionButtonLocation 参数来设置位置。默认情况下,位置设置为 bottomCenter,但你可以通过 fabAlignment 属性将其放置在屏幕的任何位置。此外,你可以通过 fabIcon 和 fabBackgroundColor 属性自定义按钮的图标和颜色。

菜单

按下浮动操作按钮后,页面将以一些酷炫的动画方式打开一个包含菜单项的列表。这些菜单项可以通过 children 列表属性进行指定。每个列表中的子项需要一个 title 来表示项目名称,一个 style 来表示文本小部件的样式,以及一个 onTap 函数来处理点击事件。此外,你还可以使用 overlayColor 属性更改背景色,overlayOpacity 属性更改背景色透明度,以及 closeMenuButton 属性自定义关闭菜单按钮。

包将自行处理动画。

animated_fab_menu


开始使用

步骤1:添加最新版本的包到你的 pubspec.yaml 文件中(并运行 dart pub get):

dependencies:
  animated_fab_button_menu: ^0.0.2

步骤2:导入包并在你的 Flutter 应用程序中使用它:

import 'package:animated_fab_button_menu/animated_fab_button_menu.dart';

使用方法

你可以修改以下属性:

  • fabIcon
  • fabBackgroundColor
  • fabAlignment
  • overlayColor(菜单背景颜色)
  • overlayOpacity(菜单背景颜色透明度)
  • elevation
  • closeMenuButton(关闭菜单按钮)
  • children(菜单项)
  • title(子菜单项名称)
  • style(子菜单项文本样式)
  • onTap(子菜单项点击函数)

示例使用(包含所有参数):

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

  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FabMenu(
          fabBackgroundColor: Colors.amber,
          elevation: 2.0,
          fabAlignment: Alignment.bottomCenter,
          fabIcon: const Icon(Icons.more_horiz),
          closeMenuButton: const Icon(
            Icons.arrow_back,
            color: Colors.white,
          ),
          overlayOpacity: 0.5,
          overlayColor: Colors.amber,
          children: [
            MenuItem(
              title: '首页',
              onTap: () {},
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 24,
                  fontWeight: FontWeight.bold),
            ),
            MenuItem(
              title: '个人资料',
              onTap: () {},
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 24,
                  fontWeight: FontWeight.bold),
            ),
            MenuItem(
              title: '消息',
              onTap: () {},
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 24,
                  fontWeight: FontWeight.bold),
            ),
            MenuItem(
              title: '反馈',
              onTap: () {},
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 24,
                  fontWeight: FontWeight.bold),
            ),
            MenuItem(
              title: '设置',
              onTap: () {},
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 24,
                  fontWeight: FontWeight.bold),
            ),
            MenuItem(
              title: '分享',
              onTap: () {},
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 24,
                  fontWeight: FontWeight.bold),
            ),
            MenuItem(
              title: '联系我们!',
              onTap: () {},
              style: const TextStyle(
                  color: Colors.white,
                  fontSize: 24,
                  fontWeight: FontWeight.bold),
            ),
          ]),
      body: const Center(
        child: Text('Flutter 动画FAB菜单'),
      ),
    );
  }
}

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

1 回复

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


当然,以下是如何在Flutter项目中使用animated_fab_button_menu插件来实现动画菜单按钮的示例代码。这个插件允许你创建一个带有动画效果的浮动操作按钮(FAB)菜单。

首先,确保你已经在pubspec.yaml文件中添加了animated_fab_button_menu依赖:

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

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

接下来,在你的Flutter项目中,你可以使用以下代码来实现动画菜单按钮:

import 'package:flutter/material.dart';
import 'package:animated_fab_button_menu/animated_fab_button_menu.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> menuItems = ['Item 1', 'Item 2', 'Item 3'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Animated FAB Button Menu Demo'),
      ),
      body: Center(
        child: Text('Tap the FAB below to see the animated menu'),
      ),
      floatingActionButton: AnimatedFabButtonMenu(
        menuButtons: menuItems.map((String item) {
          return FloatingActionButton(
            heroTag: item,
            backgroundColor: Colors.blue,
            onPressed: () {
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(content: Text("Selected: $item")),
              );
            },
            child: Icon(Icons.add),
          );
        }).toList(),
        mainButton: FloatingActionButton(
          backgroundColor: Colors.blueAccent,
          onPressed: () {},
          child: Icon(Icons.menu),
        ),
        animationDuration: Duration(milliseconds: 300),
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.end,
        elevation: 8.0,
        mainButtonElevation: 12.0,
      ),
    );
  }
}

代码说明:

  1. 依赖引入:确保在pubspec.yaml中添加了animated_fab_button_menu依赖。
  2. 主应用:MyApp是一个基本的MaterialApp,设置了主题和主页。
  3. 主页:MyHomePage是一个有状态的Widget,包含一个Scaffold,其中包含一个AppBar和一个居中的Text。
  4. 浮动操作按钮菜单:
    • AnimatedFabButtonMenu是核心组件,它接受一个menuButtons列表和一个mainButton。
    • menuButtons列表包含了多个FloatingActionButton,每个按钮都有相应的图标和点击事件。
    • mainButton是主要的浮动操作按钮,当点击时,会展开或收起菜单。
    • animationDuration定义了动画的持续时间。
    • mainAxisAlignment和crossAxisAlignment定义了菜单按钮的布局对齐方式。
    • elevation和mainButtonElevation定义了按钮的阴影高度。

这样,你就创建了一个带有动画效果的浮动操作按钮菜单。当你点击主按钮时,菜单按钮会以动画效果展开或收起。

回到顶部