Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Tabbarview underline color

Tags:

flutter

How to add underline to unselected tabs, like this:

https://ibb.co/mfkzKp

Here you can see it is gray colour for unselected tabs, and blue for selected.

like image 683
Kevin Janson Avatar asked Sep 08 '18 15:09

Kevin Janson


People also ask

How do you use TabController in flutter?

To create a tab in it, create a tab list and create an object of its TabController. TabController _tabController; Initalize the TabController inside the initState() method and also override it in the dispose() method. The CustomTabBar results can be seen in the image.


1 Answers

I haven't found any reference in the documentation about how to customize disabled indicator. However, you can build your own widget that will take additional decoration parameter:

class DecoratedTabBar extends StatelessWidget implements PreferredSizeWidget {
  DecoratedTabBar({@required this.tabBar, @required this.decoration});

  final TabBar tabBar;
  final BoxDecoration decoration;

  @override
  Size get preferredSize => tabBar.preferredSize;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned.fill(child: Container(decoration: decoration)),
        tabBar,
      ],
    );
  }
}

Then you can decorate your TabBar however you want:

appBar: AppBar(
  bottom: DecoratedTabBar(
    tabBar: TabBar(
      tabs: [
        // ...
      ],
    ),
    decoration: BoxDecoration(
      border: Border(
        bottom: BorderSide(
          color: Colors.blue,
          width: 2.0,
        ),
      ),
    ),
  ),
),

Which will result in desired behavior:

TabBar custom decoration

like image 131
tomwyr Avatar answered Sep 28 '22 09:09

tomwyr