Flutter底部气泡导航栏插件bubble_bottom_bar的使用

Flutter底部气泡导航栏插件bubble_bottom_bar的使用

BubbleBottomBar 是一个由 cubertodesign 设计并由 westdabestdb 开发的 Flutter 小部件。

开始使用

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

dependencies:
  bubble_bottom_bar: ^1.2.0

然后在 Dart 代码中导入该包:

import 'package:bubble_bottom_bar/bubble_bottom_bar.dart';

使用示例

下面是一个完整的示例代码,展示了如何使用 BubbleBottomBar 插件:

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Bubble Bottom Bar Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Bubble Bottom Bar Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late int currentIndex;

  @override
  void initState() {
    super.initState();
    currentIndex = 0;
  }

  void changePage(int index) {
    setState(() {
      currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: Icon(Icons.add),
        backgroundColor: Colors.red,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
      bottomNavigationBar: BubbleBottomBar(
        hasNotch: true,
        fabLocation: BubbleBottomBarFabLocation.end,
        opacity: .2,
        currentIndex: currentIndex,
        onTap: changePage,
        borderRadius: BorderRadius.vertical(
          top: Radius.circular(16),
        ), // 当凹槽启用时,圆角不起作用。
        elevation: 8,
        tilesPadding: EdgeInsets.symmetric(vertical: 8.0),
        items: [
          BubbleBottomBarItem(
            showBadge: true,
            badge: Text("5"),
            badgeColor: Colors.deepPurpleAccent,
            backgroundColor: Colors.red,
            icon: Icon(
              Icons.dashboard,
              color: Colors.black,
            ),
            activeIcon: Icon(
              Icons.dashboard,
              color: Colors.red,
            ),
            title: Text("Home"),
          ),
          BubbleBottomBarItem(
              backgroundColor: Colors.deepPurple,
              icon: Icon(
                Icons.access_time,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.access_time,
                color: Colors.deepPurple,
              ),
              title: Text("Logs")),
          BubbleBottomBarItem(
              backgroundColor: Colors.indigo,
              icon: Icon(
                Icons.folder_open,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.folder_open,
                color: Colors.indigo,
              ),
              title: Text("Folders")),
          BubbleBottomBarItem(
              backgroundColor: Colors.green,
              icon: Icon(
                Icons.menu,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.menu,
                color: Colors.green,
              ),
              title: Text("Menu"))
        ],
      ),
    );
  }
}

显示徽章

你可以通过设置 showBadgebadge 属性来在底部导航栏项上显示徽章。例如:

BubbleBottomBarItem(
  showBadge: true,
  badge: Text("5"),
  badgeColor: Colors.deepPurpleAccent,
  backgroundColor: Colors.red,
  icon: Icon(
    Icons.dashboard,
    color: Colors.black,
  ),
  activeIcon: Icon(
    Icons.dashboard,
    color: Colors.red,
  ),
  title: Text("Home"),
)

更多关于Flutter底部气泡导航栏插件bubble_bottom_bar的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter底部气泡导航栏插件bubble_bottom_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter应用中使用bubble_bottom_bar插件来实现底部气泡导航栏的代码示例。

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

dependencies:
  flutter:
    sdk: flutter
  bubble_bottom_bar: ^2.0.0  # 请检查最新版本号

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

接下来,是一个完整的Flutter应用示例,展示如何使用bubble_bottom_bar

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Bubble Bottom Bar Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: BubbleBottomBarExample(),
    );
  }
}

class BubbleBottomBarExample extends StatefulWidget {
  @override
  _BubbleBottomBarExampleState createState() => _BubbleBottomBarExampleState();
}

class _BubbleBottomBarExampleState extends State<BubbleBottomBarExample> with SingleTickerProviderStateMixin {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('Current Index: $_currentIndex'),
      ),
      bottomNavigationBar: BubbleBottomBar(
        opacity: 0.6,
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BubbleBottomBarItem(
            backgroundColor: Colors.blue,
            icon: Icon(Icons.home, color: Colors.white),
            title: Text('Home'),
          ),
          BubbleBottomBarItem(
            backgroundColor: Colors.green,
            icon: Icon(Icons.search, color: Colors.white),
            title: Text('Search'),
          ),
          BubbleBottomBarItem(
            backgroundColor: Colors.amber,
            icon: Icon(Icons.add, color: Colors.black),
            title: Text('Add'),
          ),
          BubbleBottomBarItem(
            backgroundColor: Colors.deepOrange,
            icon: Icon(Icons.favorite, color: Colors.white),
            title: Text('Favorite'),
          ),
          BubbleBottomBarItem(
            backgroundColor: Colors.indigo,
            icon: Icon(Icons.settings, color: Colors.white),
            title: Text('Settings'),
          ),
        ],
      ),
    );
  }
}

代码解释:

  1. 依赖引入:在pubspec.yaml中添加了bubble_bottom_bar依赖。
  2. 主应用MyApp是一个无状态小部件,它定义了应用的主题和主页。
  3. 主页BubbleBottomBarExample是一个有状态小部件,用于管理底部导航栏的当前索引。
  4. 状态管理:使用_BubbleBottomBarExampleState类来管理导航栏的当前索引,并在用户点击导航项时更新索引。
  5. BubbleBottomBar:底部导航栏小部件,设置了透明度、当前索引、点击事件处理函数以及导航项列表。
  6. BubbleBottomBarItem:每个导航项的定义,包括背景颜色、图标和标题。

运行这段代码后,你会看到一个简单的Flutter应用,底部有一个气泡导航栏,点击不同的导航项会在屏幕中央显示当前索引。

希望这能帮助你理解如何在Flutter应用中使用bubble_bottom_bar插件!

回到顶部