Если вы обернетесь Scaffold
внутри Builder
, вы сможете получить доступ к DefaultTabController
в пределах context
.Затем вы можете получить индекс вкладки с помощью DefaultTabController.of(context).index
.
Widget build(BuildContext context) {
return new MaterialApp(
title: 'pari',
debugShowCheckedModeBanner: false,
theme: widget._themeData,
home: DefaultTabController(
length: 4,
child: Builder(builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('pari'),
bottom: TabBar(
isScrollable: true,
tabs: [Text('0'), Text('1'), Text('2'), Text('3')]),
),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: () {
print(
'Current Index: ${DefaultTabController.of(context).index}');
},
),
);
}),
),
);
}