当前位置:   article > 正文

flutter 顶部导航栏TabBarView自定义下划线的宽度和圆角_flutter tabbar下划线的宽度

flutter tabbar下划线的宽度

  flutter自带的appbar框架无法自定义下划线的宽度和圆角,需要自定义,如下

MyUnderlineTabIndicator

MyUnderlineTabIndicator 为UnderlineTabIndicator 的拷贝,修改了两处源码实现了下标的宽度固定和圆角功能

  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 = 32;
  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 =
  82. _indicatorRectFor(rect, textDirection).deflate(borderSide.width / 2.0);
  83. // 改为圆角
  84. final Paint paint = borderSide.toPaint()..strokeCap = StrokeCap.round;
  85. canvas.drawLine(indicator.bottomLeft, indicator.bottomRight, paint);
  86. }
  87. }

 使用案例:

 

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:trade_app/base/baseColor.dart';
import 'package:trade_app/base/baseSize.dart';
import 'package:trade_app/pages/utils/icon_utils.dart';
import 'package:trade_app/widgets/extend/my_underline_indicator.dart';

///参考 https://www.imooc.com/article/300411
class MineStencilPage extends StatefulWidget {
  @override
  MineStencilPageState createState() => MineStencilPageState();
}

//使用with SingleTickerProviderStateMixin 防止页面被回收
class MineStencilPageState extends State<MineStencilPage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(vsync: this, length: 2);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        brightness: Brightness.light,
        leading: IconButton(
            icon: Image.asset(IconUtils.getIconPath('fanhui')),
            onPressed: () => Navigator.pop(context)),
        title: Text(
          '打印配置',
          style: TextStyle(color: BaseColor.colorFF262626, fontSize: 18),
        ),
        backgroundColor: Colors.white,
        bottom: _tabBarTop(),
      ),
      body: Container(child: _tabBarView()),
      backgroundColor: BaseColor.colorFFF5F5F5,
    );
  }

  _tabBarView() => TabBarView(
          controller: _tabController,
          physics: NeverScrollableScrollPhysics(),
          children: <Widget>[
            Center(child: Text('烟草默认模版')),
            Center(child: Text('商超默认模版')),
          ]);

  _tabBarTop() => TabBar(
        tabs: <Widget>[
          Tab(text: '烟草默认模版'),
          Tab(text: '商超默认模版'),
        ],
        controller: _tabController,
        indicatorColor: BaseColor.colorFF03B798,
        indicatorWeight: 3,
        indicatorSize: TabBarIndicatorSize.label,
        indicator: MyUnderlineTabIndicator(
            borderSide: BorderSide(width: 3.0, color: BaseColor.colorFF03B798)),
        indicatorPadding: EdgeInsets.all(5),
        labelColor: BaseColor.colorFF262626,
        labelStyle: TextStyle(fontSize: BaseSize.sp(14)),
        unselectedLabelColor: BaseColor.colorFF999999,
        unselectedLabelStyle: TextStyle(fontSize: BaseSize.sp(14)),
      );
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/431841
推荐阅读
相关标签
  

闽ICP备14008679号