I have an AnimatedContainer
, and I want to rotate it by 180 degrees, and the origin should be at its center.
It has a transform
parameter that, unfortunately, asks for a Matrix4
.
There is no explanation of Matrix4
in the Flutter documentation:
https://api.flutter.dev/flutter/vector_math_64/Matrix4-class.html
What is the Matrix4 I must use to rotate it by its center?
I've found the answer and just made this package (to do all kinds of transformations, not only rotate by the center): https://pub.dev/packages/matrix4_transform
var height = 30;
var width = 30;
AnimatedContainer(
color: Colors.red,
width: width,
height: height,
transform:
Matrix4Transform()
.rotateDegrees(180, origin: Offset(width/2, height/2))
.matrix4,
);
There is no need to use a Matrix4
for a simple 180 degrees rotation. Use RotationTransition
to wrap your AnimatedContainer
instead. RotationTransition
takes a turns
parameter, which is an Animation<T>
whose value can be used to represent the RotationTransition
's child widget rotation in radians. This way, you can control your rotation animation via an AnimationController
. Check out this example from the Flutter official GitHub to find out more.
You can specify the alignment using the transformAlignment
field.
AnimatedContainer(
duration: Duration(seconds: 1),
transform: Matrix4.rotationZ(_yourAngle),
transformAlignment: Alignment.center,
child: YourWidget(),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With