简体   繁体   中英

Flutter border radius TabBar indicator

in Flutter how can i draw border radius tab indicator like with below image?

在此处输入图片说明

below code can only draw circle indicator which i found in github .

TabBar(
  isScrollable: true,
  controller: _tabController,
  labelColor: Colors.black,
  tabs: <Widget>[
    Tab(
      text: 'DOTTED',
    ),
    Tab(
      text: 'Tab',
    ),
    Tab(
      text: 'INDICATOR',
    ),
  ],
  indicator: DotIndicator(
    indicatorColor: Colors.black,
    radius: 3,
  ),
  indicatorWeight: 2 * kDefaultDotIndicatorRadius,
),

library:

const kDefaultDotIndicatorRadius = 3.0;

class DotTabIndicator extends Decoration {
  const DotTabIndicator({this.indicatorColor, this.radius = kDefaultDotIndicatorRadius});

  final Color indicatorColor;
  final double radius;

  @override
  BoxPainter createBoxPainter([onChanged]) {
    return _DotPainter(this, onChanged);
  }
}

class _DotPainter extends BoxPainter {
  _DotPainter(this.decoration, VoidCallback onChanged) : super(onChanged);

  final DotTabIndicator decoration;

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    final center = configuration.size.center(offset);
    final dy = configuration.size.height;

    final newOffset = Offset(center.dx, dy - 8);

    final paint = Paint();
    paint.color = decoration.indicatorColor;
    paint.style = PaintingStyle.fill;

    canvas.drawCircle(newOffset, decoration.radius, paint);
  }
}

Check this out

class CustomIndicator extends StatelessWidget {

  var radius = Radius.circular(10);
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        appBar: AppBar(
          title: Text("Custom Indicator"),
          bottom: TabBar(
            tabs: <Widget>[
              Tab(
                text: "Hello",
              ),
              Tab(
                text: "Megan",
              ),
            ],
            indicator: ShapeDecoration(
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.only(topRight: radius, topLeft: radius)),
              color: Colors.red
            ),
          ),
        ),
        body: TabBarView(
          children: <Widget>[
            Container(
              child: Center(
                child: Text("Hello"),
              ),
            ),
            Container(
              child: Center(
                child: Text("Hi"),
              ),
            )
          ],
        ),
      ),
    );
  }
}

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM