Flutter自定义TabBar

custom_tabbar.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import 'package:ajbaby/res/colors.dart';
import 'package:flutter/material.dart';


// ignore: must_be_immutable
class CustomTabBar extends StatefulWidget implements PreferredSizeWidget {
Color containerBgColor;
Color labelColor;
Color unselectedLabelColor;
Color indicatorColor;
TabController tabController;
List<Widget> tabsList = new List<Widget>();
List<Widget> tabBarViewsList = new List<Widget>();

// @required声明必传参数
CustomTabBar({
Key key,
@required this.tabsList,
@required this.tabBarViewsList,
@required this.tabController,
this.containerBgColor = YColors.color_FFFFFF,
this.labelColor = YColors.color_FF5F6D,
this.unselectedLabelColor = YColors.color_858585,
this.indicatorColor = YColors.color_FF5F6D,
}) : super(key: key);


@override
_CustomTabBarState createState() {
return _CustomTabBarState();
}


@override
// TODO: implement preferredSize
Size get preferredSize => null;
}


class _CustomTabBarState extends State<CustomTabBar> with AutomaticKeepAliveClientMixin {
@override
void initState() {
super.initState();
}


@override
void dispose() {
super.dispose();
// widget.tabController.dispose();
}


@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
appBar: PreferredSize(
child: Container(
color: widget.containerBgColor,
child: TabBar(
isScrollable: true,
controller: widget.tabController,
tabs: widget.tabsList,
labelColor: widget.labelColor,
unselectedLabelColor: widget.unselectedLabelColor,
indicatorColor: widget.indicatorColor,
),
),
preferredSize: Size.fromHeight(kToolbarHeight)),
body: TabBarView(
controller: widget.tabController,
children: widget.tabBarViewsList,
),
);
}


@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}

引用

1
2
3
4
5
body: CustomTabBar(
tabsList: _tabsList,
tabBarViewsList: _tabBarViewsList,
tabController: _tabController,
),
坚持原创技术分享,您的支持将鼓励我继续创作!