Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/wave.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -214,6 +215,7 @@ class WaveWidget extends StatefulWidget {
final double heightPercentange;
final int duration;
final Color backgroundColor;
final bool isLoop;

WaveWidget({
@required this.config,
Expand All @@ -224,6 +226,7 @@ class WaveWidget extends StatefulWidget {
this.wavePhase = 10.0,
this.backgroundColor,
this.heightPercentange = 0.2,
this.isLoop = true,
});

@override
Expand All @@ -236,6 +239,7 @@ class _WaveWidgetState extends State<WaveWidget> with TickerProviderStateMixin {

List<double> _waveAmplitudes = [];
Map<Animation<double>, AnimationController> valueList;
Timer _endAnimationTimer;

_initAnimations() {
if (widget.config.colorMode == ColorMode.custom) {
Expand Down Expand Up @@ -270,6 +274,15 @@ class _WaveWidgetState extends State<WaveWidget> with TickerProviderStateMixin {
controller.forward();
return value;
}).toList();

// If isLoop is false, stop the animation after the specified duration.
if (!widget.isLoop) {
_endAnimationTimer = Timer(Duration(milliseconds: widget.duration), () {
for (AnimationController waveController in _waveControllers) {
waveController.stop();
}
});
}
}
}

Expand Down Expand Up @@ -321,6 +334,7 @@ class _WaveWidgetState extends State<WaveWidget> with TickerProviderStateMixin {
@override
void dispose() {
_disposeAnimations();
_endAnimationTimer?.cancel();
super.dispose();
}

Expand Down
33 changes: 20 additions & 13 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
description:
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:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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"
60 changes: 59 additions & 1 deletion test/wave_test.dart
Original file line number Diff line number Diff line change
@@ -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,
),
),
);
}