Flutter底部导航栏插件bottom_bar_matu的使用

发布于 1周前 作者 eggper 来自 Flutter

Flutter底部导航栏插件bottom_bar_matu的使用

bottom_bar_matu 是一个漂亮的、带动画效果的底部导航栏插件。它支持多种样式,如 BottomBarBubbleBottomBarDoubleBulletBottomBarLabelSlide

支持的样式

BottomBarBubble

DEMO

BottomBarDoubleBullet

DEMO

BottomBarLabelSlide

DEMO

使用方法

简单实现(使用图标数据)

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

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

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _index = 0;
  final PageController controller = PageController();

  void _onSelect(int index) {
    setState(() {
      _index = index;
      controller.jumpToPage(index);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      bottomNavigationBar: BottomBarBubble(
        selectedIndex: _index,
        items: [
          BottomBarItem(iconData: Icons.home),
          BottomBarItem(iconData: Icons.chat),
          BottomBarItem(iconData: Icons.notifications),
          BottomBarItem(iconData: Icons.calendar_month),
          BottomBarItem(iconData: Icons.settings),
        ],
        onSelect: _onSelect,
      ),
      body: PageView(
        controller: controller,
        children: const <Widget>[
          Center(child: Text('First Page')),
          Center(child: Text('Second Page')),
          Center(child: Text('Third Page')),
          Center(child: Text('Fourth Page')),
          Center(child: Text('Fifth Page')),
        ],
      ),
    );
  }
}

自定义小部件实现

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Bottom Bar Custom Widget'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _index = 0;
  final PageController controller = PageController();

  void _onSelect(int index) {
    setState(() {
      _index = index;
      controller.jumpToPage(index);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      bottomNavigationBar: BottomBarBubble(
        selectedIndex: _index,
        items: [
          BottomBarItem(
            iconBuilder: (color) => Image.asset('assets/ic_alarm.png', color: color, height: 30, width: 30),
          ),
          BottomBarItem(
            iconBuilder: (color) => Image.asset('assets/ic_bill.png', color: color, height: 30, width: 30),
          ),
          BottomBarItem(
            iconBuilder: (color) => Image.asset('assets/ic_box.png', color: color, height: 30, width: 30),
          ),
        ],
        onSelect: _onSelect,
      ),
      body: PageView(
        controller: controller,
        children: const <Widget>[
          Center(child: Text('First Page')),
          Center(child: Text('Second Page')),
          Center(child: Text('Third Page')),
        ],
      ),
    );
  }
}

许可证

Copyright (c) 2021 Tuannvm

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

通过以上示例代码,您可以快速上手并使用 bottom_bar_matu 插件来实现带有动画效果的底部导航栏。希望这些示例对您有所帮助!


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

1 回复

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


当然,关于使用Flutter中的bottom_bar_matu插件来创建底部导航栏,这里是一个简单的代码示例,展示了如何集成和使用这个插件。请确保你已经将bottom_bar_matu添加到了你的pubspec.yaml文件中:

dependencies:
  flutter:
    sdk: flutter
  bottom_bar_matu: ^最新版本号  # 请替换为实际的最新版本号

然后,在你的Dart文件中,你可以按照以下方式使用BottomBarMatu

import 'package:flutter/material.dart';
import 'package:bottom_bar_matu/bottom_bar_matu.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> with SingleTickerProviderStateMixin {
  int _selectedIndex = 0;
  final List<Widget> _widgetOptions = <Widget>[
    Text('首页'),
    Text('发现'),
    Text('消息'),
    Text('我的'),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('BottomBarMatu Demo'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomBarMatu(
        items: [
          BottomBarItem(
            icon: Icons.home,
            title: '首页',
          ),
          BottomBarItem(
            icon: Icons.explore,
            title: '发现',
          ),
          BottomBarItem(
            icon: Icons.message,
            title: '消息',
          ),
          BottomBarItem(
            icon: Icons.person,
            title: '我的',
          ),
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
        backgroundColor: Colors.white,
        unselectedItemColor: Colors.grey,
        selectedItemColor: Colors.blue,
        animationDuration: Duration(milliseconds: 300),
      ),
    );
  }
}

在这个示例中,我们做了以下几件事:

  1. 引入必要的包:我们导入了flutter/material.dartbottom_bar_matu/bottom_bar_matu.dart
  2. 定义主应用MyApp是一个无状态组件,它定义了应用的主题和主页。
  3. 创建主页MyHomePage是一个有状态组件,它管理底部导航栏的选中状态。
  4. 定义底部导航栏:使用BottomBarMatu来创建底部导航栏,并定义了四个导航项。
  5. 处理导航项点击:当点击导航项时,_onItemTapped方法会被调用,更新当前选中的导航项。

请注意,BottomBarItem的属性(如icontitle)可以根据你的需求进行调整。此外,BottomBarMatu还提供了许多自定义选项,比如设置未读消息数、更改动画持续时间等,你可以参考其官方文档获取更多详细信息。

回到顶部