Flutter文档显示了将"div"旋转15度的示例,包括HTML/CSS和Flutter代码:
Flutter代码是:
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),
),
),
);
Run Code Online (Sandbox Code Playgroud)
及相关部件new Transform
,并alignment: FractionalOffset.center
与transform: new Matrix4.identity()..rotateZ(15 * 3.1415927 / 180)
我很好奇,是否有更简单的方法来旋转Container
颤动?"15度"的情况是否有空头?
谢谢!
Col*_*son 54
在移动应用程序中,我认为让事情开始旋转15度并永远呆在那里是很少见的.因此,如果您计划随时间调整旋转,那么这可能是Flutter对旋转的支持更好的原因.
感觉有点矫枉过正,但RotationTransition
有一个AlwaysStoppedAnimation
会完全达到你想要的效果.
new RotationTransition(
turns: new AlwaysStoppedAnimation(15 / 360),
child: new Text("Lorem ipsum"),
)
Run Code Online (Sandbox Code Playgroud)
如果要旋转90度,180度或270度,可以使用RotatedBox
.
new RotatedBox(
quarterTurns: 1,
child: new Text("Lorem ipsum")
)
Run Code Online (Sandbox Code Playgroud)
Cop*_*oad 28
您可以使用Transform.rotate
旋转窗口小部件.我用Text
45˚(π/ 4)旋转它
例:
Widget build(BuildContext context) {
return Transform.rotate(angle: - math.pi / 4, child: Text("Text"),);
}
Run Code Online (Sandbox Code Playgroud)
shi*_*kla 17
有两个主要的 Flutter Widget 可用于此功能:RotationTransition和Transform.rotate
另一个受支持的选项是RotatedBox,但此旋转小部件仅支持四分之一圈,这意味着它们支持垂直方向且仅支持水平方向。如果您旋转已经创建的小部件(例如容器),则通过TransformAlignment对于容器,您可以旋转小部件。
RotationTransition小部件示例:-
RotationTransition(
turns: AlwaysStoppedAnimation(15 / 360),
child: Text("flutter is awesome")
)
Run Code Online (Sandbox Code Playgroud)
Transform.rotate Widget 示例:-
Transform.rotate(
angle: 15 * math.pi / 180,
child: Text("flutter is awesome")
)
Run Code Online (Sandbox Code Playgroud)
Sur*_*gch 12
如果您正在使用画布(如在 CustomPaint 小部件中),您可以像这样旋转 15 度:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您正在做一些简单的事情,那么我会按照其他答案的建议使用 aRotatedBox
或Transform.rotate
。
归档时间: |
|
查看次数: |
14118 次 |
最近记录: |