How can I customize the TabBar indicator width and height?

后端 未结 5 573
傲寒
傲寒 2020-12-30 13:08

I\'m using a TabBar widget and I\'d like to customize the height and width of the indicator. I can\'t see any other property besides color I can modify.

相关标签:
5条回答
  • 2020-12-30 13:23

    You can use property TabBar(indicatorWeight:detail Height).

    0 讨论(0)
  • 2020-12-30 13:34

    Give your TabBar a property of isScrollable: true if you don't want the tabs to expand to fill the screen horizontally the way they do by default.

    You can use a Container wrapped in a PreferredSize to size the TabBar. (The PreferredSize is only necessary if you want it to live in the bottom slot of an AppBar.) This has the effect of making the indicators appear narrower because the TabBar doesn't fill the screen. However, the indicator has a hard coded height. If you don't like it, you'll have to import your own copy of tabs.dart and customize the constants in that file.

    Note that you can also use a Container to set the height of the individual Tabs, although that doesn't seem like what you're trying to do.

    import 'package:flutter/material.dart';
    
    
    void main() {
      runApp(new MaterialApp(
        home: new MyApp(),
      ));
    }
    
    
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new DefaultTabController(
          length: 2,
          child: new Scaffold(
            appBar: new AppBar(
              title: new Text('Tabs Demo'),
              bottom: new PreferredSize(
                preferredSize: new Size(200.0, 200.0),
                child: new Container(
                  width: 200.0,
                  child: new TabBar(
                    tabs: [
                      new Container(
                        height: 200.0,
                        child: new Tab(text: 'hello'),
                      ),
                      new Container(
                        height: 200.0,
                        child: new Tab(text: 'world'),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            // body: ...
          ),
        );
      }
    
    }
    
    0 讨论(0)
  • 2020-12-30 13:40

    You can use indicatorSize: TabBarIndicatorSize.label on the TabBar to make the indicator the same size as the label.

    Or you could set the indicator directly, this is a Decoration which you can customize:

    AppBar(
        bottom: TabBar(
            indicator: UnderlineTabIndicator(
              borderSide: BorderSide(width: 5.0),
              insets: EdgeInsets.symmetric(horizontal:16.0)
            ),
            tabs: [
              Tab(text: 'tab 1'),
              Tab(text: 'tab 2'),
              Tab(text: 'tab 3'),
            ],
         ),
    );
    

    For more customisation options check this post

    0 讨论(0)
  • 2020-12-30 13:45

    You can adjust the spacing between the tabs -> labelPadding: EdgeInsets.symmetric (horizontal: 5),

    0 讨论(0)
  • 2020-12-30 13:45

    In the same way as this answer https://stackoverflow.com/a/44273493/5938089, The best is to use containers, but I will only use one.

    For a change, I will use a bottom bar

    bottomNavigationBar: new Material(
        color: Colors.teal,
        child: new Container(
          height: 60.0,
          child: new TabBar(
            controller: controller,
            tabs: <Widget>[
                new Tab(icon: new Icon(Icons.access_alarm)),
                new Tab(icon: new Icon(Icons.account_balance)),
            ]
          ),
        )
      ),
    

    Screen Shot

    0 讨论(0)
提交回复
热议问题