【问题标题】:Flutter border radius TabBar indicatorFlutter 边框半径 TabBar 指示器
【发布时间】:2020-04-02 17:26:50
【问题描述】:

Flutter 中,我怎样才能像下图一样绘制边框半径选项卡指示器?

以下代码只能绘制我在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,
),

图书馆:

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);
  }
}

【问题讨论】:

  • 在哪里调用了DotTabIndicator 构造函数?
  • @pskink 此代码是另一个库的示例,该库用于制作点制表符
  • 你为什么不使用ShapeDecoration而不是另一个库?
  • @pskink 我不知道如何为标签指示器绘制这个形状
  • indicator: ShapeDecoration(...)

标签: flutter flutter-layout


【解决方案1】:

看看这个

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"),
              ),
            )
          ],
        ),
      ),
    );
  }
}

输出:

【讨论】:

  • 我想要标签指示器而不是标签装饰:)
  • 那是红色的指标
  • @DolDurma 立即查看
  • 我很抱歉这是我需要的 https://imgur.com/a/tLHXdWk 因为我自己的错误我接受并支持你的帖子
猜你喜欢
  • 2021-06-22
  • 2021-01-20
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 2020-12-31
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
相关资源
最近更新 更多