tab_indicator.dart 4.45 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
  }) : assert(borderSide != null),
       assert(insets != null);
28

29 30 31 32 33 34
  /// 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;

35 36 37 38 39
  /// 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.
  ///
40 41 42 43
  /// 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].
44 45 46
  final EdgeInsetsGeometry insets;

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

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

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

  Rect _indicatorRectFor(Rect rect, TextDirection textDirection) {
    assert(rect != null);
    assert(textDirection != null);
    final Rect indicator = insets.resolve(textDirection).deflateRect(rect);
77
    return Rect.fromLTWH(
78 79 80 81 82 83 84
      indicator.left,
      indicator.bottom - borderSide.width,
      indicator.width,
      borderSide.width,
    );
  }

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

class _UnderlinePainter extends BoxPainter {
97 98 99 100 101
  _UnderlinePainter(
    this.decoration,
    this.borderRadius,
    super.onChanged,
  )
102
    : assert(decoration != null);
103 104

  final UnderlineTabIndicator decoration;
105
  final BorderRadius? borderRadius;
106

107 108 109 110
  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    assert(configuration != null);
    assert(configuration.size != null);
111 112
    final Rect rect = offset & configuration.size!;
    final TextDirection textDirection = configuration.textDirection!;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    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);
    }
132 133
  }
}