Commit d0c95608 authored by Philip's avatar Philip Committed by Jonah Williams

Make RotationTransition widget alignment configurable (#22535)

parent 07fc9f64
...@@ -254,6 +254,7 @@ class RotationTransition extends AnimatedWidget { ...@@ -254,6 +254,7 @@ class RotationTransition extends AnimatedWidget {
const RotationTransition({ const RotationTransition({
Key key, Key key,
@required Animation<double> turns, @required Animation<double> turns,
this.alignment = Alignment.center,
this.child, this.child,
}) : super(key: key, listenable: turns); }) : super(key: key, listenable: turns);
...@@ -263,6 +264,13 @@ class RotationTransition extends AnimatedWidget { ...@@ -263,6 +264,13 @@ class RotationTransition extends AnimatedWidget {
/// rotated v * 2 * pi radians before being painted. /// rotated v * 2 * pi radians before being painted.
Animation<double> get turns => listenable; Animation<double> get turns => listenable;
/// The alignment of the origin of the coordinate system around which the
/// rotation occurs, relative to the size of the box.
///
/// For example, to set the origin of the rotation to top right corner, use
/// an alignment of (1.0, -1.0) or use [Alignment.topRight]
final Alignment alignment;
/// The widget below this widget in the tree. /// The widget below this widget in the tree.
/// ///
/// {@macro flutter.widgets.child} /// {@macro flutter.widgets.child}
...@@ -274,7 +282,7 @@ class RotationTransition extends AnimatedWidget { ...@@ -274,7 +282,7 @@ class RotationTransition extends AnimatedWidget {
final Matrix4 transform = Matrix4.rotationZ(turnsValue * math.pi * 2.0); final Matrix4 transform = Matrix4.rotationZ(turnsValue * math.pi * 2.0);
return Transform( return Transform(
transform: transform, transform: transform,
alignment: Alignment.center, alignment: alignment,
child: child, child: child,
); );
} }
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:math' as math;
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
...@@ -253,4 +255,54 @@ void main() { ...@@ -253,4 +255,54 @@ void main() {
await tester.pump(); await tester.pump();
expect(actualPositionedBox.widthFactor, 1.0); expect(actualPositionedBox.widthFactor, 1.0);
}); });
testWidgets('RotationTransition animates', (WidgetTester tester) async {
final AnimationController controller =
new AnimationController(vsync: const TestVSync());
final Widget widget = new RotationTransition(
alignment: Alignment.topRight,
turns: controller,
child: const Text('Rotation', textDirection: TextDirection.ltr),
);
await tester.pumpWidget(widget);
Transform actualRotatedBox = tester.widget(find.byType(Transform));
Matrix4 actualTurns = actualRotatedBox.transform;
expect(actualTurns, equals(Matrix4.rotationZ(0.0)));
controller.value = 0.5;
await tester.pump();
actualRotatedBox = tester.widget(find.byType(Transform));
actualTurns = actualRotatedBox.transform;
expect(actualTurns, Matrix4.rotationZ(math.pi));
controller.value = 0.75;
await tester.pump();
actualRotatedBox = tester.widget(find.byType(Transform));
actualTurns = actualRotatedBox.transform;
expect(actualTurns, Matrix4.rotationZ(math.pi * 1.5));
});
testWidgets('RotationTransition maintains chosen alignment during animation',
(WidgetTester tester) async {
final AnimationController controller =
new AnimationController(vsync: const TestVSync());
final Widget widget = new RotationTransition(
alignment: Alignment.topRight,
turns: controller,
child: const Text('Rotation', textDirection: TextDirection.ltr),
);
await tester.pumpWidget(widget);
RotationTransition actualRotatedBox =
tester.widget(find.byType(RotationTransition));
Alignment actualAlignment = actualRotatedBox.alignment;
expect(actualAlignment, const Alignment(1.0, -1.0));
controller.value = 0.5;
await tester.pump();
actualRotatedBox = tester.widget(find.byType(RotationTransition));
actualAlignment = actualRotatedBox.alignment;
expect(actualAlignment, const Alignment(1.0, -1.0));
});
} }
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