我想对容器小部件(包含一些其他小部件)执行非常简单的 2D 旋转。该小部件将围绕中心的单个固定点旋转,不会变形。
我尝试将
transform
属性与 Matrix4.rotationZ
一起使用,这在某种程度上有效 - 但锚点位于 左上角,而不是在 center。有没有一种简单的方法来指定该锚点?
此外,有没有一种更简单的方法可以不需要 Matrix4 来 2D 旋转这个小部件?
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,
);
RotatedBox(
quarterTurns: 3,
child: Container(
color:Colors.red
),
),
容器嵌套在
Transform widget中。使用 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,
);
使用变换:
Transform(
transform: Matrix4.rotationZ(...),
alignment: FractionalOffset.center,
child: Container(...)
或者在Container中设置transformAlignment
值:
Container(
...
transform: Matrix4.rotationZ(...),
transformAlignment: Alignment.center,
)
您可以为自己创建一个小部件来执行此操作,从而同时解决其他问题(隐藏 Matrix4)。
深入了解 Flutter 中的 Transform Widgets
: 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),
),
),
),
),