beveled_rectangle_border.dart 5.48 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;

7 8
import 'package:flutter/foundation.dart';

9 10 11 12 13 14 15 16 17 18 19
import 'basic_types.dart';
import 'border_radius.dart';
import 'borders.dart';

/// A rectangular border with flattened or "beveled" corners.
///
/// The line segments that connect the rectangle's four sides will
/// begin and at locations offset by the corresponding border radius,
/// but not farther than the side's center. If all the border radii
/// exceed the sides' half widths/heights the resulting shape is
/// diamond made by connecting the centers of the sides.
20
class BeveledRectangleBorder extends OutlinedBorder {
21 22 23 24 25
  /// Creates a border like a [RoundedRectangleBorder] except that the corners
  /// are joined by straight lines instead of arcs.
  ///
  /// The arguments must not be null.
  const BeveledRectangleBorder({
26
    super.side,
27
    this.borderRadius = BorderRadius.zero,
28
  }) : assert(side != null),
29
       assert(borderRadius != null);
30 31 32 33 34 35 36 37 38 39 40 41 42 43

  /// The radii for each corner.
  ///
  /// Each corner [Radius] defines the endpoints of a line segment that
  /// spans the corner. The endpoints are located in the same place as
  /// they would be for [RoundedRectangleBorder], but they're connected
  /// by a straight line instead of an arc.
  ///
  /// Negative radius values are clamped to 0.0 by [getInnerPath] and
  /// [getOuterPath].
  final BorderRadiusGeometry borderRadius;

  @override
  ShapeBorder scale(double t) {
44
    return BeveledRectangleBorder(
45 46 47 48 49 50
      side: side.scale(t),
      borderRadius: borderRadius * t,
    );
  }

  @override
51
  ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
52 53
    assert(t != null);
    if (a is BeveledRectangleBorder) {
54
      return BeveledRectangleBorder(
55
        side: BorderSide.lerp(a.side, side, t),
56
        borderRadius: BorderRadiusGeometry.lerp(a.borderRadius, borderRadius, t)!,
57 58 59 60 61 62
      );
    }
    return super.lerpFrom(a, t);
  }

  @override
63
  ShapeBorder? lerpTo(ShapeBorder? b, double t) {
64 65
    assert(t != null);
    if (b is BeveledRectangleBorder) {
66
      return BeveledRectangleBorder(
67
        side: BorderSide.lerp(side, b.side, t),
68
        borderRadius: BorderRadiusGeometry.lerp(borderRadius, b.borderRadius, t)!,
69 70 71 72 73
      );
    }
    return super.lerpTo(b, t);
  }

74 75 76
  /// Returns a copy of this RoundedRectangleBorder with the given fields
  /// replaced with the new values.
  @override
77
  BeveledRectangleBorder copyWith({ BorderSide? side, BorderRadiusGeometry? borderRadius }) {
78 79 80 81 82 83
    return BeveledRectangleBorder(
      side: side ?? this.side,
      borderRadius: borderRadius ?? this.borderRadius,
    );
  }

84
  Path _getPath(RRect rrect) {
85 86 87 88
    final Offset centerLeft = Offset(rrect.left, rrect.center.dy);
    final Offset centerRight = Offset(rrect.right, rrect.center.dy);
    final Offset centerTop = Offset(rrect.center.dx, rrect.top);
    final Offset centerBottom = Offset(rrect.center.dx, rrect.bottom);
89 90 91 92 93 94 95 96 97 98 99

    final double tlRadiusX = math.max(0.0, rrect.tlRadiusX);
    final double tlRadiusY = math.max(0.0, rrect.tlRadiusY);
    final double trRadiusX = math.max(0.0, rrect.trRadiusX);
    final double trRadiusY = math.max(0.0, rrect.trRadiusY);
    final double blRadiusX = math.max(0.0, rrect.blRadiusX);
    final double blRadiusY = math.max(0.0, rrect.blRadiusY);
    final double brRadiusX = math.max(0.0, rrect.brRadiusX);
    final double brRadiusY = math.max(0.0, rrect.brRadiusY);

    final List<Offset> vertices = <Offset>[
100 101 102 103 104 105 106 107
      Offset(rrect.left, math.min(centerLeft.dy, rrect.top + tlRadiusY)),
      Offset(math.min(centerTop.dx, rrect.left + tlRadiusX), rrect.top),
      Offset(math.max(centerTop.dx, rrect.right -trRadiusX), rrect.top),
      Offset(rrect.right, math.min(centerRight.dy, rrect.top + trRadiusY)),
      Offset(rrect.right, math.max(centerRight.dy, rrect.bottom - brRadiusY)),
      Offset(math.max(centerBottom.dx, rrect.right - brRadiusX), rrect.bottom),
      Offset(math.min(centerBottom.dx, rrect.left + blRadiusX), rrect.bottom),
      Offset(rrect.left, math.max(centerLeft.dy, rrect.bottom  - blRadiusY)),
108 109
    ];

110
    return Path()..addPolygon(vertices, true);
111 112 113
  }

  @override
114
  Path getInnerPath(Rect rect, { TextDirection? textDirection }) {
115
    return _getPath(borderRadius.resolve(textDirection).toRRect(rect).deflate(side.strokeInset));
116 117 118
  }

  @override
119
  Path getOuterPath(Rect rect, { TextDirection? textDirection }) {
120 121 122 123
    return _getPath(borderRadius.resolve(textDirection).toRRect(rect));
  }

  @override
124
  void paint(Canvas canvas, Rect rect, { TextDirection? textDirection }) {
125
    if (rect.isEmpty) {
126
      return;
127
    }
128 129 130 131
    switch (side.style) {
      case BorderStyle.none:
        break;
      case BorderStyle.solid:
132
        final RRect borderRect = borderRadius.resolve(textDirection).toRRect(rect);
133
        final RRect adjustedRect = borderRect.inflate(side.strokeOutset);
134 135
        final Path path = _getPath(adjustedRect)
          ..addPath(getInnerPath(rect, textDirection: textDirection), Offset.zero);
136 137 138 139 140 141
        canvas.drawPath(path, side.toPaint());
        break;
    }
  }

  @override
142
  bool operator ==(Object other) {
143
    if (other.runtimeType != runtimeType) {
144
      return false;
145
    }
146 147 148
    return other is BeveledRectangleBorder
        && other.side == side
        && other.borderRadius == borderRadius;
149 150 151
  }

  @override
152
  int get hashCode => Object.hash(side, borderRadius);
153 154 155

  @override
  String toString() {
156
    return '${objectRuntimeType(this, 'BeveledRectangleBorder')}($side, $borderRadius)';
157 158
  }
}