tab_indicator.dart 3.47 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 24
    this.borderSide = const BorderSide(width: 2.0, color: Colors.white),
    this.insets = EdgeInsets.zero,
25 26
  }) : assert(borderSide != null),
       assert(insets != null);
27 28 29 30 31 32

  /// 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.
  ///
33 34 35 36
  /// 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].
37 38 39
  final EdgeInsetsGeometry insets;

  @override
40
  Decoration? lerpFrom(Decoration? a, double t) {
41
    if (a is UnderlineTabIndicator) {
42
      return UnderlineTabIndicator(
43
        borderSide: BorderSide.lerp(a.borderSide, borderSide, t),
44
        insets: EdgeInsetsGeometry.lerp(a.insets, insets, t)!,
45 46 47 48 49 50
      );
    }
    return super.lerpFrom(a, t);
  }

  @override
51
  Decoration? lerpTo(Decoration? b, double t) {
52
    if (b is UnderlineTabIndicator) {
53
      return UnderlineTabIndicator(
54
        borderSide: BorderSide.lerp(borderSide, b.borderSide, t),
55
        insets: EdgeInsetsGeometry.lerp(insets, b.insets, t)!,
56 57 58 59 60 61
      );
    }
    return super.lerpTo(b, t);
  }

  @override
62
  BoxPainter createBoxPainter([ VoidCallback? onChanged ]) {
63
    return _UnderlinePainter(this, onChanged);
64 65 66 67 68 69
  }

  Rect _indicatorRectFor(Rect rect, TextDirection textDirection) {
    assert(rect != null);
    assert(textDirection != null);
    final Rect indicator = insets.resolve(textDirection).deflateRect(rect);
70
    return Rect.fromLTWH(
71 72 73 74 75 76 77
      indicator.left,
      indicator.bottom - borderSide.width,
      indicator.width,
      borderSide.width,
    );
  }

78 79 80 81 82 83 84
  @override
  Path getClipPath(Rect rect, TextDirection textDirection) {
    return Path()..addRect(_indicatorRectFor(rect, textDirection));
  }
}

class _UnderlinePainter extends BoxPainter {
85
  _UnderlinePainter(this.decoration, VoidCallback? onChanged)
86 87 88 89 90
    : assert(decoration != null),
      super(onChanged);

  final UnderlineTabIndicator decoration;

91 92 93 94
  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    assert(configuration != null);
    assert(configuration.size != null);
95 96
    final Rect rect = offset & configuration.size!;
    final TextDirection textDirection = configuration.textDirection!;
97 98
    final Rect indicator = decoration._indicatorRectFor(rect, textDirection).deflate(decoration.borderSide.width / 2.0);
    final Paint paint = decoration.borderSide.toPaint()..strokeCap = StrokeCap.square;
99
    canvas.drawLine(indicator.bottomLeft, indicator.bottomRight, paint);
100 101
  }
}