How can I rotate a Container widget in 2D around a specified anchor point?

后端 未结 4 2068
余生分开走
余生分开走 2021-02-05 09:44

I\'d like to perform a very simple 2D rotation of a Container widget (that contains a handful of other widgets.) This widget would be rotated around a single fixed point in the

4条回答
  •  天命终不由人
    2021-02-05 10:06

    Per Ian's advice, I gave the following a try. It appears to work, at least in my limited testing.

    The container is nested within a Transform widget. Using alignment allows one to adjust the transform-origin in relative terms, i.e., in the center, the top left, etc.

    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,
    );
    

提交回复
热议问题