|
| 1 | +// Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:math' as math; |
| 6 | + |
| 7 | +import 'package:flutter/widgets.dart'; |
| 8 | +import 'package:flutter/rendering.dart'; |
| 9 | +import 'base/theme.dart'; |
| 10 | + |
| 11 | +const double _kDefaultIndicatorRadius = 10.0; |
| 12 | + |
| 13 | + |
| 14 | +/// An iOS-style activity indicator that spins clockwise. |
| 15 | +/// |
| 16 | +/// See also: |
| 17 | +/// |
| 18 | +/// * <https://developer.apple.com/ios/human-interface-guidelines/controls/progress-indicators/#activity-indicators> |
| 19 | +class Loading extends StatefulWidget { |
| 20 | + /// Creates an iOS-style activity indicator that spins clockwise. |
| 21 | + const Loading({ |
| 22 | + Key key, |
| 23 | + this.animating = true, |
| 24 | + this.radius = _kDefaultIndicatorRadius, |
| 25 | + }) : assert(animating != null), |
| 26 | + assert(radius != null), |
| 27 | + assert(radius > 0), |
| 28 | + super(key: key); |
| 29 | + |
| 30 | + /// Whether the activity indicator is running its animation. |
| 31 | + /// |
| 32 | + /// Defaults to true. |
| 33 | + final bool animating; |
| 34 | + |
| 35 | + /// Radius of the spinner widget. |
| 36 | + /// |
| 37 | + /// Defaults to 10px. Must be positive and cannot be null. |
| 38 | + final double radius; |
| 39 | + |
| 40 | + @override |
| 41 | + _LoadingState createState() => _LoadingState(); |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +class _LoadingState extends State<Loading> with SingleTickerProviderStateMixin { |
| 46 | + AnimationController _controller; |
| 47 | + |
| 48 | + @override |
| 49 | + void initState() { |
| 50 | + super.initState(); |
| 51 | + _controller = AnimationController( |
| 52 | + duration: const Duration(seconds: 1), |
| 53 | + vsync: this, |
| 54 | + ); |
| 55 | + |
| 56 | + if (widget.animating) |
| 57 | + _controller.repeat(); |
| 58 | + } |
| 59 | + |
| 60 | + @override |
| 61 | + void didUpdateWidget(Loading oldWidget) { |
| 62 | + super.didUpdateWidget(oldWidget); |
| 63 | + if (widget.animating != oldWidget.animating) { |
| 64 | + if (widget.animating) |
| 65 | + _controller.repeat(); |
| 66 | + else |
| 67 | + _controller.stop(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @override |
| 72 | + void dispose() { |
| 73 | + _controller.dispose(); |
| 74 | + super.dispose(); |
| 75 | + } |
| 76 | + |
| 77 | + @override |
| 78 | + Widget build(BuildContext context) { |
| 79 | + return SizedBox( |
| 80 | + height: widget.radius * 2, |
| 81 | + width: widget.radius * 2, |
| 82 | + child: CustomPaint( |
| 83 | + painter: _CupertinoActivityIndicatorPainter( |
| 84 | + position: _controller, |
| 85 | + activeColor: WeUITheme.of(context).loadingColor, |
| 86 | + radius: widget.radius, |
| 87 | + ), |
| 88 | + ), |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +const double _kTwoPI = math.pi * 2.0; |
| 94 | +const int _kTickCount = 12; |
| 95 | + |
| 96 | +// Alpha values extracted from the native component (for both dark and light mode). |
| 97 | +// The list has a length of 12. |
| 98 | +const List<int> _alphaValues = <int>[147, 131, 114, 97, 81, 64, 47, 47, 47, 47, 47, 47]; |
| 99 | + |
| 100 | +class _CupertinoActivityIndicatorPainter extends CustomPainter { |
| 101 | + _CupertinoActivityIndicatorPainter({ |
| 102 | + @required this.position, |
| 103 | + @required this.activeColor, |
| 104 | + double radius, |
| 105 | + }) : tickFundamentalRRect = RRect.fromLTRBXY( |
| 106 | + -radius, |
| 107 | + radius / _kDefaultIndicatorRadius, |
| 108 | + -radius / 2.0, |
| 109 | + -radius / _kDefaultIndicatorRadius, |
| 110 | + radius / _kDefaultIndicatorRadius, |
| 111 | + radius / _kDefaultIndicatorRadius, |
| 112 | + ), |
| 113 | + super(repaint: position); |
| 114 | + |
| 115 | + final Animation<double> position; |
| 116 | + final RRect tickFundamentalRRect; |
| 117 | + final Color activeColor; |
| 118 | + |
| 119 | + @override |
| 120 | + void paint(Canvas canvas, Size size) { |
| 121 | + final Paint paint = Paint(); |
| 122 | + |
| 123 | + canvas.save(); |
| 124 | + canvas.translate(size.width / 2.0, size.height / 2.0); |
| 125 | + |
| 126 | + final int activeTick = (_kTickCount * position.value).floor(); |
| 127 | + |
| 128 | + for (int i = 0; i < _kTickCount; ++ i) { |
| 129 | + final int t = (i + activeTick) % _kTickCount; |
| 130 | + paint.color = activeColor.withAlpha(_alphaValues[t]); |
| 131 | + canvas.drawRRect(tickFundamentalRRect, paint); |
| 132 | + canvas.rotate(-_kTwoPI / _kTickCount); |
| 133 | + } |
| 134 | + |
| 135 | + canvas.restore(); |
| 136 | + } |
| 137 | + |
| 138 | + @override |
| 139 | + bool shouldRepaint(_CupertinoActivityIndicatorPainter oldPainter) { |
| 140 | + return oldPainter.position != position || oldPainter.activeColor != activeColor; |
| 141 | + } |
| 142 | +} |
0 commit comments