2

How to prevent a child Text from Rotating along with Parent?

This is sample code of what I have. I do not want the Text widget to rotate along with the parent Container. Is there an efficient way of doing it? Else I may have to wrap the child Text in another Transform.rotate and rotate it back.

class MyRotateContainer extends StatefulWidget {
@override
  _MyRotateContainerState createState() => new _MyRotateContainerState();
}

class _MyRotateContainerState extends State<MyRotateContainer> {
@override
Widget build(BuildContext context) {
  return new Transform.rotate(
    angle: (30.0 * PI / 180.0),
    child: new Container(
      child: new Text(
        'SOME TEXT',
        textScaleFactor: 2.0,
      ),
    ));
 }
}

1 Answer 1

2

The solution to the rotation problem is quite simple. You can add a second Transform.rotate, wrapping your Text Widget as seen in the code below. I added red color to the Container to display the rotated Container.

Before adding the second rotate: Before adding 2nd rotate

After adding the second rotate: After adding the 2nd rotate

class MyRotateContainer extends StatefulWidget {
  @override
  _MyRotateContainerState createState() => new _MyRotateContainerState();
}

class _MyRotateContainerState extends State<MyRotateContainer> {
  @override
  Widget build(BuildContext context) {
    double angle = 30.0 * pi / 180.0;

    return Transform.rotate(
      angle: angle,
      child: Container(
        color: Colors.red,
        child: Transform.rotate(
          angle: -angle,
          child: Text(
            'SOME TEXT',
            textScaleFactor: 2.0,
          ),
        ),
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Applying the inverse transform to the child is the right way and probably the only way of doing it. If your transform is more ocmplicated, you can assign it to a final Matrix4 myTransform = .... You can use this constructor for the parent. For the child use the same constructor but pass Matrix4.inverted(myTransform). This way you don't have to do the inverse transform by hand, or by brain :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.