自定义TabBar
# Flutter自定义TabBar
# 1. 创建tab_navigator.dart文件
import 'package:flutter/material.dart';
import 'package:flutter_trip/pages/home_page.dart';
import 'package:flutter_trip/pages/my_page.dart';
import 'package:flutter_trip/pages/search_page.dart';
import 'package:flutter_trip/pages/travel_page.dart';
import 'package:flutter_trip/utils/theme_color.dart';
class TabNavigator extends StatefulWidget {
const TabNavigator({Key? key}) : super(key: key);
_TabNavigatorState createState() => _TabNavigatorState();
}
class _TabNavigatorState extends State<TabNavigator> {
// 默认字体颜色
final _defaultColor = WZThemeColor.text2Color;
// 选中颜色
final _activeColor = WZThemeColor.primaryColor;
// 页面控制器
final PageController _controller = PageController(initialPage: 0);
// 当前界面索引记录
int _currentIndex = 0;
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
// PageView组件
controller: _controller,
// 控制器绑定
children: const [HomePage(), SearchPage(), TravelPage(), MyPage()],
// 子页面列表
onPageChanged: (int valueChanged) {
// 监听界面改变
setState(() {
if (_currentIndex != valueChanged) {
// 当界面改变时改变当前界面索引
_currentIndex = valueChanged;
}
});
}),
// 底部导航栏
bottomNavigationBar: BottomNavigationBar(
// 关联当前相应索引
currentIndex: _currentIndex,
// 监听点击事件
onTap: (index) {
// 页面控制器跳转到点击的界面
_controller.jumpToPage(index);
// 设置当前选中索引
setState(() {
_currentIndex = index;
});
},
// BottomNavigationBarType有两种类型,fixed:一直显示文字;shifting:仅选中的Item显示文字
type: BottomNavigationBarType.fixed,
// 设置Item选中的颜色
selectedItemColor: _activeColor,
// 设置Item选中的字体
selectedFontSize: 12.0,
// 设置未选中Item的颜色
unselectedItemColor: _defaultColor,
// 子选项
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home, color: _defaultColor),
activeIcon: Icon(Icons.home, color: _activeColor),
label: '首页'),
BottomNavigationBarItem(
icon: Icon(Icons.search, color: _defaultColor),
activeIcon: Icon(Icons.search, color: _activeColor),
label: '搜索'),
BottomNavigationBarItem(
icon: Icon(Icons.camera_alt, color: _defaultColor),
activeIcon: Icon(Icons.camera_alt, color: _activeColor),
label: '旅拍'),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle, color: _defaultColor),
activeIcon: Icon(Icons.account_circle, color: _activeColor),
label: '我的')
],
),
);
}
}
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 2. 在main.dart中引入tab_navigator.dart
import 'package:flutter/material.dart';
import 'navigator/tab_navigator.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
highlightColor: const Color.fromRGBO(0, 0, 0, 0),
splashColor: const Color.fromRGBO(0, 0, 0, 0)),
home: const TabNavigator(),
);
}
}
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
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
上次更新: 10/29/2021, 4:56:25 PM