Flutter设置TabBar indicator宽度(爆改UnderlineTabIndicator )

冷不防 2021-06-06 15:29 1538阅读 0赞

在默认的TabBar中,indicator的宽度是不可以修改的,每个长度都是平分一个Item的,如下图:

在这里插入图片描述

这个是未修改的图,下面是修改的图,总的宽度为20

在这里插入图片描述

总的在源码里翻来翻去,发现并没有可以设置Indicator的字段,这时候,尝试着通过源码来修改indicator的宽度,同时也是借助了这篇文章,不过,给的有些随意,故在这记录之。

源码探索:

点进TabBar的源码

  1. const TabBar({
  2. Key key,
  3. @required this.tabs,
  4. this.controller,
  5. this.isScrollable = false,
  6. this.indicatorColor,
  7. this.indicatorWeight = 2.0,
  8. this.indicatorPadding = EdgeInsets.zero,
  9. this.indicator, //这是我们要修改的indicator
  10. this.indicatorSize,
  11. this.labelColor,
  12. this.labelStyle,
  13. this.labelPadding,
  14. this.unselectedLabelColor,
  15. this.unselectedLabelStyle,
  16. this.dragStartBehavior = DragStartBehavior.start,
  17. this.onTap,
  18. }) : assert(tabs != null),
  19. assert(isScrollable != null),
  20. assert(dragStartBehavior != null),
  21. assert(indicator != null || (indicatorWeight != null && indicatorWeight > 0.0)),
  22. assert(indicator != null || (indicatorPadding != null)),
  23. super(key: key);

再点进去indicator的源码,发现了有一个默认的UnderlineTabIndicator

在这里插入图片描述

这时候我们再点进去默认的UnderlineTabIndicator,看下是否有进行修改宽度的地方。

刚好,我们看到了一个print输出,输出的是一个Rect,这个可能恰好是我们说要修改的。

在这里插入图片描述

修改:

这时候我们将整个UnderlineTabIndicatorcopy过来,重命名为MyUnderlineTabIndicator,为了避免与官方相冲突。再对上面截图的地方进行修改,下面是总的源码
my_underline_indicator.dart

  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/widgets.dart';
  6. /// Used with [TabBar.indicator] to draw a horizontal line below the
  7. /// selected tab.
  8. ///
  9. /// The selected tab underline is inset from the tab's boundary by [insets].
  10. /// The [borderSide] defines the line's color and weight.
  11. ///
  12. /// The [TabBar.indicatorSize] property can be used to define the indicator's
  13. /// bounds in terms of its (centered) widget with [TabIndicatorSize.label],
  14. /// or the entire tab with [TabIndicatorSize.tab].
  15. class MyUnderlineTabIndicator extends Decoration {
  16. /// Create an underline style selected tab indicator.
  17. ///
  18. /// The [borderSide] and [insets] arguments must not be null.
  19. const MyUnderlineTabIndicator({
  20. this.borderSide = const BorderSide(width: 2.0, color: Colors.white),
  21. this.insets = EdgeInsets.zero,
  22. }) : assert(borderSide != null),
  23. assert(insets != null);
  24. /// The color and weight of the horizontal line drawn below the selected tab.
  25. final BorderSide borderSide;
  26. /// Locates the selected tab's underline relative to the tab's boundary.
  27. ///
  28. /// The [TabBar.indicatorSize] property can be used to define the
  29. /// tab indicator's bounds in terms of its (centered) tab widget with
  30. /// [TabIndicatorSize.label], or the entire tab with [TabIndicatorSize.tab].
  31. final EdgeInsetsGeometry insets;
  32. @override
  33. Decoration lerpFrom(Decoration a, double t) {
  34. if (a is UnderlineTabIndicator) {
  35. return UnderlineTabIndicator(
  36. borderSide: BorderSide.lerp(a.borderSide, borderSide, t),
  37. insets: EdgeInsetsGeometry.lerp(a.insets, insets, t),
  38. );
  39. }
  40. return super.lerpFrom(a, t);
  41. }
  42. @override
  43. Decoration lerpTo(Decoration b, double t) {
  44. if (b is MyUnderlineTabIndicator) {
  45. return MyUnderlineTabIndicator(
  46. borderSide: BorderSide.lerp(borderSide, b.borderSide, t),
  47. insets: EdgeInsetsGeometry.lerp(insets, b.insets, t),
  48. );
  49. }
  50. return super.lerpTo(b, t);
  51. }
  52. @override
  53. _MyUnderlinePainter createBoxPainter([ VoidCallback onChanged ]) {
  54. return _MyUnderlinePainter(this, onChanged);
  55. }
  56. }
  57. class _MyUnderlinePainter extends BoxPainter {
  58. _MyUnderlinePainter(this.decoration, VoidCallback onChanged)
  59. : assert(decoration != null),
  60. super(onChanged);
  61. final MyUnderlineTabIndicator decoration;
  62. BorderSide get borderSide => decoration.borderSide;
  63. EdgeInsetsGeometry get insets => decoration.insets;
  64. Rect _indicatorRectFor(Rect rect, TextDirection textDirection) {
  65. assert(rect != null);
  66. assert(textDirection != null);
  67. final Rect indicator = insets.resolve(textDirection).deflateRect(rect);
  68. //希望的宽度
  69. double wantWidth = 20;
  70. //取中间坐标
  71. double cw = (indicator.left + indicator.right) / 2;
  72. return Rect.fromLTWH(cw - wantWidth / 2,
  73. indicator.bottom - borderSide.width, wantWidth, borderSide.width);
  74. }
  75. @override
  76. void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
  77. assert(configuration != null);
  78. assert(configuration.size != null);
  79. final Rect rect = offset & configuration.size;
  80. final TextDirection textDirection = configuration.textDirection;
  81. final Rect indicator = _indicatorRectFor(rect, textDirection).deflate(borderSide.width / 2.0);
  82. final Paint paint = borderSide.toPaint()..strokeCap = StrokeCap.square;
  83. canvas.drawLine(indicator.bottomLeft, indicator.bottomRight, paint);
  84. }
  85. }

使用:

  1. Container(
  2. height: 46,
  3. child: TabBar(
  4. indicatorColor: MyColorRes.primaryColor,
  5. labelColor: MyColorRes.tvMainSelectColor,
  6. unselectedLabelColor: MyColorRes.tvMainUnSelectColor,
  7. unselectedLabelStyle: TextStyle(fontSize: 15),
  8. labelStyle: TextStyle(fontSize: 17),
  9. isScrollable: true,
  10. indicator: MyUnderlineTabIndicator(borderSide: BorderSide(width: 2.0, color: MyColorRes.primaryColor)),
  11. controller: _tabController,
  12. tabs: mTabs.map((value) {
  13. return Text(value.name);
  14. }).toList(),
  15. ),
  16. alignment: Alignment.centerLeft,
  17. color: MyColorRes.bg_white,
  18. ),

发表评论

表情:
评论列表 (有 0 条评论,1538人围观)

还没有评论,来说两句吧...

相关阅读