How do I rotate something 15 degrees in Flutter?

DartFlutter

Dart Problem Overview


The Flutter docs show an example of rotating a "div" by 15 degrees, both for HTML/CSS and Flutter code:

The Flutter code is:

var container = new Container( // gray box
  child: new Center(
    child:  new Transform(
      child:  new Text(
        "Lorem ipsum",
      ),
      alignment: FractionalOffset.center,
      transform: new Matrix4.identity()
        ..rotateZ(15 * 3.1415927 / 180),
    ), 
  ),
);

And the relevant parts are new Transform and alignment: FractionalOffset.center and transform: new Matrix4.identity()..rotateZ(15 * 3.1415927 / 180)

I'm curious, is there a simpler way to rotate a Container in Flutter? Is there a short-hand for the case of "15 degrees" ?

Thanks!

Dart Solutions


Solution 1 - Dart

In mobile apps, I think it's kind of rare to have things start out rotated 15 degrees and just stay there forever. So that may be why Flutter's support for rotation is better if you're planning to adjust the rotation over time.

It feels like overkill, but a RotationTransition with an AlwaysStoppedAnimation would accomplish exactly what you want.

screenshot

new RotationTransition(
  turns: new AlwaysStoppedAnimation(15 / 360),
  child: new Text("Lorem ipsum"),
)

If you want to rotate something 90, 180, or 270 degrees, you can use a RotatedBox.

screenshot

new RotatedBox(
  quarterTurns: 1,
  child: new Text("Lorem ipsum")
)

Solution 2 - Dart

You can use Transform.rotate to rotate your widget. I used Text and rotated it with 45˚ (π/4)

Example:

import 'dart:math' as math;

Transform.rotate(
  angle: -math.pi / 4,
  child: Text('Text'),
)

enter image description here

Solution 3 - Dart

enter image description here

If you are working with a canvas (as in a CustomPaint widget), you can rotate 15 degrees like this:

import 'dart:math' as math;

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    canvas.save();

    // rotate the canvas
    final degrees = 15;
    final radians = degrees * math.pi / 180;
    canvas.rotate(radians);

    // draw the text
    final textStyle = TextStyle(color: Colors.black, fontSize: 30);
    final textSpan = TextSpan(text: 'Hello, world.', style: textStyle);
    TextPainter(text: textSpan, textDirection: TextDirection.ltr)
      ..layout(minWidth: 0, maxWidth: size.width)
      ..paint(canvas, Offset(0, 0));

    canvas.restore();
  }

  @override
  bool shouldRepaint(CustomPainter old) {
    return false;
  }
}

However, if you are doing something simple then I would use a RotatedBox or Transform.rotate as suggested by the other answers.

Solution 4 - Dart

There is Two Main Flutter Widget available for this functionality, RotationTransition and Transform.rotate

another supported option is RotatedBox but this rotate widget only supports quarter turns, which means they support vertical and only horizontal orientation. and if you rotate already created widgets like Container so for the container by transformAlignmentyou can rotate widget.

RotationTransition Widget example:-

           RotationTransition(
                turns: AlwaysStoppedAnimation(15 / 360),
                child: Text("flutter is awesome")
           )

Transform.rotate Widget example :-

    Transform.rotate(
        angle: 15 * math.pi / 180, 
        child: Text("flutter is awesome")
          )

Solution 5 - Dart

Container(
          decoration: BoxDecoration(borderRadius: BorderRadius.circular(50), color: Color(0xffF6F8FF),),
          width: MediaQuery.of(context).size.width*0.6,
          height: MediaQuery.of(context).size.height*0.4,
          alignment:
          new Alignment(0, 0),
          transform:
          new Matrix4.translationValues(MediaQuery.of(context).size.width * 0.55, -250.0, 0.0)
              ..rotateZ(28 * 3.1415927 / 180),
          ),

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSeth LaddView Question on Stackoverflow
Solution 1 - DartCollin JacksonView Answer on Stackoverflow
Solution 2 - DartCopsOnRoadView Answer on Stackoverflow
Solution 3 - DartSuragchView Answer on Stackoverflow
Solution 4 - Dartshirsh shuklaView Answer on Stackoverflow
Solution 5 - DartHamza JavedView Answer on Stackoverflow