Unverified Commit f0698786 authored by Hans Muller's avatar Hans Muller Committed by GitHub

Update indeterminate LinearProgressIndicator (#16106)

parent 0c899200
...@@ -12,6 +12,7 @@ import 'theme.dart'; ...@@ -12,6 +12,7 @@ import 'theme.dart';
const double _kLinearProgressIndicatorHeight = 6.0; const double _kLinearProgressIndicatorHeight = 6.0;
const double _kMinCircularProgressIndicatorSize = 36.0; const double _kMinCircularProgressIndicatorSize = 36.0;
const int _kIndeterminateLinearDuration = 1800;
// TODO(hansmuller): implement the support for buffer indicator // TODO(hansmuller): implement the support for buffer indicator
...@@ -68,6 +69,29 @@ abstract class ProgressIndicator extends StatefulWidget { ...@@ -68,6 +69,29 @@ abstract class ProgressIndicator extends StatefulWidget {
} }
class _LinearProgressIndicatorPainter extends CustomPainter { class _LinearProgressIndicatorPainter extends CustomPainter {
// The indeterminate progress animation displays two lines whose leading (head)
// and trailing (tail) endpoints are defined by the following four curves.
static const Curve line1Head = const Interval(
0.0,
750.0 / _kIndeterminateLinearDuration,
curve: const Cubic(0.2, 0.0, 0.8, 1.0),
);
static const Curve line1Tail = const Interval(
333.0 / _kIndeterminateLinearDuration,
(333.0 + 750.0) / _kIndeterminateLinearDuration,
curve: const Cubic(0.4, 0.0, 1.0, 1.0),
);
static const Curve line2Head = const Interval(
1000.0 / _kIndeterminateLinearDuration,
(1000.0 + 567.0) / _kIndeterminateLinearDuration,
curve: const Cubic(0.0, 0.0, 0.65, 1.0),
);
static const Curve line2Tail = const Interval(
1267.0 / _kIndeterminateLinearDuration,
(1267.0 + 533.0) / _kIndeterminateLinearDuration,
curve: const Cubic(0.10, 0.0, 0.45, 1.0),
);
const _LinearProgressIndicatorPainter({ const _LinearProgressIndicatorPainter({
this.backgroundColor, this.backgroundColor,
this.valueColor, this.valueColor,
...@@ -90,25 +114,10 @@ class _LinearProgressIndicatorPainter extends CustomPainter { ...@@ -90,25 +114,10 @@ class _LinearProgressIndicatorPainter extends CustomPainter {
canvas.drawRect(Offset.zero & size, paint); canvas.drawRect(Offset.zero & size, paint);
paint.color = valueColor; paint.color = valueColor;
if (value != null) {
final double width = value.clamp(0.0, 1.0) * size.width;
double left;
switch (textDirection) {
case TextDirection.rtl:
left = size.width - width;
break;
case TextDirection.ltr:
left = 0.0;
break;
}
canvas.drawRect(new Offset(left, 0.0) & new Size(width, size.height), paint); void drawBar(double x, double width) {
} else { if (width <= 0.0)
final double startX = size.width * (1.5 * animationValue - 0.5); return;
final double endX = startX + 0.5 * size.width;
final double x = startX.clamp(0.0, size.width);
final double width = endX.clamp(0.0, size.width) - x;
double left; double left;
switch (textDirection) { switch (textDirection) {
...@@ -119,9 +128,21 @@ class _LinearProgressIndicatorPainter extends CustomPainter { ...@@ -119,9 +128,21 @@ class _LinearProgressIndicatorPainter extends CustomPainter {
left = x; left = x;
break; break;
} }
canvas.drawRect(new Offset(left, 0.0) & new Size(width, size.height), paint); canvas.drawRect(new Offset(left, 0.0) & new Size(width, size.height), paint);
} }
if (value != null) {
drawBar(0.0, value.clamp(0.0, 1.0) * size.width);
} else {
final double x1 = size.width * line1Tail.transform(animationValue);
final double width1 = size.width * line1Head.transform(animationValue) - x1;
final double x2 = size.width * line2Tail.transform(animationValue);
final double width2 = size.width * line2Head.transform(animationValue) - x2;
drawBar(x1, width1);
drawBar(x2, width2);
}
} }
@override @override
...@@ -170,18 +191,15 @@ class LinearProgressIndicator extends ProgressIndicator { ...@@ -170,18 +191,15 @@ class LinearProgressIndicator extends ProgressIndicator {
} }
class _LinearProgressIndicatorState extends State<LinearProgressIndicator> with SingleTickerProviderStateMixin { class _LinearProgressIndicatorState extends State<LinearProgressIndicator> with SingleTickerProviderStateMixin {
Animation<double> _animation;
AnimationController _controller; AnimationController _controller;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_controller = new AnimationController( _controller = new AnimationController(
duration: const Duration(milliseconds: 1500), duration: const Duration(milliseconds: _kIndeterminateLinearDuration),
vsync: this, vsync: this,
); );
_animation = new CurvedAnimation(parent: _controller, curve: Curves.fastOutSlowIn);
if (widget.value == null) if (widget.value == null)
_controller.repeat(); _controller.repeat();
} }
...@@ -224,12 +242,12 @@ class _LinearProgressIndicatorState extends State<LinearProgressIndicator> with ...@@ -224,12 +242,12 @@ class _LinearProgressIndicatorState extends State<LinearProgressIndicator> with
final TextDirection textDirection = Directionality.of(context); final TextDirection textDirection = Directionality.of(context);
if (widget.value != null) if (widget.value != null)
return _buildIndicator(context, _animation.value, textDirection); return _buildIndicator(context, _controller.value, textDirection);
return new AnimatedBuilder( return new AnimatedBuilder(
animation: _animation, animation: _controller.view,
builder: (BuildContext context, Widget child) { builder: (BuildContext context, Widget child) {
return _buildIndicator(context, _animation.value, textDirection); return _buildIndicator(context, _controller.value, textDirection);
}, },
); );
} }
......
...@@ -100,16 +100,15 @@ void main() { ...@@ -100,16 +100,15 @@ void main() {
), ),
); );
await tester.pump(const Duration(milliseconds: 750)); await tester.pump(const Duration(milliseconds: 300));
final double animationValue = const Interval(0.0, 750.0 / 1800.0, curve: const Cubic(0.2, 0.0, 0.8, 1.0))
final double animationValue = Curves.fastOutSlowIn.transform(0.5); .transform(300.0 / 1800.0);
final double startX = 200.0 * (1.5 * animationValue - 0.5);
expect( expect(
find.byType(LinearProgressIndicator), find.byType(LinearProgressIndicator),
paints paints
..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0, 6.0)) ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0, 6.0))
..rect(rect: new Rect.fromLTRB(startX, 0.0, 200.0, 6.0)) ..rect(rect: new Rect.fromLTRB(0.0, 0.0, animationValue * 200.0, 6.0))
); );
expect(tester.binding.transientCallbackCount, 1); expect(tester.binding.transientCallbackCount, 1);
...@@ -128,16 +127,15 @@ void main() { ...@@ -128,16 +127,15 @@ void main() {
), ),
); );
await tester.pump(const Duration(milliseconds: 750)); await tester.pump(const Duration(milliseconds: 300));
final double animationValue = const Interval(0.0, 750.0 / 1800.0, curve: const Cubic(0.2, 0.0, 0.8, 1.0))
final double animationValue = Curves.fastOutSlowIn.transform(0.5); .transform(300.0 / 1800.0);
final double startX = 200.0 * (1.5 * animationValue - 0.5);
expect( expect(
find.byType(LinearProgressIndicator), find.byType(LinearProgressIndicator),
paints paints
..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0, 6.0)) ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0, 6.0))
..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0 - startX, 6.0)) ..rect(rect: new Rect.fromLTRB(200.0 - animationValue * 200.0, 0.0, 200.0, 6.0))
); );
expect(tester.binding.transientCallbackCount, 1); expect(tester.binding.transientCallbackCount, 1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment