From 6f435b286e45b91ee2da0e7d3c721893613bb857 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Mon, 17 Sep 2018 10:08:27 -0700 Subject: [PATCH] Stop the animation controller if a CircularProgressIndicator is determinate (#21832) Fixes https://github.com/flutter/flutter/issues/21445 --- .../lib/src/material/progress_indicator.dart | 15 +++++++++-- .../material/progress_indicator_test.dart | 25 ++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/material/progress_indicator.dart b/packages/flutter/lib/src/material/progress_indicator.dart index 6a3cd57a20..811f07f021 100644 --- a/packages/flutter/lib/src/material/progress_indicator.dart +++ b/packages/flutter/lib/src/material/progress_indicator.dart @@ -374,9 +374,20 @@ class _CircularProgressIndicatorState extends State w void initState() { super.initState(); _controller = AnimationController( - duration: const Duration(milliseconds: 6666), + duration: const Duration(seconds: 5), vsync: this, - )..repeat(); + ); + if (widget.value == null) + _controller.repeat(); + } + + @override + void didUpdateWidget(CircularProgressIndicator oldWidget) { + super.didUpdateWidget(oldWidget); + if (widget.value == null && !_controller.isAnimating) + _controller.repeat(); + else if (widget.value != null && _controller.isAnimating) + _controller.stop(); } @override diff --git a/packages/flutter/test/material/progress_indicator_test.dart b/packages/flutter/test/material/progress_indicator_test.dart index 8b2424fe78..96eb9dd010 100644 --- a/packages/flutter/test/material/progress_indicator_test.dart +++ b/packages/flutter/test/material/progress_indicator_test.dart @@ -222,7 +222,7 @@ void main() { ); expect(tester.hasRunningAnimations, isTrue); - await tester.pump(const Duration(milliseconds: 6666)); + await tester.pump(const Duration(seconds: 5)); expect(tester.hasRunningAnimations, isTrue); await tester.pump(const Duration(milliseconds: 1)); @@ -232,4 +232,27 @@ void main() { expect(tester.hasRunningAnimations, isTrue); }); + testWidgets('Determinate CircularProgressIndicator stops the animator', (WidgetTester tester) async { + double progressValue; + StateSetter setState; + await tester.pumpWidget( + Center( + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setter) { + setState = setter; + return CircularProgressIndicator(value: progressValue); + } + ), + ) + ); + expect(tester.hasRunningAnimations, isTrue); + + setState(() { progressValue = 1.0; }); + await tester.pump(const Duration(milliseconds: 1)); + expect(tester.hasRunningAnimations, isFalse); + + setState(() { progressValue = null; }); + await tester.pump(const Duration(milliseconds: 1)); + expect(tester.hasRunningAnimations, isTrue); + }); }