底部导航栏的制作|非静态页面的使用
效果
源代码
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59import 'package:flutter/material.dart';
import 'package:flutter_app2/message_page.dart';
import 'package:flutter_app2/contact_page.dart';
import 'package:flutter_app2/social_page.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: MyBottom(),
);
}
}
class MyBottom extends StatefulWidget {
_MyBottomState createState() => _MyBottomState();
}
class _MyBottomState extends State<MyBottom> {
int _curindex = 0; //当前页面索引
List<Widget> page = List(); //用List保存大量子页面
//重写State中的initState方法来初始化页面
void initState() {
//将子页面添加到page列表中
page..add(MessagePage())..add(ContactPage())..add(SocialPage());
super.initState();
}
Widget build(BuildContext context) {
return Scaffold(
body: page[_curindex],
//底部导航按钮栏
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(icon: Icon(Icons.mode_comment), title: Text("消息")),
BottomNavigationBarItem(icon: Icon(Icons.account_circle), title: Text("联系人"),),
BottomNavigationBarItem(icon: Icon(Icons.explore), title: Text("动态"))
],
//当前页面索引,按照items排序
//如果没有此属性,依然可以跳转,但是切换时底部导航栏颜色不会改变
currentIndex: _curindex,
//当点击底部按钮时,index会做出相应改变
onTap: (int index){
//状态对象调用setState(),告诉框架重绘widget
setState(() {
_curindex = index;
});
},
),
);
}
}message_page.dart
1 | import 'package:flutter/material.dart'; |
- contact_page.dart
1 | import 'package:flutter/material.dart'; |
- social.dart
1 | import 'package:flutter/material.dart'; |