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

5
import 'dart:ui' as ui show lerpDouble;
Ian Hickson's avatar
Ian Hickson committed
6

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

Ian Hickson's avatar
Ian Hickson committed
9 10 11 12 13 14 15 16 17 18 19
import 'basic_types.dart';
import 'borders.dart';

/// A border that fits a circle within the available space.
///
/// Typically used with [ShapeDecoration] to draw a circle.
///
/// The [dimensions] assume that the border is being used in a square space.
/// When applied to a rectangular space, the border paints in the center of the
/// rectangle.
///
20 21 22 23 24 25
/// The [eccentricity] parameter describes how much a circle will deform to
/// fit the rectangle it is a border for. A value of zero implies no
/// deformation (a circle touching at least two sides of the rectangle), a
/// value of one implies full deformation (an oval touching all sides of the
/// rectangle).
///
Ian Hickson's avatar
Ian Hickson committed
26 27
/// See also:
///
28
///  * [OvalBorder], which draws a Circle touching all the edges of the box.
Ian Hickson's avatar
Ian Hickson committed
29
///  * [BorderSide], which is used to describe each side of the box.
30
///  * [Border], which, when used with [BoxDecoration], can also describe a circle.
31
class CircleBorder extends OutlinedBorder {
Ian Hickson's avatar
Ian Hickson committed
32
  /// Create a circle border.
33
  const CircleBorder({ super.side, this.eccentricity = 0.0 })
34
      : assert(eccentricity >= 0.0, 'The eccentricity argument $eccentricity is not greater than or equal to zero.'),
35 36 37 38 39 40 41
        assert(eccentricity <= 1.0, 'The eccentricity argument $eccentricity is not less than or equal to one.');

  /// Defines the ratio (0.0-1.0) from which the border will deform
  /// to fit a rectangle.
  /// When 0.0, it draws a circle touching at least two sides of the rectangle.
  /// When 1.0, it draws an oval touching all sides of the rectangle.
  final double eccentricity;
Ian Hickson's avatar
Ian Hickson committed
42 43

  @override
44
  ShapeBorder scale(double t) => CircleBorder(side: side.scale(t), eccentricity: eccentricity);
Ian Hickson's avatar
Ian Hickson committed
45 46

  @override
47
  ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
48
    if (a is CircleBorder) {
49 50 51 52
      return CircleBorder(
        side: BorderSide.lerp(a.side, side, t),
        eccentricity: clampDouble(ui.lerpDouble(a.eccentricity, eccentricity, t)!, 0.0, 1.0),
      );
53
    }
Ian Hickson's avatar
Ian Hickson committed
54 55 56 57
    return super.lerpFrom(a, t);
  }

  @override
58
  ShapeBorder? lerpTo(ShapeBorder? b, double t) {
59
    if (b is CircleBorder) {
60 61 62 63
      return CircleBorder(
        side: BorderSide.lerp(side, b.side, t),
        eccentricity: clampDouble(ui.lerpDouble(eccentricity, b.eccentricity, t)!, 0.0, 1.0),
      );
64
    }
Ian Hickson's avatar
Ian Hickson committed
65 66 67 68
    return super.lerpTo(b, t);
  }

  @override
69
  Path getInnerPath(Rect rect, { TextDirection? textDirection }) {
70
    return Path()..addOval(_adjustRect(rect).deflate(side.strokeInset));
Ian Hickson's avatar
Ian Hickson committed
71 72 73
  }

  @override
74
  Path getOuterPath(Rect rect, { TextDirection? textDirection }) {
75
    return Path()..addOval(_adjustRect(rect));
Ian Hickson's avatar
Ian Hickson committed
76 77
  }

78 79 80 81 82 83 84 85 86 87 88 89
  @override
  void paintInterior(Canvas canvas, Rect rect, Paint paint, { TextDirection? textDirection }) {
    if (eccentricity == 0.0) {
      canvas.drawCircle(rect.center, rect.shortestSide / 2.0, paint);
    } else {
      canvas.drawOval(_adjustRect(rect), paint);
    }
  }

  @override
  bool get preferPaintInterior => true;

90
  @override
91 92
  CircleBorder copyWith({ BorderSide? side, double? eccentricity }) {
    return CircleBorder(side: side ?? this.side, eccentricity: eccentricity ?? this.eccentricity);
93 94
  }

Ian Hickson's avatar
Ian Hickson committed
95
  @override
96
  void paint(Canvas canvas, Rect rect, { TextDirection? textDirection }) {
Ian Hickson's avatar
Ian Hickson committed
97 98 99 100
    switch (side.style) {
      case BorderStyle.none:
        break;
      case BorderStyle.solid:
101 102
        if (eccentricity == 0.0) {
          canvas.drawCircle(rect.center, (rect.shortestSide + side.strokeOffset) / 2, side.toPaint());
103
        } else {
104 105
          final Rect borderRect = _adjustRect(rect);
          canvas.drawOval(borderRect.inflate(side.strokeOffset / 2), side.toPaint());
106
        }
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    }
  }

  Rect _adjustRect(Rect rect) {
    if (eccentricity == 0.0 || rect.width == rect.height) {
      return Rect.fromCircle(center: rect.center, radius: rect.shortestSide / 2.0);
    }
    if (rect.width < rect.height) {
      final double delta = (1.0 - eccentricity) * (rect.height - rect.width) / 2.0;
      return Rect.fromLTRB(
        rect.left,
        rect.top + delta,
        rect.right,
        rect.bottom - delta,
      );
    } else {
      final double delta = (1.0 - eccentricity) * (rect.width - rect.height) / 2.0;
      return Rect.fromLTRB(
        rect.left + delta,
        rect.top,
        rect.right - delta,
        rect.bottom,
      );
Ian Hickson's avatar
Ian Hickson committed
130 131 132 133
    }
  }

  @override
134
  bool operator ==(Object other) {
135
    if (other.runtimeType != runtimeType) {
Ian Hickson's avatar
Ian Hickson committed
136
      return false;
137
    }
138
    return other is CircleBorder
139 140
        && other.side == side
        && other.eccentricity == eccentricity;
Ian Hickson's avatar
Ian Hickson committed
141 142 143
  }

  @override
144
  int get hashCode => Object.hash(side, eccentricity);
Ian Hickson's avatar
Ian Hickson committed
145 146 147

  @override
  String toString() {
148 149 150
    if (eccentricity != 0.0) {
      return '${objectRuntimeType(this, 'CircleBorder')}($side, eccentricity: $eccentricity)';
    }
151
    return '${objectRuntimeType(this, 'CircleBorder')}($side)';
Ian Hickson's avatar
Ian Hickson committed
152 153
  }
}