flutter的tab标签页和视图组件代码片
Easul Lv4
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
class _TabTestState extends State<TabTest> with TickerProviderStateMixin {
final List<String> _tabs = ["首页", "动画"];
late TabController _controller;

@override
void initState() {
_controller = TabController(length: _tabs.length, vsync: this);
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"若轻视频",
style: TextStyle(color: Colors.white),
),
actions: const [],
bottom: PreferredSize(
preferredSize: const Size.fromHeight(40),
child: Align(
alignment: Alignment.centerLeft,
child: TabBar(
controller: _controller,
isScrollable: true,
padding: const EdgeInsets.only(left: 10.0, right: 10.0),
// 使用_tabs创建顶部标签栏
tabs: _tabs
.map((e) => Tab(
child: Text(
e,
style: const TextStyle(color: Colors.white),
)))
.toList(),
),
),
)),
// 创建标签栏对应的不同视图
body: TabBarView(
controller: _controller,
children: _tabs.map((e) {
// KeepAliveWrapper用于页面状态保存
return KeepAliveWrapper(child: Text(e));
}).toList(),
));
}
}

KeepAliveWrapper参考

 评论