|
| 1 | +import 'dart:math' as Math; |
| 2 | + |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | + |
| 6 | +/// Draws an [ActionIcon] and [_ArcProgressPainter] that represent an active action. |
| 7 | +/// As the provided [Animation] progresses the ActionArc grows into a full |
| 8 | +/// circle and the ActionIcon moves along it. |
| 9 | +class ArcProgressIndicator extends StatelessWidget { |
| 10 | + // required |
| 11 | + final Animation<double> controller; |
| 12 | + final double radius; |
| 13 | + |
| 14 | + // optional |
| 15 | + final double startAngle; |
| 16 | + final double width; |
| 17 | + |
| 18 | + /// The color to use when filling the arc. |
| 19 | + /// |
| 20 | + /// Defaults to the accent color of the current theme. |
| 21 | + final Color color; |
| 22 | + final IconData icon; |
| 23 | + final Color iconColor; |
| 24 | + final double iconSize; |
| 25 | + |
| 26 | + // private |
| 27 | + final Animation<double> _progress; |
| 28 | + |
| 29 | + ArcProgressIndicator({ |
| 30 | + @required this.controller, |
| 31 | + @required this.radius, |
| 32 | + this.startAngle = 0.0, |
| 33 | + this.width, |
| 34 | + this.color, |
| 35 | + this.icon, |
| 36 | + this.iconColor, |
| 37 | + this.iconSize, |
| 38 | + }) : _progress = new Tween(begin: 0.0, end: 1.0).animate(controller); |
| 39 | + |
| 40 | + @override |
| 41 | + Widget build(BuildContext context) { |
| 42 | + TextPainter _iconPainter; |
| 43 | + final ThemeData theme = Theme.of(context); |
| 44 | + final Color _iconColor = iconColor ?? theme.accentIconTheme.color; |
| 45 | + final double _iconSize = iconSize ?? IconTheme.of(context).size; |
| 46 | + |
| 47 | + if (icon != null) { |
| 48 | + _iconPainter = new TextPainter( |
| 49 | + textDirection: Directionality.of(context), |
| 50 | + text: new TextSpan( |
| 51 | + text: new String.fromCharCode(icon.codePoint), |
| 52 | + style: new TextStyle( |
| 53 | + inherit: false, |
| 54 | + color: _iconColor, |
| 55 | + fontSize: _iconSize, |
| 56 | + fontFamily: icon.fontFamily, |
| 57 | + package: icon.fontPackage, |
| 58 | + ), |
| 59 | + ), |
| 60 | + )..layout(); |
| 61 | + } |
| 62 | + |
| 63 | + return new CustomPaint( |
| 64 | + painter: new _ArcProgressPainter( |
| 65 | + controller: _progress, |
| 66 | + color: color ?? theme.accentColor, |
| 67 | + radius: radius, |
| 68 | + width: width ?? _iconSize * 2, |
| 69 | + startAngle: startAngle, |
| 70 | + icon: _iconPainter, |
| 71 | + ), |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class _ArcProgressPainter extends CustomPainter { |
| 77 | + // required |
| 78 | + final Animation<double> controller; |
| 79 | + final Color color; |
| 80 | + final double radius; |
| 81 | + final double width; |
| 82 | + |
| 83 | + // optional |
| 84 | + final double startAngle; |
| 85 | + final TextPainter icon; |
| 86 | + |
| 87 | + _ArcProgressPainter({ |
| 88 | + @required this.controller, |
| 89 | + @required this.color, |
| 90 | + @required this.radius, |
| 91 | + @required this.width, |
| 92 | + this.startAngle = 0.0, |
| 93 | + this.icon, |
| 94 | + }) : super(repaint: controller); |
| 95 | + |
| 96 | + @override |
| 97 | + void paint(Canvas canvas, Size size) { |
| 98 | + Paint paint = new Paint() |
| 99 | + ..color = color |
| 100 | + ..strokeWidth = width |
| 101 | + ..strokeCap = StrokeCap.round |
| 102 | + ..style = PaintingStyle.stroke; |
| 103 | + |
| 104 | + final double sweepAngle = controller.value * 2 * Math.pi; |
| 105 | + |
| 106 | + canvas.drawArc( |
| 107 | + Offset.zero & size, |
| 108 | + startAngle, |
| 109 | + sweepAngle, |
| 110 | + false, |
| 111 | + paint, |
| 112 | + ); |
| 113 | + |
| 114 | + if (icon != null) { |
| 115 | + double angle = startAngle + sweepAngle; |
| 116 | + Offset offset = new Offset( |
| 117 | + (size.width / 2 - icon.size.width / 2) + radius * Math.cos(angle), |
| 118 | + (size.height / 2 - icon.size.height / 2) + radius * Math.sin(angle), |
| 119 | + ); |
| 120 | + |
| 121 | + icon.paint(canvas, offset); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @override |
| 126 | + bool shouldRepaint(_ArcProgressPainter other) { |
| 127 | + return controller.value != other.controller.value || |
| 128 | + color != other.color || |
| 129 | + radius != other.radius || |
| 130 | + width != other.width || |
| 131 | + startAngle != other.startAngle || |
| 132 | + icon != other.icon; |
| 133 | + } |
| 134 | +} |
0 commit comments