Layout 集装箱边缘颤振布局
我的颤振布局有问题 我有一个简单的容器,左右边距为20.0 在这个容器里我有另一个容器 但此容器不适合仅位于左侧的父容器。 我不知道为什么会这样 这是我的密码:Layout 集装箱边缘颤振布局,layout,dart,containers,margin,flutter,Layout,Dart,Containers,Margin,Flutter,我的颤振布局有问题 我有一个简单的容器,左右边距为20.0 在这个容器里我有另一个容器 但此容器不适合仅位于左侧的父容器。 我不知道为什么会这样 这是我的密码: @override Widget build(BuildContext context) { return new Scaffold( backgroundColor: Colors.white, body: new Container( margin: new EdgeInset
@override
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.white,
body: new Container(
margin: new EdgeInsets.symmetric(horizontal: 20.0),
child: new Container(
)
),
);
}
可以使用左值和右值:)
你可以尝试:到任何一个边缘的边缘
new Container(
margin: const EdgeInsets.only(left: 20.0, right: 20.0),
child: new Container()
)
你可以尝试:到任何边缘的边缘
new Container(
margin: const EdgeInsets.all(20.0),
child: new Container()
)
如果需要在
小部件,考虑使用[ MyActual.Of ]来获得这些值而不是
使用[dart:ui.window]中的值,以便收到更改通知
new Container(
margin: EdgeInsets.fromWindowPadding(padding, devicePixelRatio),
child: new Container()
)
容器(
保证金:所有(10),
对齐:对齐.bottomCenter,
装饰:盒子装饰(
梯度:线性梯度(
开始:Alignment.topCenter,
结束:对齐。底部中心,
颜色:[
颜色。黑色。带alpha(0),
颜色。黑色12,
颜色。黑色45
],
),
),
子:文本(
“前景文本”,
样式:TextStyle(颜色:Colors.white,fontSize:20.0),
),
),
你确定吗?我们可以清楚地看到两侧的两个竖条。是的,父容器是正确的,但是子容器不适合父容器(标记为蓝色区域)。在Inspector my Faults中选择了错误的小部件如果您想知道子小部件在父部件中的位置,只需将子部件包装在容器中,并为容器指定颜色即可“边距:EdgeInsets.fromLTRB(20.0,0.0,20.0,0.0),”可以使用。为什么要编写const
?@Cold_Classconst
是一个关键字,表示构造函数是const构造函数,即它只能接受编译时的值,而不能像某些变量一样在运行时计算值。@KushagraSaxena这样做有好处吗?比如性能提升?因为我认为将多个元素的边距值存储在一个变量中是非常有用的。@Cold\u Class No它与性能无关。这正是达特说要遵循的惯例。你能添加一些细节来解释你的代码是如何回答这个问题的吗?
new Container(
margin: EdgeInsets.fromWindowPadding(padding, devicePixelRatio),
child: new Container()
)
Container(
margin: EdgeInsets.all(10) ,
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: <Color>[
Colors.black.withAlpha(0),
Colors.black12,
Colors.black45
],
),
),
child: Text(
"Foreground Text",
style: TextStyle(color: Colors.white, fontSize: 20.0),
),
),