Search code examples
flutterflutter-layouttabbarindicator

Flutter Tab Bar Indicator: How to change the vertical position of the indicator?


I am working on this Tab Bar for a while, it now looks like this:

currect appbar, and here is my code now:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        highlightColor: Colors.transparent,
        splashColor: Colors.transparent,
        hoverColor: Colors.transparent,
      ),
      title: 'Flutter Demo',
      home: DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            leading: Icon(
              Icons.menu,
              color: Colors.black,
            ),
            actions: [
              Icon(
                Icons.settings,
                color: Colors.black,
              ),
            ],
            automaticallyImplyLeading: false,
            backgroundColor: Colors.white,
            title: Padding(
              padding: EdgeInsets.only(left: 50, right: 50),
              child:TabBar(
              indicatorColor: Colors.pink[100],
              indicatorSize: TabBarIndicatorSize.label,  
              tabs: [
                Tab(text: 'Dogs'),
                Tab(text: 'Cats'),
              ],
              labelColor: Colors.black,
            ),),
          ), //appbar
          body: TabBarView(
            children: [
              Center(child: Text('DOGS')),
              Center(child: Text('CATS')),
            ],
          ),
        ),
      ),
    );
  }
}

It is clear to see that the position of the indicator is not quite right. Ideally, I would like the indicator to vertically move up, closer to (and under) the text("Dogs" && "Cats"), or vertically move down, stay at the bottom of the app bar. How could I achieve that? Any help is appreciated.


Solution

  • You can controls those things using more parameters of the TabBar widget.

    I have added this,

    indicatorWeight: 4,
    indicatorPadding: EdgeInsets.symmetric(vertical: 8),
    

    and I got this,

    enter image description here

    If you want it much closer, play with the vertical value of indicatorPadding.
    If you want it thicker or thinner, play with the indicatorWeight.