Created
March 30, 2020 08:38
-
-
Save 410063005/937a9a464a58f5bcc1c0d9b4ad1f1b31 to your computer and use it in GitHub Desktop.
A fixed version of loading library's BallSpinFadeLoaderIndicator。针对 BallSpinFadeLoaderIndicator 的修复
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
import 'package:loading/indicator.dart'; | |
class FixedBallSpinFadeLoaderIndicator extends Indicator { | |
var scaleDoubles = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]; | |
var alphas = [255, 255, 255, 255, 255, 255, 255, 255]; | |
@override | |
paint(Canvas canvas, Paint paint, Size size) { | |
var radius = size.width / 10; | |
for (int i = 0; i < 8; i++) { | |
canvas.save(); | |
Offset point = circleAt( | |
size.width, size.height, size.width / 2 - radius, i * (pi / 4)); | |
canvas.translate(point.dx, point.dy); | |
canvas.scale(scaleDoubles[i], scaleDoubles[i]); | |
paint.color = paint.color.withAlpha(alphas[i]); | |
canvas.drawCircle(Offset(0, 0), radius, paint); | |
canvas.restore(); | |
} | |
} | |
@override | |
List<AnimationController> animation() { | |
var controllers = List<AnimationController>(); | |
for (int i = 0; i < 8; i++) { | |
var controller = new AnimationController( | |
duration: Duration(milliseconds: 500), vsync: context); | |
var alphaTween = new IntTween(begin: 255, end: 77).animate(controller); | |
var scaleTween = new Tween(begin: 1.0, end: 0.4).animate(controller); | |
controller.addListener(() { | |
scaleDoubles[i] = scaleTween.value; | |
alphas[i] = alphaTween.value; | |
postInvalidate(); | |
}); | |
controllers.add(controller); | |
} | |
return controllers; | |
} | |
@override | |
startAnims(List<AnimationController> controllers) { | |
var delays = [0, 120, 240, 360, 480, 600, 720, 780, 840]; | |
for (var i = 0; i < controllers.length; i++) { | |
// Fix 1, check if dispose() has been called | |
// I'm not very sure it is necessary | |
if (!context.mounted) { | |
break; | |
} | |
Future.delayed(Duration(milliseconds: delays[i]), () { | |
if (context.mounted) { | |
// Fix 2, check if dispose() has been called | |
controllers[i].repeat(reverse: true); | |
} | |
}); | |
} | |
} | |
Offset circleAt(double width, double height, double radius, double angle) { | |
var x = (width / 2 + radius * (cos(angle))); | |
var y = (height / 2 + radius * (sin(angle))); | |
return new Offset(x, y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment