In the onPressed for my fab, I want to know the index of the tab that is currently selected in my DefaultTabController. How do I do this?
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'pari',
debugShowCheckedModeBanner: false,
theme: widget._themeData,
home: DefaultTabController(
length: widget._tabs.length,
child: Scaffold(
appBar: AppBar(
title: Text('pari'),
bottom: TabBar(
isScrollable: true,
tabs: widget._tabs,
),
),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: addWagerTap,
),
)
),
);
}
Tab bar controllers are implemented by the UITabBarController class. They allow a user of to switch between multiple arbitrary view controllers by maintaining an array of UIViewControllers .
DefaultTabController is an inherited widget that is used to share a TabController with a TabBar or a TabBarView. It's used when sharing an explicitly created TabController isn't convenient because the tab bar widgets are created by a stateless parent widget or by different parent widgets.
Use IgnorePointer. It will disable the click action of tabs.
The whole point of DefaultTabController is for it to manage tabs by itself. If you want some custom tab management, use TabController instead. With TabController you have access to much more informations, including the current index.
Just apply a listener on the TabController. Show activity on this post. Use DefaultTabController you can get current index easily whether the user changes tabs by swiping or tap on the tab bar.
If TabController is not provided, then the value of DefaultTabController will be used. tabs: This property we will pass the list of Tabs how many we need to display. The length of tabs list must equal to the controller's (TabController.length/DefaultTabController.length) and also TabView children length
DefaultTabController & TabBar (Flutter Widget of the Week) DefaultTabController is an inherited widget that is used to share a TabController with a TabBar or a TabBarView. It's used when sharing an explicitly created TabController isn't convenient because the tab bar widgets are created by a stateless parent widget or by different parent widgets.
If you wrap your Scaffold
inside of a Builder
, you'll be able to access your DefaultTabController
within the proper context
. You can then retrieve the tab index with 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}');
},
),
);
}),
),
);
}
You can also get the index of the tab by using the onTap property of the DefaultTabController.
Widget build(BuildContext context) {
return new MaterialApp(
title: 'pari',
debugShowCheckedModeBanner: false,
theme: widget._themeData,
home: DefaultTabController(
length: widget._tabs.length,
child: Scaffold(
appBar: AppBar(
title: Text('pari'),
bottom: TabBar(
isScrollable: true,
onTap: (int index) {
print('index is $index');
}
tabs: widget._tabs,
),
),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: addWagerTap,
),
)
),
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With