Flutter: Matrix4 to rotate a Container 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.

Tags:

Math

Flutter