Flutter圆形按钮动画插件circle_bnb的使用

Flutter圆形按钮动画插件circle_bnb的使用

该插件允许在屏幕底部创建一个圆形底部导航栏。

安装

在你的 pubspec.yaml 文件中添加以下依赖:

dependencies:
  circle_bnb: 0.0.1

然后运行 flutter pub get 来获取依赖。

使用

首先导入插件:

import 'package:circle_bnb/circle_bnb.dart';

示例

以下是一个完整的示例,展示了如何使用 circle_bnb 插件来创建一个带有圆形按钮动画的底部导航栏。

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

void main() => runApp(const App());

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primaryColor: Colors.blue,
        highlightColor: Colors.transparent,
        splashColor: Colors.blue.shade200,
        bottomNavigationBarTheme: const BottomNavigationBarThemeData(
          selectedItemColor: Colors.blue,
          selectedLabelStyle: TextStyle(
            fontSize: 11,
            fontWeight: FontWeight.w600
          ),
          unselectedItemColor: Colors.black38,
          unselectedLabelStyle: TextStyle(
            fontSize: 10,
            fontWeight: FontWeight.w300
          ),
          showUnselectedLabels: true,
          backgroundColor: Colors.white,
          type: BottomNavigationBarType.fixed,
          elevation: 0,
        )
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  [@override](/user/override)
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  int _currentIndex = 0;

  List<CircleBNBItem> pages = [
    CircleBNBItem(title: "Home", icon: Icons.home_outlined),
    CircleBNBItem(title: "Dashboard", icon: Icons.dashboard_outlined),
    CircleBNBItem(title: "Profile", icon: Icons.person_outlined),
    CircleBNBItem(title: "Explore", icon: Icons.explore_outlined),
    CircleBNBItem(title: "Settings", icon: Icons.settings_outlined),
    CircleBNBItem(title: "Notifications", icon: Icons.notifications_outlined),
    CircleBNBItem(title: "Saved", icon: Icons.bookmark_outline_outlined),
    CircleBNBItem(title: "Favorites", icon: Icons.favorite_outline_outlined),
  ];

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'Circle BNB',
          style: TextStyle(
            color: Colors.white
          ),
        ),
        backgroundColor: Colors.primaries[_currentIndex],
        surfaceTintColor: Colors.transparent,
      ),
      body: IndexedStack(
        index: _currentIndex,
        children: List.generate(
          pages.length, (index) => ListView(
            children: List.generate(
              pages.length,
              (index2) => Container(
                color: Colors.primaries[(index + index2 >= 17) ? index2 : index + index2],
                height: MediaQuery.of(context).size.height * 0.5,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: List.generate(
                    pages.length,
                    (index3) => Text(
                      pages[index3].title,
                      style: TextStyle(
                        color: index3 == _currentIndex ? Colors.white : Colors.white24
                      ),
                    ),
                  )
                ),
              ),
            )
          )
        )
      ),
      extendBody: true,
      backgroundColor: Colors.primaries[_currentIndex],
      bottomNavigationBar: CircleBNB(
        navigationStyle: NavigationStyle.linear,
        linearItemCount: 5,
        size: Size(
          MediaQuery.of(context).size.width * 0.75,
          MediaQuery.of(context).size.height * 0.235
        ),
        dragSpeed: 0.05,
        items: pages,
        onChangeIndex: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}

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

1 回复

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


circle_bnb 是一个用于创建圆形底部导航栏(Circle Bottom Navigation Bar)的 Flutter 插件。它允许你创建一个带有动画效果的圆形按钮,通常用于底部导航栏中的主要操作按钮(Floating Action Button, FAB)。以下是如何使用 circle_bnb 插件的基本步骤。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  circle_bnb: ^1.0.0  # 请检查最新版本

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

2. 基本用法

在你的 Dart 文件中引入 circle_bnb,并使用 CircleBottomNavigationBar 小部件来创建圆形底部导航栏。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Circle BNB Example'),
        ),
        body: Center(
          child: Text('Welcome to Circle Bottom Navigation Bar Example'),
        ),
        bottomNavigationBar: CircleBottomNavigationBar(
          initialIndex: 0,
          onTap: (index) {
            print('Selected index: $index');
          },
          items: [
            CircleBottomNavigationBarItem(
              icon: Icons.home,
              label: 'Home',
            ),
            CircleBottomNavigationBarItem(
              icon: Icons.search,
              label: 'Search',
            ),
            CircleBottomNavigationBarItem(
              icon: Icons.person,
              label: 'Profile',
            ),
          ],
        ),
      ),
    );
  }
}

3. 自定义选项

CircleBottomNavigationBar 提供了多种自定义选项,你可以根据需要进行配置。以下是一些常用的自定义选项:

  • initialIndex: 初始选中的索引。
  • onTap: 当用户点击某个选项时的回调函数。
  • items: 底部导航栏的选项列表。
  • circleColor: 圆形按钮的背景颜色。
  • activeColor: 选中项的颜色。
  • inactiveColor: 未选中项的颜色。
  • circleSize: 圆形按钮的大小。
  • iconSize: 图标的尺寸。
  • circleShadow: 圆形按钮的阴影效果。

4. 自定义示例

以下是一个更复杂的示例,展示了如何自定义 CircleBottomNavigationBar

bottomNavigationBar: CircleBottomNavigationBar(
  initialIndex: 0,
  onTap: (index) {
    print('Selected index: $index');
  },
  circleColor: Colors.blue,
  activeColor: Colors.white,
  inactiveColor: Colors.grey,
  circleSize: 50.0,
  iconSize: 24.0,
  circleShadow: const [
    BoxShadow(
      color: Colors.black26,
      blurRadius: 10.0,
      spreadRadius: 2.0,
    ),
  ],
  items: [
    CircleBottomNavigationBarItem(
      icon: Icons.home,
      label: 'Home',
    ),
    CircleBottomNavigationBarItem(
      icon: Icons.search,
      label: 'Search',
    ),
    CircleBottomNavigationBarItem(
      icon: Icons.person,
      label: 'Profile',
    ),
  ],
),
回到顶部