Flutter中DefaultTabController或TabBar获取当前Tab页

使用自定义的TabController

参考:https://stackoverflow.com/questions/50123354/how-to-get-current-tab-index-in-flutter

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
class MyTabPage extends StatefulWidget {
const MyTabbedPage({Key key}) : super(key: key);
@override
_MyTabPageState createState() => new _MyTabPageState();
}

class _MyTabPageState extends State<MyTabPage>
with SingleTickerProviderStateMixin {
final List<Tab> myTabs = <Tab>[
new Tab(text: 'Android'),
new Tab(text: 'iOS'),
];

TabController _tabController;

@override
void initState() {
super.initState();
_tabController = new TabController(vsync: this, length: myTabs.length);
}

@override
void dispose() {
_tabController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
bottom: new TabBar(
controller: _tabController,
tabs: myTabs,
),
),
body: new TabBarView(
controller: _tabController,
children: myTabs.map((Tab tab) {
return new Center(child: new Text(tab.text));
}).toList(),
),
);
}

void _handleEvent() {
switch (_tabController.index) {

}
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :