[英]Flutter How to prevent child from Rotating with Parent?
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. 否则,我可能不得不将子文本包装在另一个Transform.rotate中,然后将其旋转回去。
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,
),
));
}
}
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. 您可以添加第二个Transform.rotate ,包装您的Text
Widget,如下面的代码所示。 I added red color to the Container
to display the rotated Container
. 我向Container
添加了红色以显示旋转的Container
。
Before adding the second rotate: 在添加第二个旋转之前:
After adding the second 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,
),
),
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.