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

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

简介

bottom_nav_bar 是一个易于使用的干净底部导航栏。

预览

Preview 1 Preview 2
Preview 1 Preview 2
Preview 3
Preview 3

可用的自定义选项

BottomNavBar
  • iconSize - 项目图标的大小。
  • items - 导航项,项目数量应在3到5之间。
  • selectedIndex - 当前选中项目的索引。默认为零。
  • onItemSelected - 监听项目点击事件,提供选中项目的索引。
  • backgroundColor - 底部导航栏的背景颜色。
  • showElevation - 如果为false,则移除AppBar的阴影。
  • mainAxisAlignment - 用于更改项目水平对齐方式。主要用于只有两个项目且需要居中对齐时。
  • curve - 自定义项目变化动画的参数。
  • containerHeight - 更改导航栏的高度。
  • containerPadding - 更改导航栏的内边距。
BottomNavBarItem
  • icon - 项目的图标。
  • title - 选中此项目时出现在图标的文本。
  • activeColor - 激活项目的图标和文本颜色。
  • inactiveColor - 未激活项目的图标颜色。
  • activeBackgroundColor - 激活项目的背景颜色。
  • inactiveBackgroundColor - 未激活项目的背景颜色。

基本用法

import 'package:bottom_nav_bar/bottom_nav_bar.dart';

bottomNavigationBar: BottomNavBar(
  showElevation: true,
  selectedIndex: _currentIndex,
  onItemSelected: (index) {
    setState(() => _currentIndex = index);
  },
  items: [
    BottomNavBarItem(
      title: 'Home',
      icon: const Icon(Icons.home),
      activeColor: Colors.white,
      inactiveColor: Colors.black,
      activeBackgroundColor: Colors.red.shade300,
    ),
    BottomNavBarItem(
      title: 'Profile',
      icon: const Icon(Icons.person),
      activeColor: Colors.white,
      inactiveColor: Colors.black,
      activeBackgroundColor: Colors.blue.shade300,
    ),
    BottomNavBarItem(
      title: 'Message',
      icon: const Icon(Icons.chat_bubble),
      inactiveColor: Colors.black,
      activeColor: Colors.white,
      activeBackgroundColor: Colors.green.shade300,
    ),
    BottomNavBarItem(
      title: 'Settings',
      icon: const Icon(Icons.settings),
      inactiveColor: Colors.black,
      activeColor: Colors.black,
      activeBackgroundColor: Colors.yellow.shade300,
    ),
  ],
)

完整示例

import 'package:bottom_nav_bar/bottom_nav_bar.dart';

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

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

class _RootPageState extends State<RootPage> {
  int _currentIndex = 0;

  [@override](/user/override)
  void initState() {
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: _body(),
      bottomNavigationBar: _bottomNavBar(),
    );
  }

  Widget _body() => SizedBox.expand(
        child: IndexedStack(
          index: _currentIndex,
          children: const [
            PageOne(),
            PageTwo(),
            PageThree(),
            PageFour(),
          ],
        ),
      );

  Widget _bottomNavBar() => BottomNavBar(
        showElevation: true,
        selectedIndex: _currentIndex,
        onItemSelected: (index) {
          setState(() => _currentIndex = index);
        },
        items: [
          BottomNavBarItem(
            title: 'Home',
            icon: const Icon(Icons.home),
            activeColor: Colors.white,
            inactiveColor: Colors.black,
            activeBackgroundColor: Colors.red.shade300,
          ),
          BottomNavBarItem(
            title: 'Profile',
            icon: const Icon(Icons.person),
            activeColor: Colors.white,
            inactiveColor: Colors.black,
            activeBackgroundColor: Colors.blue.shade300,
          ),
          BottomNavBarItem(
            title: 'Message',
            icon: const Icon(Icons.chat_bubble),
            inactiveColor: Colors.black,
            activeColor: Colors.white,
            activeBackgroundColor: Colors.green.shade300,
          ),
          BottomNavBarItem(
            title: 'Settings',
            icon: const Icon(Icons.settings),
            inactiveColor: Colors.black,
            activeColor: Colors.black,
            activeBackgroundColor: Colors.yellow.shade300,
          ),
        ],
      );
}

示例代码

import 'package:example/root_page.dart';
import 'package:flutter/material.dart';

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const RootPage(),
    );
  }
}

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

1 回复

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


bottom_nav_bar 是一个用于 Flutter 的第三方插件,可以帮助你轻松地实现底部导航栏。你可以使用它来创建具有不同图标、标签和动画效果的底部导航栏。

以下是使用 bottom_nav_bar 插件的基本步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  bottom_nav_bar: ^1.0.0  # 使用最新版本

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

2. 导入包

在你的 Dart 文件中导入 bottom_nav_bar 包:

import 'package:bottom_nav_bar/bottom_nav_bar.dart';

3. 创建底部导航栏

你可以使用 BottomNavBar 组件来创建底部导航栏。以下是一个简单的示例:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  [@override](/user/override)
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int _currentIndex = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Bottom Nav Bar Example'),
      ),
      body: Center(
        child: Text('Selected Index: $_currentIndex'),
      ),
      bottomNavigationBar: BottomNavBar(
        items: [
          BottomNavBarItem(
            icon: Icon(Icons.home),
            title: Text('Home'),
          ),
          BottomNavBarItem(
            icon: Icon(Icons.search),
            title: Text('Search'),
          ),
          BottomNavBarItem(
            icon: Icon(Icons.person),
            title: Text('Profile'),
          ),
        ],
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}

4. 自定义底部导航栏

bottom_nav_bar 提供了多种自定义选项,例如:

  • 颜色:你可以自定义选中和未选中项的颜色。
  • 动画:你可以启用或禁用动画效果。
  • 图标大小:你可以调整图标的大小。

以下是一个自定义底部导航栏的示例:

bottomNavigationBar: BottomNavBar(
  items: [
    BottomNavBarItem(
      icon: Icon(Icons.home),
      title: Text('Home'),
    ),
    BottomNavBarItem(
      icon: Icon(Icons.search),
      title: Text('Search'),
    ),
    BottomNavBarItem(
      icon: Icon(Icons.person),
      title: Text('Profile'),
    ),
  ],
  currentIndex: _currentIndex,
  onTap: (index) {
    setState(() {
      _currentIndex = index;
    });
  },
  selectedItemColor: Colors.blue,
  unselectedItemColor: Colors.grey,
  animationDuration: Duration(milliseconds: 300),
  iconSize: 30,
  elevation: 10,
),

5. 处理页面切换

通常,底部导航栏用于切换不同的页面。你可以根据 _currentIndex 的值来显示不同的页面内容。例如:

class _HomeScreenState extends State<HomeScreen> {
  int _currentIndex = 0;

  final List<Widget> _pages = [
    HomePage(),
    SearchPage(),
    ProfilePage(),
  ];

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Bottom Nav Bar Example'),
      ),
      body: _pages[_currentIndex],
      bottomNavigationBar: BottomNavBar(
        items: [
          BottomNavBarItem(
            icon: Icon(Icons.home),
            title: Text('Home'),
          ),
          BottomNavBarItem(
            icon: Icon(Icons.search),
            title: Text('Search'),
          ),
          BottomNavBarItem(
            icon: Icon(Icons.person),
            title: Text('Profile'),
          ),
        ],
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}
回到顶部