您现在的位置是:首页  > 前端  > 后端  > Flutter Flutter

flutter-12( BottomNavigationBar 自定义底部导航条、以及实现页面切换)

2020-04-30【Flutter】1657人已围观

简介## 一、Flutter BottomNavigationBar 组件 `BottomNavigationBar` 是底部导航条,可以让我们定义底部 `Tab` 切换,`bottomNavigationBar` 是 `Scaffold` 组件的参数。 **BottomNavigationBar** 属性名 | 说明 ---|--- items | List 底部导航条按钮集合 iconSize | icon currentIndex | 默认选中第几个 onTap | 选中变

一、Flutter BottomNavigationBar 组件

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

BottomNavigationBar

属性名 说明
items List 底部导航条按钮集合
iconSize icon
currentIndex 默认选中第几个
onTap 选中变化回调函数
fixedColor 选中的颜色
type BottomNavigationBarType.fixed
BottomNavigationBarType.shifting

image

main.dart

  1. import 'package:flutter/material.dart';
  2. import 'pages/Tabs.dart';
  3. void main() => runApp(MyApp());
  4. class MyApp extends StatelessWidget {
  5. @override
  6. Widget build(BuildContext context) {
  7. return MaterialApp(
  8. home:Tabs()
  9. );
  10. }
  11. }

Tabs.dart

  1. import 'package:flutter/material.dart';
  2. import 'tabs/Home.dart';
  3. import 'tabs/Category.dart';
  4. import 'tabs/Setting.dart';
  5. class Tabs extends StatefulWidget {
  6. Tabs({Key key}) : super(key: key);
  7. _TabsState createState() => _TabsState();
  8. }
  9. class _TabsState extends State<Tabs> {
  10. int _currentIndex=0;
  11. List _pageList=[
  12. HomePage(),
  13. CategoryPage(),
  14. SettingPage(),
  15. ];
  16. @override
  17. Widget build(BuildContext context) {
  18. return Scaffold(
  19. appBar: AppBar(
  20. title: Text("Flutter Demo"),
  21. ),
  22. body: this._pageList[this._currentIndex],
  23. bottomNavigationBar: BottomNavigationBar(
  24. currentIndex: this._currentIndex, //配置对应的索引值选中
  25. onTap: (int index){
  26. setState(() { //改变状态
  27. this._currentIndex=index;
  28. });
  29. },
  30. iconSize:36.0, //icon的大小
  31. fixedColor:Colors.red, //选中的颜色
  32. type:BottomNavigationBarType.fixed, //配置底部tabs可以有多个按钮
  33. items: [
  34. BottomNavigationBarItem(
  35. icon: Icon(Icons.home),
  36. title: Text("首页")
  37. ),
  38. BottomNavigationBarItem(
  39. icon: Icon(Icons.category),
  40. title: Text("分类")
  41. ),
  42. BottomNavigationBarItem(
  43. icon: Icon(Icons.settings),
  44. title: Text("设置")
  45. )
  46. ],
  47. ),
  48. );
  49. }
  50. }

Home.dart

  1. import 'package:flutter/material.dart';
  2. class HomePage extends StatefulWidget {
  3. HomePage({Key key}) : super(key: key);
  4. _HomePageState createState() => _HomePageState();
  5. }
  6. class _HomePageState extends State<HomePage> {
  7. @override
  8. Widget build(BuildContext context) {
  9. return Text("我是首页组件");
  10. }
  11. }

Category.dart

  1. import 'package:flutter/material.dart';
  2. class CategoryPage extends StatefulWidget {
  3. CategoryPage({Key key}) : super(key: key);
  4. _CategoryPageState createState() => _CategoryPageState();
  5. }
  6. class _CategoryPageState extends State<CategoryPage> {
  7. @override
  8. Widget build(BuildContext context) {
  9. return Text("分类");
  10. }
  11. }

Setting.dart

  1. import 'package:flutter/material.dart';
  2. class SettingPage extends StatefulWidget {
  3. SettingPage({Key key}) : super(key: key);
  4. _SettingPageState createState() => _SettingPageState();
  5. }
  6. class _SettingPageState extends State<SettingPage> {
  7. @override
  8. Widget build(BuildContext context) {
  9. return ListView(
  10. children: <Widget>[
  11. ListTile(
  12. title: Text("我是一个文本"),
  13. ),
  14. ListTile(
  15. title: Text("我是一个文本"),
  16. ),
  17. ListTile(
  18. title: Text("我是一个文本"),
  19. ),
  20. ListTile(
  21. title: Text("我是一个文本"),
  22. )
  23. ],
  24. );
  25. }
  26. }


关注博客,更多精彩分享,敬请期待!
 

Tags:

很赞哦! (0)

我的名片

网名:随心

职业:PHP程序员

现居:湖北省-武汉市

Email:704061912@qq.com