您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“flutter如何實(shí)現(xiàn)底部導(dǎo)航欄切換”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“flutter如何實(shí)現(xiàn)底部導(dǎo)航欄切換”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
思路:MaterialApp是提供了bottomnavigationbar的,可以使用,這個(gè)已經(jīng)提供了的widget,再利用每次點(diǎn)擊tab的時(shí)候使用set state方法來(lái)更新屏幕,切換中間的body的widget;
main文件:
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_app1/MyBottomNavigationBar.dart'; void main(){ runApp(new MyApp()); } class MyApp extends StatelessWidget{ @override Widget build(BuildContext context) { // TODO: implement build // throw UnimplementedError(); return new MaterialApp( title:" MyNavigationBar", home: new MyBottomNavigationBar(), ); } }
MyBottomNavigationBar():
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_app1/pages/AddScreen.dart'; import 'package:flutter_app1/pages/HomeScreen.dart'; import 'package:flutter_app1/pages/PersonScreen.dart'; class MyBottomNavigationBar extends StatefulWidget{ @override MyNavigationBarState createState() { // TODO: implement createState // throw UnimplementedError(); return new MyNavigationBarState(); } } class MyNavigationBarState extends State<MyBottomNavigationBar>{ List<Widget> pagesList=[]; int cunrrentIndex=0; @override void initState() { pagesList=pagesList..add(new HomeScreen()) ..add(new AddScreen()) ..add(new PersonScreen()); } @override Widget build(BuildContext context) { // TODO: implement build // throw UnimplementedError(); return new Scaffold( appBar: new AppBar( title: new Text("MyNavigationBar"), ), body: pagesList[cunrrentIndex], bottomNavigationBar: new BottomNavigationBar( items: [ new BottomNavigationBarItem( icon: new Icon(Icons.home), label:"Home" ), new BottomNavigationBarItem( icon: new Icon(Icons.add), label:"Add" ), new BottomNavigationBarItem( icon: new Icon(Icons.person), label:"Person" ) ], onTap:(index){ setState(() { cunrrentIndex=index; }); }, currentIndex: cunrrentIndex, ) ); } }
addScreen:
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class AddScreen extends StatelessWidget{ @override Widget build(BuildContext context) { // TODO: implement build // throw UnimplementedError(); return new Scaffold( appBar: new AppBar( title: new Text("AddPage"), ), body: new Center( child: new Text("Add"), ), ); } }
HomeScreen
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class HomeScreen extends StatelessWidget{ @override Widget build(BuildContext context) { // TODO: implement build // throw UnimplementedError(); return new Scaffold( appBar: new AppBar( title: new Text("HomePage"), ), body: new Center( child:Image.asset("images/cat.png"), ), ); } }
PersonScreen :
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class PersonScreen extends StatelessWidget{ @override Widget build(BuildContext context) { // TODO: implement build // throw UnimplementedError(); return new Scaffold( appBar: new AppBar( title: new Text("PersonPage"), ), body: new Center( child: new Text("Person"), ), ); } }
最終效果:
讀到這里,這篇“flutter如何實(shí)現(xiàn)底部導(dǎo)航欄切換”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。