我想知道如何设置宽度以匹配父级布局宽度
new Container(
width: 200.0,
padding: const EdgeInsets.only(top: 16.0),
child: new RaisedButton(
child: new Text(
"Submit",
style: new TextStyle(
color: Colors.white,
)
),
colorBrightness: Brightness.dark,
onPressed: () {
_loginAttempt(context);
},
color: Colors.blue,
),
),
我对 Expanded
小部件了解一点,但 Expanded
将 View 扩展到两个方向,我不知道该怎么做。
最佳答案
更新:
在 Flutter 2.0 中,RaisedButton
已被弃用,取而代之的是 ElevatedButton
。您可以像这样使用 minimumSize
:
ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: Size.fromHeight(40), // fromHeight use double.infinity as width and 40 is the height
),
onPressed: () {},
child: Text('Text Of Button'),
)
Flutter 低于 2.0 的旧答案:
正确的解决方案是使用 SizedBox.expand
小部件,它强制其 child
匹配其父级的大小。
SizedBox.expand(
child: RaisedButton(...),
)
有很多选择,可以或多或少地进行自定义:
SizedBox(
width: double.infinity,
// height: double.infinity,
child: RaisedButton(...),
)
或使用 ConstrainedBox
ConstrainedBox(
constraints: const BoxConstraints(minWidth: double.infinity),
child: RaisedButton(...),
)
https://stackoverflow.com/questions/50014342/