28

我想执行一个非常简单的 Container 小部件(包含一些其他小部件)的 2D 旋转。这个小部件将围绕中心的一个固定点旋转,没有变形。

我尝试将transform属性与 一起使用Matrix4.rotationZ,这有点工作 - 但锚点在左上角,而不是在中心。有没有一种简单的方法来指定该锚点?

此外,是否有更简单的方法来 2D 旋转不需要 Matrix4 的小部件?

期望的和实际的转变

var container = new Container (
  child: new Stack (
    children: [
      new Image.asset ( // background photo
        "assets/texture.jpg",
        fit: ImageFit.cover,
      ),
      new Center (
        child: new Container (
          child: new Text (
            "Lorem ipsum",
            style: new TextStyle(
              color: Colors.white,
              fontSize: 42.0,
              fontWeight: FontWeight.w900
            )
          ),
          decoration: new BoxDecoration (
            backgroundColor: Colors.black,
          ),
          padding: new EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 16.0),
          transform: new Matrix4.rotationZ(0.174533), // rotate -10 deg
        ),
      ),
    ],
  ),
  width: 400.0,
  height: 200.0,
);
4

5 回答 5

16

根据伊恩的建议,我尝试了以下方法。它似乎有效,至少在我有限的测试中。

该容器嵌套在一个Transform 小部件中。使用 alignment允许以相对方式调整变换原点,即在中心、左上角等。

var container = new Container (
  child: new Stack (
    children: [
      new Image.asset ( // background photo
        "assets/texture.jpg",
        fit: ImageFit.cover,
      ),
      new Center (
        child: new Transform (
          child: new Container (
            child: new Text (
              "Lorem ipsum",
              style: new TextStyle(
                color: Colors.white,
                fontSize: 42.0,
                fontWeight: FontWeight.w900,
              )
            ),
            decoration: new BoxDecoration (
              backgroundColor: Colors.black,
            ),
            padding: new EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 16.0),              
          ),
          alignment: FractionalOffset.center, // set transform origin
          transform: new Matrix4.rotationZ(0.174533), // rotate -10 deg
        ),
      ),
    ],
  ),
  width: 400.0,
  height: 200.0,
);
于 2017-01-24T01:27:28.927 回答
16

您可以使用 RotatedBox Widget 来旋转任何小部件:

RotatedBox(
    quarterTurns: 3,
    child: Container(
        color:Colors.red
    ),
),
于 2020-08-27T10:39:51.697 回答
8

In flutter 2.2

Use Transform:

Transform(
    transform: Matrix4.rotationZ(...),
    alignment: FractionalOffset.center,
    child: Container(...)

Or set transformAlignment value in Container:

Container(
    ...
    transform: Matrix4.rotationZ(...),
    transformAlignment: Alignment.center,
)
于 2021-05-24T08:52:20.243 回答
4

在旋转之前和之后应用平移(到和从支点)。

您可以自己创建一个小部件来执行此操作,从而同时解决您的其他问题(隐藏 Matrix4)。

于 2017-01-12T00:41:04.370 回答
1

对于那些想像我一样制作应用程序的人,只需使用TransformGesturedetector。请记住不要同时使用 Draggable 和 Gesturedetector,因为您只能拖放但不能缩放小部件:))。

参考: 深入了解 Flutter 中的转换小部件

巴布斯瓦塔纳贝

在此处输入图像描述

             Transform.rotate(
                  alignment: FractionalOffset.center,
                  angle: state.listStickerModel[index].angle,
                  child: Transform(
                    alignment: FractionalOffset.center,
                    transform: new Matrix4.diagonal3(vector.Vector3(
                        state.listStickerModel[index].zoom,
                        state.listStickerModel[index].zoom,
                        state.listStickerModel[index].zoom)),
                    child: GestureDetector(
                      onScaleStart: (detail) {
                        _editPhotoBloc.add(UpdateSticker(
                          index: index,
                          offset: detail.focalPoint,
                          previousZoom: state.listStickerModel[index].zoom,
                        ));
                      },
                      onScaleUpdate: (detail) {
                        _editPhotoBloc.add(UpdateSticker(
                            index: index,
                            offset: Offset(detail.localFocalPoint.dx,
                                detail.focalPoint.dy),
                            angle: detail.rotation,
                            zoom:
                                state.listStickerModel[index].previousZoom *
                                    detail.scale));
                      },
                      child: Container(
                        alignment: Alignment.center,
                        child: SvgPicture.asset(
                            state.listStickerModel[index].src),
                      ),
                    ),
                  ),
                ),
于 2020-07-07T15:29:43.073 回答