Flutter底部导航栏BottomNavigationBar

本文介绍了Flutter中的BottomNavigationBar组件,包括shifting和fixed两种模式的使用,以及BottomNavigationBarItem的配置。通过实例展示了如何实现点击底部导航栏进行页面切换,详细解释了相关属性和回调函数的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flutter底部导航栏BottomNavigationBar

文章目录

Flutter 底部导航栏(BottomNavigationBar)

在移动开发中,底部导航栏是非常常见的,在之前我也写过一篇关于在Android中如何使用底部导航栏的博客,感兴趣的可以看一下。

下面,我们来看一下在Flutter 中 底部导航栏是如何使用的。

首先看一下效果图:

shifting模式

img

fixed模式

img

BottomNavigationBar

首先,bottomNavigationBar 是属于 Scaffold 中的一个位于底部的控件。通常和 BottomNavigationBarItem 配合使用

BottomNavigationBar构造方法

  BottomNavigationBar({
    Key key,
    @required this.items,  
    this.onTap,
    this.currentIndex = 0,
    BottomNavigationBarType type,
    this.fixedColor,
    this.iconSize = 24.0,
  })
123456789
属性值类型说明
itemsBottomNavigationBarItem类型的List底部导航栏的显示项
onTapValueChanged < int >点击导航栏子项时的回调
currentIndexint当前显示项的下标
typeBottomNavigationBarType底部导航栏的类型,有fixed和shifting两个类型,显示效果不一样
fixedColorColor底部导航栏type为fixed时导航栏的颜色,如果为空的话默认使用ThemeData.primaryColor
iconSizedoubleBottomNavigationBarItem icon的大小

BottomNavigationBar中属性比较简单,下面我们来看一下BottomNavigationBarItem

BottomNavigationBarItem

底部导航栏要显示的Item,有图标和标题组成


属性值类型说明
iconWidget要显示的图标控件,一般都是Iocn
titleWidget要显示的标题控件,一般都是Text
activeIconWidget选中时要显示的icon,一般也是Icon
backgroundColorColorBottomNavigationBarType为shifting时的背景颜色

简单使用

一般来说,点击底部导航栏都是要进行页面切换或者更新数据的,我们需要动态的改变一些状态,所以,我们要继承自StatefulWidget

class IndexPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _IndexState();
  }
}
123456

首先,我们需要准备导航栏要显示的项:

final List<BottomNavigationBarItem> bottomNavItems = [
    BottomNavigationBarItem(
      backgroundColor: Colors.blue,
      icon: Icon(Icons.home),
      title: Text("首页"),
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.green,
      icon: Icon(Icons.message),
      title: Text("消息"),
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.amber,
      icon: Icon(Icons.shopping_cart),
      title: Text("购物车"),
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.red,
      icon: Icon(Icons.person),
      title: Text("个人中心"),
    ),
  ];

以及点击导航项是要显示的页面:

 final pages = [HomePage(), MsgPage(), CartPage(), PersonPage()];

由于是演示,页面很简单,都是只放一个Text

import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text("首页"),
    );
  }
}

这些都准备完毕后,我们就可以开始使用底部导航栏了,首先我们要在Scaffold中使用bottomNavigationBar,然后指定items,currentIndex,type(默认是fixed)、onTap等属性

Scaffold(
      appBar: AppBar(
        title: Text("底部导航栏"),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: bottomNavItems,
        currentIndex: currentIndex,
        type: BottomNavigationBarType.shifting,
        onTap: (index) {
          _changePage(index);
        },
      ),
      body: pages[currentIndex],
    );
  }
123456789101112131415

这里我们主要看一下onTap,该属性接收一个方法回调,其中,index表示当前点击导航项的下标,也就是items的下标。
知道下标后,我们只需要更改currentIndex即可。

下面我们来看一下_changePage方法:

  /*切换页面*/
  void _changePage(int index) {
    /*如果点击的导航项不是当前项  切换 */
    if (index != currentIndex) {
      setState(() {
        currentIndex = index;
      });
    }
  }
123456789

如此一来,我们就实现了点击底部导航项切换页面的效果了,非常简单。

全部代码:

import 'package:flutter/material.dart';
import 'package:flutter_sample/widget/bottom_nav/cart_page.dart';
import 'package:flutter_sample/widget/bottom_nav/home_page.dart';
import 'package:flutter_sample/widget/bottom_nav/msg_page.dart';
import 'package:flutter_sample/widget/bottom_nav/person_page.dart';

class IndexPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _IndexState();
  }
}

class _IndexState extends State<IndexPage> {
  final List<BottomNavigationBarItem> bottomNavItems = [
    BottomNavigationBarItem(
      backgroundColor: Colors.blue,
      icon: Icon(Icons.home),
      title: Text("首页"),
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.green,
      icon: Icon(Icons.message),
      title: Text("消息"),
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.amber,
      icon: Icon(Icons.shopping_cart),
      title: Text("购物车"),
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.red,
      icon: Icon(Icons.person),
      title: Text("个人中心"),
    ),
  ];

  int currentIndex;

  final pages = [HomePage(), MsgPage(), CartPage(), PersonPage()];

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("底部导航栏"),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: bottomNavItems,
        currentIndex: currentIndex,
        type: BottomNavigationBarType.shifting,
        onTap: (index) {
          _changePage(index);
        },
      ),
      body: pages[currentIndex],
    );
  }

  /*切换页面*/
  void _changePage(int index) {
    /*如果点击的导航项不是当前项  切换 */
    if (index != currentIndex) {
      setState(() {
        currentIndex = index;
      });
    }
  }
}

效果图如下

img

一般情况下,我们底部导航栏不会弄得这么花哨,所以一般都是使用fixed模式,此时,导航栏的图标和标题颜色会使用fixedColor指定的颜色,如果没有指定fixedColor,则使用默认的主题色primaryColor

代码示例:

Scaffold(
      appBar: AppBar(
        title: Text("底部导航栏"),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: bottomNavItems,
        currentIndex: currentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          _changePage(index);
        },
      ),
      body: pages[currentIndex],
    );

入口函数:

/*入口函数*/
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter入门示例程序',
      theme: ThemeData(
        primaryColor: Colors.blue,
      ),
      home: IndexPage(),
    );
  }
}

img

好了,Flutter 底部导航栏就这些,还是很简单的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值