Flutter實現(xiàn)底部導(dǎo)航
本文實例為大家分享了Flutter實現(xiàn)底部導(dǎo)航的具體代碼,供大家參考,具體內(nèi)容如下
BottomNavigationBar使用
底部導(dǎo)航欄 主文件 main.dart (注意導(dǎo)入文件路徑)
import 'package:flutter/material.dart';
import './views/firstPage.dart';
import './views/secondPage.dart';
import './views/thirdPage.dart';
//首先導(dǎo)入三個界面
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyApp> with TickerProviderStateMixin{
int _tabIndex = 0;
List<BottomNavigationBarItem> _navigationViews;
var appBarTitles = ['首頁', '發(fā)現(xiàn)', '我的'];
PageController pageController;
var _body;
initData() {
_body = new IndexedStack(
children: <Widget>[new FirstPage(), new SecondPage(), new ThirdPage()],
index: _tabIndex,
);
}
@override
void initState() {
super.initState();
_navigationViews = <BottomNavigationBarItem>[
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<NavigatorState>();
@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;
});
},
),
),
);
}
}
底部包含三個導(dǎo)航按鈕,分別對應(yīng)三個界面:
firstPage.dart
import 'package:flutter/material.dart';
class FirstPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new FirstPageState();
}
class FirstPageState extends State<FirstPage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Text('這是第一個界面'),
),
);
}
}
secondPage.dart
import 'package:flutter/material.dart';
class SecondPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => SecondPageState();
}
class SecondPageState extends State<SecondPage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Text("這是我第二個頁面"),
),
);
}
}
thirdPage.dart
import 'package:flutter/material.dart';
class ThirdPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => ThirdPageState();
}
class ThirdPageState extends State<ThirdPage>{
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Text('我是界面三'),
),
);
}
}
運行截圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- flutter實現(xiàn)更新彈窗內(nèi)容例子(親測有效)
- Flutter之自定義Dialog實現(xiàn)版本更新彈窗功能的實現(xiàn)
- Flutter沉浸式狀態(tài)欄/AppBar導(dǎo)航欄/仿咸魚底部凸起導(dǎo)航欄效果
- flutter仿微信底部圖標漸變功能的實現(xiàn)代碼
- Flutter實現(xiàn)仿微信底部菜單欄功能
- Flutter底部不規(guī)則導(dǎo)航的實現(xiàn)過程
- flutter BottomAppBar實現(xiàn)不規(guī)則底部導(dǎo)航欄
- Flutter實現(xiàn)底部導(dǎo)航欄
- Flutter實現(xiàn)底部菜單導(dǎo)航
- Flutter 底部彈窗ModelBottomSheet的使用示例
相關(guān)文章
Android 監(jiān)聽手機GPS打開狀態(tài)實現(xiàn)代碼
這篇文章主要介紹了Android 監(jiān)聽手機GPS打開狀態(tài)實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android邊播放邊緩存視頻框架AndroidVideoCache詳解
這篇文章主要為大家介紹了Android邊播放邊緩存視頻框架AndroidVideoCache詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Flutter網(wǎng)絡(luò)請求庫DIO的基本使用
這篇文章主要介紹了Flutter網(wǎng)絡(luò)請求庫DIO的基本使用,幫助大家更好的理解和學(xué)習(xí)使用Flutter,感興趣的朋友可以了解下2021-04-04
Kotlin ViewModelProvider.Factory的使用實例詳解
這篇文章主要介紹了Kotlin ViewModelProvider.Factory的使用,在我們使用 ViewModel 的時候,我們會發(fā)現(xiàn),有的時候我們需要用到 ViewModelFactory,有的時候不需要2023-02-02
Android 超詳細深刨Activity Result API的使用
這篇文章主要介紹了Android開發(fā)中Activity Result API的使用,掌握了它以后你就可以放棄startActivityForResult了,感興趣的朋友一起來看看吧2022-03-03
Android基于ViewFilpper實現(xiàn)文字LED顯示效果示例
這篇文章主要介紹了Android基于ViewFilpper實現(xiàn)文字LED顯示效果,結(jié)合完整實例形式分析了Android使用ViewFilpper實現(xiàn)文字LED顯示動畫效果的相關(guān)步驟與實現(xiàn)技巧,需要的朋友可以參考下2017-08-08
Android開發(fā)中PopupWindow用法實例分析
這篇文章主要介紹了Android開發(fā)中PopupWindow用法,結(jié)合實例形式分析了PopupWindow彈出窗口效果的使用技巧,需要的朋友可以參考下2016-02-02

