From 40f3b9a73d8eb84cb8668764265c2e18e76903cc Mon Sep 17 00:00:00 2001 From: Grant Skinner Date: Mon, 13 Feb 2023 12:25:34 -0700 Subject: [PATCH] Implement FlipEffect test --- lib/src/effects/flip_effect.dart | 43 +++++++++++++++++++------------- test/effects/flip_test.dart | 25 ++++++++++++++++++- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/lib/src/effects/flip_effect.dart b/lib/src/effects/flip_effect.dart index fb11ce1..0d23611 100644 --- a/lib/src/effects/flip_effect.dart +++ b/lib/src/effects/flip_effect.dart @@ -64,26 +64,33 @@ class FlipEffect extends Effect { Animation animation = buildAnimation(controller, entry); return getOptimizedBuilder( animation: animation, - builder: (_, __) { - double value = animation.value * pi; - - final Matrix4 mtx = Matrix4( - 1.0, 0.0, 0.0, 0.0, // - 0.0, 1.0, 0.0, 0.0, // - 0.0, 0.0, 1.0, 0.002 * perspective, // - 0.0, 0.0, 0.0, 1.0, - ); - if (value != 0) { - if (direction == Axis.vertical) { - mtx.rotateX(value); - } else { - mtx.rotateY(value); - } - } + builder: (_, __) => Transform( + alignment: alignment, + transform: getTransformMatrix(animation.value, direction, perspective), + child: child, + ), + ); + } - return Transform(alignment: alignment, transform: mtx, child: child); - }, + static Matrix4 getTransformMatrix( + double value, + Axis direction, + double perspective, + ) { + final Matrix4 mtx = Matrix4( + 1.0, 0.0, 0.0, 0.0, // + 0.0, 1.0, 0.0, 0.0, // + 0.0, 0.0, 1.0, 0.002 * perspective, // + 0.0, 0.0, 0.0, 1.0, ); + if (value != 0) { + if (direction == Axis.vertical) { + mtx.rotateX(value * pi); + } else { + mtx.rotateY(value * pi); + } + } + return mtx; } } diff --git a/test/effects/flip_test.dart b/test/effects/flip_test.dart index a848bec..b9755f8 100644 --- a/test/effects/flip_test.dart +++ b/test/effects/flip_test.dart @@ -1,3 +1,26 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter_animate/flutter_animate.dart'; + +import '../tester_extensions.dart'; + void main() { - // TODO: expose FlipEffect.getTransformMatrix (name TBD) and use in test + testWidgets('FlipEffect: core', (tester) async { + Axis direction = Axis.vertical; + double perspective = 2; + final animation = const FlutterLogo().animate().flip( + duration: 1000.ms, + direction: direction, + perspective: perspective, + begin: 0, + end: 1, + ); + + // Check halfway, + await tester.pumpAnimation(animation, initialDelay: 500.ms); + // Create a matrix and compare to the one in the widget tree + var mtx = FlipEffect.getTransformMatrix(0.5, direction, perspective); + tester.expectWidgetWithBool( + (o) => o.transform == mtx, true, 'transform @ 500ms'); + }); }