十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本文实例为大家分享了Flutter实现底部导航的具体代码,供大家参考,具体内容如下

BottomNavigationBar使用
底部导航栏 主文件 main.dart (注意导入文件路径)
import 'package:flutter/material.dart';
import './views/firstPage.dart';
import './views/secondPage.dart';
import './views/thirdPage.dart';
//首先导入三个界面
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State with TickerProviderStateMixin{
int _tabIndex = 0;
List _navigationViews;
var appBarTitles = ['首页', '发现', '我的'];
PageController pageController;
var _body;
initData() {
_body = new IndexedStack(
children: [new FirstPage(), new SecondPage(), new ThirdPage()],
index: _tabIndex,
);
}
@override
void initState() {
super.initState();
_navigationViews = [
new BottomNavigationBarItem(
icon: const Icon(Icons.home),
title: new Text(appBarTitles[0]),
backgroundColor: Colors.blue,
),
new BottomNavigationBarItem(
icon: const Icon(Icons.widgets),
title: new Text(appBarTitles[1]),
backgroundColor: Colors.blue,
),
new BottomNavigationBarItem(
icon: const Icon(Icons.person),
title: new Text(appBarTitles[2]),
backgroundColor: Colors.blue,
),
];
}
final navigatorKey = GlobalKey();
@override
Widget build(BuildContext context) {
initData();
return new MaterialApp(
navigatorKey: navigatorKey,
theme: new ThemeData(
primaryColor: Colors.blue,
accentColor: Colors.blue
),
home: new Scaffold(
appBar: new AppBar(
title: new Text(
appBarTitles[_tabIndex],
style: new TextStyle(color: Colors.white),
),
),
body: _body,
bottomNavigationBar: new BottomNavigationBar(
items: _navigationViews
.map((BottomNavigationBarItem navigationView) => navigationView)
.toList(),
currentIndex: _tabIndex,
type: BottomNavigationBarType.fixed,
onTap: (index) {
setState(() {
_tabIndex = index;
});
},
),
),
);
}
}