简体   繁体   English

Flutter如何防止孩子与父母一起旋转?

[英]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.

相关问题 如何将 Map 从孩子传给父母 Flutter - How to Pass Map From Child to Parent Flutter Flutter:如何防止我的主要小部件旋转并仍然跟踪它的方向? - Flutter: How to prevent my main widget from rotating and still keep track of it's orientation? Flutter:如何从子小部件更新子(和父)小部件的 state(父小部件得到更新,子小部件没有) - Flutter: How to update state of child (and parent) widget from child widget (parent widget gets update, child does not) 从子级到父级 Flutter 的回调 - Callback from child to parent Flutter 如何从子小部件调用父小部件中的 function | Flutter - How to Call a function in parent widget from child widget | Flutter Flutter - 如何将 focusNode 从 Parent 设置为 Child 小部件? - Flutter - How to set focusNode from Parent to Child widget? 如何在flutter中从父小部件调用子小部件的initState()方法 - How to Invoke initState() method of child widget from parent widget in flutter 如何从 Flutter 中的子小部件调用父小部件 function - How to call parent widget function from child widget in Flutter Flutter 如何从子部件更改父 BottomNavigationBar 索引 - Flutter how to change the parent BottomNavigationBar index from a child widget 从子到父 class 回调 flutter 中的 function - Callback function in flutter from child to parent class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM