Flutter-底部切换栏的方式-BottomNavigationBar

第一步,准备工作

//指示器
  int currentIndex = 0;
  //定义显示的页面,自备点击的时候所显示的页面
  final pages = [TestOne(), TestTwo(), TestThree()];
  • 构建bottomNavItem,就是底部的按钮/文字这些东西。
final List<BottomNavigationBarItem> bottomNavItem = [
    BottomNavigationBarItem(
      icon: Icon(Icons.hail),
      label: '首页',
      activeIcon: Icon(Icons.handyman_outlined),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.hail),
      label: '中心',
      activeIcon: Icon(Icons.handyman_outlined),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      label: '我的',
      activeIcon: Icon(Icons.handyman_outlined),
    ),
  ];

第二步:在Scaffold里面,使用bottomNavigationBar

 bottomNavigationBar: BottomNavigationBar(
        items: bottomNavItem,
        onTap: (index) {
          setState(() {
            this.currentIndex = index;
          });
},
        currentIndex: currentIndex,
      ),

第三步,在body部分,将页面我们点击的索引值绑定。

body: pages[currentIndex],

展示完整代码

import 'package:flutter/material.dart';
import 'package:flutter_form_validator/Pages/practice_basic/bottomNavgationBar/test1.dart';
import 'package:flutter_form_validator/Pages/practice_basic/bottomNavgationBar/test2.dart';
import 'package:flutter_form_validator/Pages/practice_basic/bottomNavgationBar/test3.dart';

///@desc BottomNavigationBar
///@author @zhizhizang
///

class BottomNavgationBarPage extends StatefulWidget {
  @override
  _BottomNavgationBarPageState createState() => _BottomNavgationBarPageState();
}

class _BottomNavgationBarPageState extends State<BottomNavgationBarPage> {
  //指示器
  int currentIndex = 0;
  //定义显示的页面
  final pages = [TestOne(), TestTwo(), TestThree()];

  //抽离item项
  final List<BottomNavigationBarItem> bottomNavItem = [
    BottomNavigationBarItem(
      icon: Icon(Icons.hail),
      label: '首页',
      activeIcon: Icon(Icons.handyman_outlined),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.hail),
      label: '中心',
      activeIcon: Icon(Icons.handyman_outlined),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      label: '我的',
      activeIcon: Icon(Icons.handyman_outlined),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('bottomNavigationBar的使用'),
      ),
      body: pages[currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        items: bottomNavItem,
        onTap: (index) {
          setState(() {
            this.currentIndex = index;
          });
        },
        currentIndex: currentIndex,
      ),
    );
  }
}

展示图
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值