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

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

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

简介

awesome_bottom_bar 是一个用于Flutter的底部导航栏插件,提供了多种风格和动画效果。以下是如何在你的Flutter项目中使用这个插件。

插件安装

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

dependencies:
  awesome_bottom_bar: any

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

接下来,在你的 Dart 文件中导入该插件:

import 'package:awesome_bottom_bar/awesome_bottom_bar.dart';
import 'package:awesome_bottom_bar/widgets/inspired/inspired.dart';

示例代码

以下是一个完整的示例代码,展示如何使用 awesome_bottom_bar 插件来创建一个带有多种风格的底部导航栏。

import 'package:flutter/material.dart';
import 'package:awesome_bottom_bar/awesome_bottom_bar.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 Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int visit = 0;
  double height = 30;
  Color colorSelect = const Color(0XFF0686F8);
  Color color = const Color(0XFF7AC0FF);
  Color color2 = const Color(0XFF96B1FD);
  Color bgColor = const Color(0XFF1752FE);

  static const List<TabItem> items = [
    TabItem(icon: Icons.home),
    TabItem(icon: Icons.search_sharp, title: 'Shop'),
    TabItem(icon: Icons.favorite_border, title: 'Wishlist'),
    TabItem(icon: Icons.shopping_cart_outlined, title: 'Cart'),
    TabItem(icon: Icons.account_box, title: 'Profile'),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.symmetric(vertical: 20),
        child: Column(
          children: [
            SizedBox(height: height),
            BottomBarInspiredOutside(
              items: items,
              backgroundColor: bgColor,
              color: color2,
              colorSelected: Colors.white,
              indexSelected: visit,
              onTap: (int index) => setState(() {
                visit = index;
              }),
              top: -25,
              animated: true,
              itemStyle: ItemStyle.hexagon,
              chipStyle: const ChipStyle(drawHexagon: true),
            ),
            SizedBox(height: height),
            BottomBarCreative(
              items: items,
              backgroundColor: Colors.green.withOpacity(0.21),
              color: color,
              colorSelected: colorSelect,
              indexSelected: visit,
              onTap: (int index) => setState(() {
                visit = index;
              }),
            ),
            SizedBox(height: height),
            BottomBarDefault(
              items: items,
              backgroundColor: Colors.green,
              color: Colors.white,
              colorSelected: Colors.orange,
              onTap: (int index) => setState(() {
                visit = index;
              }),
            ),
            SizedBox(height: height),
            BottomBarFloating(
              items: items,
              backgroundColor: bgColor,
              color: color2,
              colorSelected: Colors.white,
              indexSelected: visit,
              onTap: (int index) => setState(() {
                visit = index;
              }),
            ),
          ],
        ),
      ),
      bottomNavigationBar: Container(
        padding: const EdgeInsets.only(bottom: 30, right: 32, left: 32),
        child: BottomBarFloating(
          items: items,
          backgroundColor: Colors.green,
          color: Colors.white,
          colorSelected: Colors.orange,
          indexSelected: visit,
          paddingVertical: 24,
          onTap: (int index) => setState(() {
            visit = index;
          }),
        ),
      ),
    );
  }
}

使用说明

添加TabItem

在上面的示例中,我们定义了一个 List<TabItem>,每个 TabItem 包含一个图标和可选的标题:

static const List<TabItem> items = [
  TabItem(icon: Icons.home),
  TabItem(icon: Icons.search_sharp, title: 'Shop'),
  TabItem(icon: Icons.favorite_border, title: 'Wishlist'),
  TabItem(icon: Icons.shopping_cart_outlined, title: 'Cart'),
  TabItem(icon: Icons.account_box, title: 'Profile'),
];

创建不同的底部导航栏样式

你可以选择不同的底部导航栏样式,例如 BottomBarInspiredOutsideBottomBarCreativeBottomBarDefaultBottomBarFloating。每个样式都有自己的参数,如背景颜色、未选中颜色、选中颜色等。

处理点击事件

通过 onTap 回调函数处理用户点击事件,并更新当前选中的索引:

onTap: (int index) => setState(() {
  visit = index;
}),

结论

awesome_bottom_bar 提供了丰富的底部导航栏样式和动画效果,可以满足不同应用的需求。更多详细信息和高级用法,请参考 官方GitHub仓库

如果你遇到任何问题或有改进建议,欢迎在 GitHub Issues 上提交。


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

1 回复

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


当然,以下是一个关于如何在Flutter应用中使用awesome_bottom_bar插件的示例代码。awesome_bottom_bar是一个流行的Flutter包,用于创建美观且功能丰富的底部导航栏。

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

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

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

接下来,是一个完整的示例代码,展示如何使用awesome_bottom_bar

import 'package:flutter/material.dart';
import 'package:awesome_bottom_bar/awesome_bottom_bar.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<AwesomeBarItem> _items = [
    AwesomeBarItem(
      iconData: Icons.home,
      title: 'Home',
      color: Colors.blue,
    ),
    AwesomeBarItem(
      iconData: Icons.search,
      title: 'Search',
      color: Colors.red,
    ),
    AwesomeBarItem(
      iconData: Icons.add,
      title: 'Add',
      color: Colors.green,
    ),
    AwesomeBarItem(
      iconData: Icons.account_circle,
      title: 'Profile',
      color: Colors.orange,
    ),
  ];

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Awesome Bottom Bar Demo'),
      ),
      body: Center(
        child: Text('You have selected: ${_items[_selectedIndex].title}'),
      ),
      bottomNavigationBar: AwesomeBottomBar(
        fixedColor: Colors.white,
        notchedShape: RoundedNotchedRectangle(),
        backgroundColor: Colors.grey[850],
        borderRadius: BorderRadius.circular(30),
        items: _items,
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
      ),
    );
  }
}

代码解释

  1. 依赖导入

    import 'package:flutter/material.dart';
    import 'package:awesome_bottom_bar/awesome_bottom_bar.dart';
    
  2. 主应用

    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(),
        );
      }
    }
    
  3. 主页面

    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
      int _selectedIndex = 0;
      final List<AwesomeBarItem> _items = [
        AwesomeBarItem(
          iconData: Icons.home,
          title: 'Home',
          color: Colors.blue,
        ),
        // 更多items...
      ];
    
      void _onItemTapped(int index) {
        setState(() {
          _selectedIndex = index;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Awesome Bottom Bar Demo'),
          ),
          body: Center(
            child: Text('You have selected: ${_items[_selectedIndex].title}'),
          ),
          bottomNavigationBar: AwesomeBottomBar(
            fixedColor: Colors.white,
            notchedShape: RoundedNotchedRectangle(),
            backgroundColor: Colors.grey[850],
            borderRadius: BorderRadius.circular(30),
            items: _items,
            currentIndex: _selectedIndex,
            onTap: _onItemTapped,
          ),
        );
      }
    }
    

在这个示例中,我们创建了一个包含四个导航项的底部导航栏。当用户点击某个导航项时,应用会更新显示的内容,并相应地改变底部导航栏的选中项。

希望这个示例代码能够帮助你理解如何在Flutter应用中使用awesome_bottom_bar插件。

回到顶部