Flutter 布局参考手册:Container装饰和变换
Container
最常用的部件之一 —— 并且它之所以这么常用是有原因的:
用于布局工具的 Container
如果你没有指定 Container 的 height 和 width,它将和 child 的大小相同
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Container as a layout')),
body: Container(
color: Colors.yellowAccent,
child: Text("Hi"),
),
);
}
复制代码
如果你想要 Container 扩大到和它的父级元素相等,对 height 和 width 属性使用 double.infinity
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Container as a layout')),
body: Container(
height: double.infinity,
width: double.infinity,
color: Colors.yellowAccent,
child: Text("Hi"),
),
);
}
复制代码
Container 的装饰
你可以使用 color 属性来改变 Container 的背景色,但是 decoration 和 foregroundDecoration 则可以做更多。(使用这两个属性,你可以彻底改变 Container 的外观,这部分我将在后续讨论,因为这部分内容很多) decoration 总会放置在 child 后面,而 foregroundDecoration 则在 child 的上面。
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Container.decoration')),
body: Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(color: Colors.yellowAccent),
child: Text("Hi"),
),
);
}
复制代码
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Container.foregroundDecoration')),
body: Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(color: Colors.yellowAccent),
foregroundDecoration: BoxDecoration(color: Colors.red.withOpacity(0.5)),
child: Text("Hi"),
),
);
}
复制代码
Container 的变换
如果你不想使用 Transform 部件来改变你的布局,你可以使用 Container 的 transform 属性
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Container.transform')),
body: Container(
height: 300,
width: 300,
transform: Matrix4.rotationZ(pi / 4),
decoration: BoxDecoration(color: Colors.yellowAccent),
child: Text(
"Hi",
textAlign: TextAlign.center,
),
),
);
}
复制代码
作者:Yuqi
链接:https://juejin.im/post/5cfe0d136fb9a07efc497d7d
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
THE END







