tab_indicator.dart 4.27 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';

import 'colors.dart';

/// Used with [TabBar.indicator] to draw a horizontal line below the
/// selected tab.
///
/// The selected tab underline is inset from the tab's boundary by [insets].
/// The [borderSide] defines the line's color and weight.
///
/// The [TabBar.indicatorSize] property can be used to define the indicator's
16 17
/// bounds in terms of its (centered) widget with [TabBarIndicatorSize.label],
/// or the entire tab with [TabBarIndicatorSize.tab].
18 19 20 21 22
class UnderlineTabIndicator extends Decoration {
  /// Create an underline style selected tab indicator.
  ///
  /// The [borderSide] and [insets] arguments must not be null.
  const UnderlineTabIndicator({
23
    this.borderRadius,
24 25
    this.borderSide = const BorderSide(width: 2.0, color: Colors.white),
    this.insets = EdgeInsets.zero,
26
  });
27

28 29 30 31 32 33
  /// The radius of the indicator's corners.
  ///
  /// If this value is non-null, rounded rectangular tab indicator is
  /// drawn, otherwise rectangular tab indictor is drawn.
  final BorderRadius? borderRadius;

34 35 36 37 38
  /// The color and weight of the horizontal line drawn below the selected tab.
  final BorderSide borderSide;

  /// Locates the selected tab's underline relative to the tab's boundary.
  ///
39 40 41 42
  /// The [TabBar.indicatorSize] property can be used to define the tab
  /// indicator's bounds in terms of its (centered) tab widget with
  /// [TabBarIndicatorSize.label], or the entire tab with
  /// [TabBarIndicatorSize.tab].
43 44 45
  final EdgeInsetsGeometry insets;

  @override
46
  Decoration? lerpFrom(Decoration? a, double t) {
47
    if (a is UnderlineTabIndicator) {
48
      return UnderlineTabIndicator(
49
        borderSide: BorderSide.lerp(a.borderSide, borderSide, t),
50
        insets: EdgeInsetsGeometry.lerp(a.insets, insets, t)!,
51 52 53 54 55 56
      );
    }
    return super.lerpFrom(a, t);
  }

  @override
57
  Decoration? lerpTo(Decoration? b, double t) {
58
    if (b is UnderlineTabIndicator) {
59
      return UnderlineTabIndicator(
60
        borderSide: BorderSide.lerp(borderSide, b.borderSide, t),
61
        insets: EdgeInsetsGeometry.lerp(insets, b.insets, t)!,
62 63 64 65 66 67
      );
    }
    return super.lerpTo(b, t);
  }

  @override
68
  BoxPainter createBoxPainter([ VoidCallback? onChanged ]) {
69
    return _UnderlinePainter(this, borderRadius, onChanged);
70 71 72 73
  }

  Rect _indicatorRectFor(Rect rect, TextDirection textDirection) {
    final Rect indicator = insets.resolve(textDirection).deflateRect(rect);
74
    return Rect.fromLTWH(
75 76 77 78 79 80 81
      indicator.left,
      indicator.bottom - borderSide.width,
      indicator.width,
      borderSide.width,
    );
  }

82 83
  @override
  Path getClipPath(Rect rect, TextDirection textDirection) {
84 85 86 87 88
    if (borderRadius != null) {
      return Path()..addRRect(
        borderRadius!.toRRect(_indicatorRectFor(rect, textDirection))
      );
    }
89 90 91 92 93
    return Path()..addRect(_indicatorRectFor(rect, textDirection));
  }
}

class _UnderlinePainter extends BoxPainter {
94 95 96 97
  _UnderlinePainter(
    this.decoration,
    this.borderRadius,
    super.onChanged,
98
  );
99 100

  final UnderlineTabIndicator decoration;
101
  final BorderRadius? borderRadius;
102

103 104 105
  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    assert(configuration.size != null);
106 107
    final Rect rect = offset & configuration.size!;
    final TextDirection textDirection = configuration.textDirection!;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    final Paint paint;
    if (borderRadius != null) {
      paint = Paint()..color = decoration.borderSide.color;
      final Rect indicator = decoration._indicatorRectFor(rect, textDirection)
        .inflate(decoration.borderSide.width / 4.0);
      final RRect rrect = RRect.fromRectAndCorners(
        indicator,
        topLeft: borderRadius!.topLeft,
        topRight: borderRadius!.topRight,
        bottomRight: borderRadius!.bottomRight,
        bottomLeft: borderRadius!.bottomLeft,
      );
      canvas.drawRRect(rrect, paint);
    } else {
      paint = decoration.borderSide.toPaint()..strokeCap = StrokeCap.square;
      final Rect indicator = decoration._indicatorRectFor(rect, textDirection)
        .deflate(decoration.borderSide.width / 2.0);
      canvas.drawLine(indicator.bottomLeft, indicator.bottomRight, paint);
    }
127 128
  }
}