Flutter菜单动画插件flutter_boom_menu的使用

Flutter菜单动画插件flutter_boom_menu的使用

BoomMenu 是一个用于在 Flutter 应用中创建动画菜单的插件。它可以替代 FloatingActionButton,并支持自定义图标、标题和副标题。下面是使用该插件的详细步骤和示例代码。

使用

BoomMenu 可以放置在 Scaffold.floatingActionButton 参数中。它不能通过 Scaffold.floatingActionButtonLocation 参数来设置位置,但可以通过 marginRightmarginBottom 参数(默认值为16)来调整按钮的位置。

每个子按钮可以包含一个 IconTitleSubtitle。如果未提供 Title 参数,则不会渲染标题。

示例代码(包含所有参数)

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

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

class MyApp extends StatefulWidget {
  [@override](/user/override)
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  bool scrollVisible = true;

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Boom Menu Example')),
      body: ListView.builder(
        itemCount: 30,
        itemBuilder: (ctx, i) => ListTile(title: Text('Item $i')),
      ),
      floatingActionButton: BoomMenu(
        animatedIcon: AnimatedIcons.menu_close,
        animatedIconTheme: IconThemeData(size: 22.0),
        onOpen: () => print('OPENING DIAL'),
        onClose: () => print('DIAL CLOSED'),
        scrollVisible: scrollVisible,
        overlayColor: Colors.black,
        overlayOpacity: 0.7,
        children: [
          MenuItem(
            child: Icon(Icons.accessibility, color: Colors.black),
            title: "Profiles",
            titleColor: Colors.white,
            subtitle: "You Can View the Noel Profile",
            subTitleColor: Colors.white,
            backgroundColor: Colors.deepOrange,
            onTap: () => print('FIRST CHILD'),
          ),
          MenuItem(
            child: Icon(Icons.brush, color: Colors.black),
            title: "Profiles",
            titleColor: Colors.white,
            subtitle: "You Can View the Noel Profile",
            subTitleColor: Colors.white,
            backgroundColor: Colors.green,
            onTap: () => print('SECOND CHILD'),
          ),
          MenuItem(
            child: Icon(Icons.keyboard_voice, color: Colors.black),
            title: "Profile",
            titleColor: Colors.white,
            subtitle: "You Can View the Noel Profile",
            subTitleColor: Colors.white,
            backgroundColor: Colors.blue,
            onTap: () => print('THIRD CHILD'),
          ),
          MenuItem(
            child: Icon(Icons.ac_unit, color: Colors.black),
            title: "Profiles",
            titleColor: Colors.white,
            subtitle: "You Can View the Noel Profile",
            subTitleColor: Colors.white,
            backgroundColor: Colors.blue,
            onTap: () => print('FOURTH CHILD'),
          )
        ],
      ),
    );
  }
}

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

1 回复

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


flutter_boom_menu 是一个用于创建动画菜单的 Flutter 插件。它允许你创建一个带有动画效果的浮动按钮,点击后会展开多个子按钮,类似于“爆炸”菜单的效果。这个插件非常适合用于需要快速访问多个操作的场景。

安装

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

dependencies:
  flutter:
    sdk: flutter
  flutter_boom_menu: ^2.0.0

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

基本使用

以下是一个简单的示例,展示了如何使用 flutter_boom_menu 创建一个基本的动画菜单:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Boom Menu Example'),
        ),
        body: Center(
          child: Text('Press the button to see the menu!'),
        ),
        floatingActionButton: BoomMenu(
          animatedIcon: AnimatedIcons.menu_close,
          animatedIconTheme: IconThemeData(size: 22.0),
          child: Icon(Icons.add),
          onOpen: () => print('OPENING DIAL'),
          onClose: () => print('DIAL CLOSED'),
          scrollVisible: true,
          overlayColor: Colors.black,
          overlayOpacity: 0.7,
          children: [
            MenuItem(
              child: Icon(Icons.accessibility, color: Colors.white),
              title: "Accessibility",
              titleColor: Colors.white,
              subtitle: "Accessibility settings",
              subTitleColor: Colors.white,
              backgroundColor: Colors.blue,
              onTap: () => print('ACCESSIBILITY PRESSED'),
            ),
            MenuItem(
              child: Icon(Icons.account_balance, color: Colors.white),
              title: "Account Balance",
              titleColor: Colors.white,
              subtitle: "Check your balance",
              subTitleColor: Colors.white,
              backgroundColor: Colors.green,
              onTap: () => print('ACCOUNT BALANCE PRESSED'),
            ),
            MenuItem(
              child: Icon(Icons.adb, color: Colors.white),
              title: "Android Debug Bridge",
              titleColor: Colors.white,
              subtitle: "Debug your app",
              subTitleColor: Colors.white,
              backgroundColor: Colors.red,
              onTap: () => print('ADB PRESSED'),
            ),
          ],
        ),
      ),
    );
  }
}
回到顶部