circle_border.dart 2.63 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 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';

Ian Hickson's avatar
Ian Hickson committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
import 'basic_types.dart';
import 'borders.dart';
import 'edge_insets.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.
///
/// See also:
///
///  * [BorderSide], which is used to describe each side of the box.
///  * [Border], which, when used with [BoxDecoration], can also
///    describe a circle.
class CircleBorder extends ShapeBorder {
  /// Create a circle border.
  ///
  /// The [side] argument must not be null.
30
  const CircleBorder({ this.side = BorderSide.none }) : assert(side != null);
Ian Hickson's avatar
Ian Hickson committed
31 32 33 34 35 36

  /// The style of this border.
  final BorderSide side;

  @override
  EdgeInsetsGeometry get dimensions {
37
    return EdgeInsets.all(side.width);
Ian Hickson's avatar
Ian Hickson committed
38 39 40
  }

  @override
41
  ShapeBorder scale(double t) => CircleBorder(side: side.scale(t));
Ian Hickson's avatar
Ian Hickson committed
42 43 44 45

  @override
  ShapeBorder lerpFrom(ShapeBorder a, double t) {
    if (a is CircleBorder)
46
      return CircleBorder(side: BorderSide.lerp(a.side, side, t));
Ian Hickson's avatar
Ian Hickson committed
47 48 49 50 51 52
    return super.lerpFrom(a, t);
  }

  @override
  ShapeBorder lerpTo(ShapeBorder b, double t) {
    if (b is CircleBorder)
53
      return CircleBorder(side: BorderSide.lerp(side, b.side, t));
Ian Hickson's avatar
Ian Hickson committed
54 55 56 57 58
    return super.lerpTo(b, t);
  }

  @override
  Path getInnerPath(Rect rect, { TextDirection textDirection }) {
59 60
    return Path()
      ..addOval(Rect.fromCircle(
Ian Hickson's avatar
Ian Hickson committed
61 62 63 64 65 66 67
        center: rect.center,
        radius: math.max(0.0, rect.shortestSide / 2.0 - side.width),
      ));
  }

  @override
  Path getOuterPath(Rect rect, { TextDirection textDirection }) {
68 69
    return Path()
      ..addOval(Rect.fromCircle(
Ian Hickson's avatar
Ian Hickson committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        center: rect.center,
        radius: rect.shortestSide / 2.0,
      ));
  }

  @override
  void paint(Canvas canvas, Rect rect, { TextDirection textDirection }) {
    switch (side.style) {
      case BorderStyle.none:
        break;
      case BorderStyle.solid:
        canvas.drawCircle(rect.center, (rect.shortestSide - side.width) / 2.0, side.toPaint());
    }
  }

  @override
86 87
  bool operator ==(Object other) {
    if (other.runtimeType != runtimeType)
Ian Hickson's avatar
Ian Hickson committed
88
      return false;
89 90
    return other is CircleBorder
        && other.side == side;
Ian Hickson's avatar
Ian Hickson committed
91 92 93 94 95 96 97
  }

  @override
  int get hashCode => side.hashCode;

  @override
  String toString() {
98
    return '${objectRuntimeType(this, 'CircleBorder')}($side)';
Ian Hickson's avatar
Ian Hickson committed
99 100
  }
}