From 2220b1bdd8388548a9c14ade63f968841d5c08d1 Mon Sep 17 00:00:00 2001 From: clive107 Date: Fri, 25 Oct 2019 18:07:21 +0800 Subject: [PATCH 1/2] Added option to stop animation by duration --- lib/wave.dart | 12 +++++++++ pubspec.lock | 33 +++++++++++++++---------- test/wave_test.dart | 60 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 14 deletions(-) diff --git a/lib/wave.dart b/lib/wave.dart index 0dec8a5..b23ebe4 100644 --- a/lib/wave.dart +++ b/lib/wave.dart @@ -200,6 +200,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +import 'dart:async'; import 'dart:math'; import 'package:flutter/widgets.dart'; @@ -214,6 +215,7 @@ class WaveWidget extends StatefulWidget { final double heightPercentange; final int duration; final Color backgroundColor; + final bool isLoop; WaveWidget({ @required this.config, @@ -224,6 +226,7 @@ class WaveWidget extends StatefulWidget { this.wavePhase = 10.0, this.backgroundColor, this.heightPercentange = 0.2, + this.isLoop = true, }); @override @@ -270,6 +273,15 @@ class _WaveWidgetState extends State with TickerProviderStateMixin { controller.forward(); return value; }).toList(); + + // If isLoop is false, stop the animation after the specified duration. + if (!widget.isLoop) { + Timer(Duration(milliseconds: widget.duration), () { + for (AnimationController waveController in _waveControllers) { + waveController.stop(); + } + }); + } } } diff --git a/pubspec.lock b/pubspec.lock index 224c7bb..aebdafa 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,5 +1,5 @@ # Generated by pub -# See https://www.dartlang.org/tools/pub/glossary#lockfile +# See https://dart.dev/tools/pub/glossary#lockfile packages: async: dependency: transitive @@ -7,14 +7,14 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.3.0" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "1.0.5" charcode: dependency: transitive description: @@ -45,28 +45,35 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.3+1" + version: "0.12.5" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.1.7" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.2" + version: "1.6.4" + pedantic: + dependency: transitive + description: + name: pedantic + url: "https://pub.dartlang.org" + source: hosted + version: "1.8.0+1" quiver: dependency: transitive description: name: quiver url: "https://pub.dartlang.org" source: hosted - version: "2.0.1" + version: "2.0.5" sky_engine: dependency: transitive description: flutter @@ -78,7 +85,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.4.1" + version: "1.5.5" stack_trace: dependency: transitive description: @@ -92,28 +99,28 @@ packages: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "1.6.8" + version: "2.0.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "1.0.5" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "1.1.0" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.1" + version: "0.2.5" typed_data: dependency: transitive description: @@ -129,4 +136,4 @@ packages: source: hosted version: "2.0.8" sdks: - dart: ">=2.0.0 <3.0.0" + dart: ">=2.2.2 <3.0.0" diff --git a/test/wave_test.dart b/test/wave_test.dart index eb3f2cc..44618db 100644 --- a/test/wave_test.dart +++ b/test/wave_test.dart @@ -1 +1,59 @@ -class WaveTest {} +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:wave/config.dart'; +import 'package:wave/wave.dart'; + +void main() { + group('Wave widget', () { + testWidgets('isLoop true (default)', (WidgetTester tester) async { + await tester.pumpWidget(getWaveWidget()); + try { + /* + If isLoop is true, + animations will not stopped and therefore the pumpAndSettle() will throw error. + */ + await tester.pumpAndSettle(); + } catch (e) { + expect(e, isFlutterError); + } + }); + + testWidgets('isLoop false', (WidgetTester tester) async { + int duration = 5000; + await tester.pumpWidget(getWaveWidget(duration: duration, isLoop: false)); + int count = await tester.pumpAndSettle(const Duration(milliseconds: 1)); + // Animations should be stopped after the specified duration. + expect(count, duration); + }); + }); +} + +Widget getWaveWidget({int duration, bool isLoop = true}) { + return MaterialApp( + home: Container( + child: WaveWidget( + backgroundColor: Colors.white, + config: CustomConfig( + blur: MaskFilter.blur( + BlurStyle.solid, + 0.0, + ), + colors: [ + Colors.white54, + Colors.white30, + Colors.white, + ], + durations: [21000, 18000, 5000], + heightPercentages: [0.26, 0.28, 0.31], + ), + duration: duration, + isLoop: isLoop, + size: Size( + double.infinity, + double.infinity, + ), + waveAmplitude: 5.0, + ), + ), + ); +} From 1fee3c61c99699cfde7dd859e75b8ca34a23deb2 Mon Sep 17 00:00:00 2001 From: clive107 Date: Thu, 31 Oct 2019 17:21:02 +0800 Subject: [PATCH 2/2] Cancel timer on dispose --- lib/wave.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/wave.dart b/lib/wave.dart index b23ebe4..5e0363e 100644 --- a/lib/wave.dart +++ b/lib/wave.dart @@ -239,6 +239,7 @@ class _WaveWidgetState extends State with TickerProviderStateMixin { List _waveAmplitudes = []; Map, AnimationController> valueList; + Timer _endAnimationTimer; _initAnimations() { if (widget.config.colorMode == ColorMode.custom) { @@ -276,7 +277,7 @@ class _WaveWidgetState extends State with TickerProviderStateMixin { // If isLoop is false, stop the animation after the specified duration. if (!widget.isLoop) { - Timer(Duration(milliseconds: widget.duration), () { + _endAnimationTimer = Timer(Duration(milliseconds: widget.duration), () { for (AnimationController waveController in _waveControllers) { waveController.stop(); } @@ -333,6 +334,7 @@ class _WaveWidgetState extends State with TickerProviderStateMixin { @override void dispose() { _disposeAnimations(); + _endAnimationTimer?.cancel(); super.dispose(); }