Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current tab of DefaultTabController

Tags:

flutter

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,
          ),
        )
      ),
    );
  }
like image 893
dakamojo Avatar asked Sep 25 '18 16:09

dakamojo


People also ask

What is a tab controller?

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 .

What is DefaultTabController in flutter?

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.

How do I turn off my tab flutter?

Use IgnorePointer. It will disable the click action of tabs.

What is the difference between defaulttabcontroller and tabcontroller?

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.

How to get Current Index of current tab in tabcontroller?

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.

What to do if tabcontroller is not provided?

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

What is the use of defaulttabcontroller in flutter?

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.


2 Answers

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}');
              },
            ),
          );
        }),
      ),
    );
  }
like image 51
Albert Lardizabal Avatar answered Oct 19 '22 00:10

Albert Lardizabal


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,
        ),
      )
    ),
  );
}



like image 34
Blondie Avatar answered Oct 19 '22 01:10

Blondie