flutter实现底部导航栏切换 本文实例为大家分享了flutter实现底部导航栏切换的具体代码,供大家参考,具体内容如下 思路:MaterialApp是提供了bottomnavigationbar的,可以使用,这个已经提供了的widget,再利用每次点击tab的时候使用set state方法来更新屏幕,切换
本文实例为大家分享了flutter实现底部导航栏切换的具体代码,供大家参考,具体内容如下
思路:MaterialApp是提供了bottomnavigationbar的,可以使用,这个已经提供了的widget,再利用每次点击tab的时候使用set state方法来更新屏幕,切换中间的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实现底部导航栏切换
基础教程推荐
猜你喜欢
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- iOS开发使用XML解析网络数据 2022-11-12
- iOS开发 全机型适配解决方法 2023-01-14
- Android Compose自定义TextField实现自定义的输入框 2023-05-13
- IOS获取系统相册中照片的示例代码 2023-01-03
- Flutter进阶之实现动画效果(三) 2022-10-28
- Android开发Compose集成高德地图实例 2023-06-15
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18
- Android实现短信验证码输入框 2023-04-29
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07