Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make tabs scrollable in flutter

Tags:

flutter

I've been developing android apps for a while now and I'm currently porting an android app to flutter which I started not long ago. I have been able to make tabs scrollable in android but I'm finding it difficult to do that in flutter. I am able to create the tabs but they are about 7 and they exceed the screen width. Hence I want to make it scrollable to avoid that. Below is what I did in the android app and I want to achieve something similar. Any help would be appreciated. Thanks.

enter image description here

like image 847
nivla360 Avatar asked Mar 26 '19 17:03

nivla360


People also ask

How do you make a tab controller in Flutter?

The selected tab's index can be changed with animateTo. A stateful widget that builds a TabBar or a TabBarView can create a TabController and share it directly. When the TabBar and TabBarView don't have a convenient stateful ancestor, a TabController can be shared by providing a DefaultTabController inherited widget.


1 Answers

You can use a DefaultTabController widget , also the Tab widget has a property called : isScrollable , set true and you will see. This is a basic sample:

   final List<Tab> myTabs = List.generate(
      10,
      (index) => Tab(text: 'TAB $index'),
    );

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

You can find more info here: https://docs.flutter.io/flutter/material/DefaultTabController-class.html

like image 171
diegoveloper Avatar answered Nov 13 '22 04:11

diegoveloper