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

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

navi_bar_flutter

navi_bar_flutter 是一个可自定义的底部导航栏插件,它使用简单的动画和主题颜色,并且包含了一些预设样式。

NaviBar 示例

添加依赖

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

dependencies: 
  navi_bar_flutter: ^1.0.8 # 最新版本

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

如何使用

在你的 Scaffold 中使用 [NaviBar] 作为 bottomNavigationBar

bottomNavigationBar: NaviBar(
  type: NaviBarType.rounded, // 设置导航栏类型
  selectedIndex: _selectedIndex, // 当前选中的索引
  items: items, // 导航栏项列表
  onTap: (index) { // 点击事件处理
    setState(() {
      _selectedIndex = index;
    });
  },
)

NaviBar 属性

属性名 描述
onTap 处理点击事件的函数
items 包含 NaviBarItem 的列表
selectedIndex 当前选中的导航栏索引
type 导航栏类型,默认为 NaviBarType.basic
backgroundColor 导航栏背景颜色,默认为 null,使用主题背景色
selectedTabColor 选中选项卡的颜色,默认为 null,使用主题 scaffoldBackgroundColor
unselectedTabColor 未选中选项卡的颜色,默认为 null,使用主题背景色
selectedIconColor 选中图标的颜色,默认为 null,使用主题背景色
unselectedIconColor 未选中图标的颜色,默认为 null,使用主题 primaryColor
selectedIconSize 选中图标的大小,默认为 32
unselectedIconSize 未选中图标的大小,默认为 24
barHeight 导航栏的高度,默认为 85
barPadding 导航栏的内边距,默认为 EdgeInsets.all(0)
borderRadius 导航栏的圆角,默认为 BorderRadius.all(Radius.zero)
duration 动画持续时间,默认为 250ms
curve 动画曲线,默认为 Curves.linear

NaviBarItem 属性

属性名 描述
icon 显示的图标
page 选择该选项时显示的页面
label 选中时显示的标签
selectedBackgroundColor 选中时的背景颜色,默认为 null
unselectedBackgroundColor 未选中时的背景颜色,默认为 null
selectedIconColor 选中时的图标和标签颜色,默认为 null
unselectedIconColor 未选中时的图标和标签颜色,默认为 null
activeIcon 选中时改变的图标

完整示例代码

以下是一个完整的示例代码,展示如何使用 navi_bar_flutter

import 'package:flutter/material.dart';
import 'package:navi_bar_flutter/navi_bar_flutter.dart'; // 引入插件
import 'Welcome.dart'; // 页面组件
import 'hello.dart';
import 'page_3.dart';
import 'page_4.dart';

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DemoPage(),
    );
  }
}

class DemoPage extends StatefulWidget {
  const DemoPage({Key? key}) : super(key: key);

  [@override](/user/override)
  _DemoPageState createState() => _DemoPageState();
}

class _DemoPageState extends State<DemoPage> {
  /// 定义当前选中的索引
  int _selectedIndex = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Demo Page'),
        leading: Icon(Icons.catching_pokemon),
      ),
      
      /// 使用 NaviBar 作为底部导航栏
      bottomNavigationBar: NaviBar(
        type: NaviBarType.basic, // 导航栏类型
        selectedIndex: _selectedIndex, // 当前选中的索引
        items: items, // 导航栏项列表
        onTap: (index) { // 点击事件处理
          setState(() {
            _selectedIndex = index;
          });
        },
      ),

      /// 根据选中的索引切换页面
      body: items[_selectedIndex].page,
    );
  }
}

/// 创建 NaviBarItem 列表
List<NaviBarItem> items = [
  NaviBarItem(
    icon: Icons.house, // 图标
    label: 'Home', // 标签
    page: Hello(), // 对应的页面
  ),
  NaviBarItem(
    icon: Icons.download, // 图标
    label: 'Download', // 标签
    page: Welcome(), // 对应的页面
  ),
  NaviBarItem(
    icon: Icons.account_circle, // 图标
    label: 'Account', // 标签
    page: Page3(), // 对应的页面
  ),
  NaviBarItem(
    icon: Icons.folder, // 图标
    label: 'Files', // 标签
    page: Page4(), // 对应的页面
  ),
];

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

1 回复

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


navi_bar_flutter 是一个用于在 Flutter 应用中创建底部导航栏的插件。它提供了高度可定制的底部导航栏,支持多种样式和动画效果。以下是如何使用 navi_bar_flutter 插件的基本步骤。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  navi_bar_flutter: ^1.0.0  # 请根据最新版本号进行更新

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

2. 导入包

在你的 Dart 文件中导入 navi_bar_flutter 包。

import 'package:navi_bar_flutter/navi_bar_flutter.dart';

3. 创建底部导航栏

接下来,你可以使用 NaviBar 组件来创建一个底部导航栏。

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;

  final List<Widget> _pages = [
    Page1(),
    Page2(),
    Page3(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_currentIndex],
      bottomNavigationBar: NaviBar(
        items: [
          NaviItem(icon: Icons.home, label: 'Home'),
          NaviItem(icon: Icons.business, label: 'Business'),
          NaviItem(icon: Icons.school, label: 'School'),
        ],
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}

class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Page 1'));
  }
}

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Page 2'));
  }
}

class Page3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Page 3'));
  }
}

4. 自定义导航栏

navi_bar_flutter 提供了多种自定义选项,例如更改颜色、图标大小、动画效果等。

bottomNavigationBar: NaviBar(
  items: [
    NaviItem(icon: Icons.home, label: 'Home'),
    NaviItem(icon: Icons.business, label: 'Business'),
    NaviItem(icon: Icons.school, label: 'School'),
  ],
  currentIndex: _currentIndex,
  onTap: (index) {
    setState(() {
      _currentIndex = index;
    });
  },
  backgroundColor: Colors.blue,
  selectedItemColor: Colors.white,
  unselectedItemColor: Colors.grey,
  iconSize: 24.0,
  animationDuration: Duration(milliseconds: 300),
),

5. 处理导航栏点击事件

onTap 回调中,你可以处理导航栏的点击事件,并更新当前选中的索引。

onTap: (index) {
  setState(() {
    _currentIndex = index;
  });
},
回到顶部