Flutter组件学习九-BottomNavigationBar(底部导航栏)

1、BottomNavigationBar

是底部导航条,可以让我们定义底部Tab切换,bottomNavigationBar是Scaffold组件的参数

1.1、属性
属性说明
itemsList底部导航条按钮集合
iconSize图标大小
currentIndex默认选中第几个
onTap选中变化回调函数
fixedColor选中的颜色
typeBottomNavigationBarType.fixed
BottomNavigationBarType.shifting
1.2、代码实现

MyBottomNavigationBar

import 'package:flutter/material.dart';
import 'package:flutter_app/pages/Home.dart';
import 'package:flutter_app/pages/CategoryPage.dart';
import 'package:flutter_app/pages/SettingPage.dart';

class MyBottomNavigationBar extends StatefulWidget {
  @override
  _MyBottomNavigationBarState createState() => _MyBottomNavigationBarState();
}

class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {
  // 当前显示页面的index
  int _currentIndex = 0;

  // 页面集合
  List _pageList=[
    HomePage(),
    CategoryPage(),
    SettingPage(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 设置title
      appBar: AppBar(title: Text("jeklsjflk")),
      body: this._pageList[this._currentIndex],
      bottomNavigationBar: BottomNavigationBar(
          currentIndex:this._currentIndex,
          onTap: (int index){
            setState(() {
              this._currentIndex = index;
            });
          },
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
              title: Text('首页'),
              icon: Icon(Icons.home),
            ),
            BottomNavigationBarItem(
              title: Text('分类'),
              icon: Icon(Icons.category),
            ),
            BottomNavigationBarItem(
              title: Text('设置'),
              icon: Icon(Icons.settings),
            ),
          ]),
    );
  }
}

pages/CategoryPage.dart

import 'package:flutter/material.dart';

class CategoryPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("CategoryPage"),
    );
  }
}

pages/Home.dart

import 'package:flutter/material.dart';

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

pages/SettingPage.dart

import 'package:flutter/material.dart';

class SettingPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("SettingPage"),
    );
  }
}

main.dart

import 'package:flutter/material.dart';
import 'package:flutter_app/MyBottomNavigationBar.dart';

void main() => runApp(MyMaterialApp());

class MyMaterialApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: "test",
        home: MyBottomNavigationBar(),
    );
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值