热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

FlutterBottomNavigationBar自定义底部导航栏,实现页面切换

FlutterBottomNavigationBar组件BottomNavigationBar是底部导航条,可以让我们定义底部Tab切换,bottom

 


Flutter BottomNavigationBar组件

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


BottomNavigationBar常见属性


属性说明
itemsList 底部导航条按钮集合
iconSizeicon
currentIndex默认选中第几个
onTap选中变化回调函数
        onTap: (int index){
          setState(() {
            print("Tagwjlis index = ${index}");
            this._currentIndex = index;
          });
        },
fixedColor选中的颜色
type

BottomNavigationBarType.fixed    //配置底部tabs可以有多个按钮

BottomNavigationBarType.shifting

实现一个页面切换功能目录


main.dart

import 'package:flutter/material.dart';
import 'package:stack_align_positioned/pages/Tabs.dart';
import 'reg/listData.dart';
void main() {runApp(MyApp());
}class MyApp extends StatelessWidget {int _curentIndex = 0;// This widget is the root of your application.@overrideWidget build(BuildContext context) {return MaterialApp(home: Tabs(),);}}

Tabs.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:stack_align_positioned/main.dart';
import 'package:stack_align_positioned/pages/tabs/CategoryPages.dart';
import 'package:stack_align_positioned/pages/tabs/HomePage.dart';
import 'package:stack_align_positioned/pages/tabs/SettingsPages.dart';class Tabs extends StatefulWidget{@overrideState createState() {return _TabsState();}
}class _TabsState extends State{int _currentIndex = 0;List _listPageData = [ //页面的链表HomePages(),CategoryPages(),SettingsPages()];@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("Flutter BottomNavigationBar"),),body: this._listPageData[_currentIndex],bottomNavigationBar: BottomNavigationBar(currentIndex: this._currentIndex,//配置对应的索引值选中onTap: (int index){//index 表示选择选项setState(() {print("Tagwjlis index = ${index}");this._currentIndex = index;});},iconSize: 36.0, //icon的大小fixedColor: Colors.red, //选中颜色type: BottomNavigationBarType.fixed,//配置底部tabs可以有多个按钮items: [BottomNavigationBarItem(icon: Icon(Icons.home),title: Text("首页")),BottomNavigationBarItem(icon: Icon(Icons.category),title: Text("分类")),BottomNavigationBarItem(icon: Icon(Icons.settings),title: Text("设置"))],),);}
}


HomePages.dart

import 'package:flutter/cupertino.dart';class HomePages extends StatefulWidget{@overrideState createState() {return _HomeState();}}class _HomeState extends State{@overrideWidget build(BuildContext context) {return Center(child: Text("Home Text"),);}
}

CategoryPages.dart

import 'package:flutter/cupertino.dart';class CategoryPages extends StatefulWidget{@overrideState createState() {return _CategorysStae();}
}class _CategorysStae extends State{@overrideWidget build(BuildContext context) {return Center(child: Text("CategoryPages center"),);}
}

SettingsPages.dart

import 'package:flutter/cupertino.dart';class SettingsPages extends StatefulWidget{@overrideState createState() {return _SettingsStae();}
}class _SettingsStae extends State{@overrideWidget build(BuildContext context) {return Center(child: Text("SettingsPages center"),);}
}


推荐阅读
author-avatar
abc1733974979
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有