[英]Flutter - Transform scale in center of Container
我正在尝试创建一个翻转效果,但在开始动画之前,我试图让转换保持在容器的中心。
但即使使用FractionalOffset(0.5, 0.5)
或Center()
包装Transform()
结果仍保留在Container
的顶部,如下图所示。
你能帮我把它放在容器的中央吗?
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Container(
alignment: new FractionalOffset(0.5, 0.5),
height: 144.0,
width: 360.0,
decoration: new BoxDecoration(
border: new Border.all(color: new Color(0xFF9E9E9E))
),
child: new Transform(
transform: new Matrix4.identity()..scale(1.0, 0.05),
child: new Container(
decoration: new BoxDecoration(
color: new Color(0xFF005CA9),
),
),
)
)
],
),
),
);
}
}
Transform不会影响其子项的布局,它只会影响子项的绘制方式。
如果你想用你的Transform
进行缩放,你可以传递一个alignment: FractionalOffset.center
。
如果您想使用布局原语将项目居中,则可以使用FractionallySizedBox
而不是Transform
。
有关如何制作翻转效果动画的完整示例,请参阅我对您的其他问题的回答。
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Container(
alignment: new FractionalOffset(0.5, 0.5),
height: 144.0,
width: 360.0,
decoration: new BoxDecoration(
border: new Border.all(color: new Color(0xFF9E9E9E))
),
child: new Transform(
alignment: FractionalOffset.center,
transform: new Matrix4.identity()..scale(1.0, 0.05),
child: new Container(
decoration: new BoxDecoration(
color: new Color(0xFF005CA9),
),
),
)
)
],
),
),
);
}
}
只需将其添加到您的容器中:
alignment: FractionalOffset.center
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.