Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rotate something 15 degrees in Flutter?

Tags:

flutter

dart

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!

like image 209
Seth Ladd Avatar asked May 31 '17 05:05

Seth Ladd


4 Answers

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")
)
like image 141
Collin Jackson Avatar answered Nov 19 '22 05:11

Collin Jackson


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

like image 21
CopsOnRoad Avatar answered Nov 19 '22 05:11

CopsOnRoad


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.

like image 16
Suragch Avatar answered Nov 19 '22 07:11

Suragch


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: which animates the rotation of a widget, mainly we prefer when we need rotation with animation transition.https://api.flutter.dev/flutter/widgets/RotationTransition-class.html
  • Transform.rotate: which applies a rotation paint effect, they Create a widget that transforms its child using a rotation around the center.

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")
          )
like image 6
shirsh shukla Avatar answered Nov 19 '22 06:11

shirsh shukla