问题描述
我想知道如何设置宽度以匹配父级布局宽度
I want to know that how can I set a width to match parent layout width
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
将视图扩展到两个方向,我不知道该怎么做.
I know about little bit on Expanded
widget but Expanded
expands view to both direction, i dont know how to do it.
推荐答案
更新:
在 Flutter 2.0 中 RaisedButton
已被弃用并由 ElevatedButton
取代.你可以像这样使用 minimumSize
:
With Flutter 2.0 RaisedButton
is deprecated and replaced by ElevatedButton
. you can use minimumSize
like this:
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 的旧答案:
Old answer for Flutter less than 2.0:
正确的解决方案是使用 SizedBox.expand
小部件,它强制其 child
匹配其父级的大小.
The correct solution would be to use the SizedBox.expand
widget, which enforces its child
to match its parent's size.
SizedBox.expand(
child: RaisedButton(...),
)
有许多替代方案,可以或多或少地进行自定义:
There are many alternatives, which allows for more or less customization:
SizedBox(
width: double.infinity,
// height: double.infinity,
child: RaisedButton(...),
)
或使用 ConstrainedBox
ConstrainedBox(
constraints: const BoxConstraints(minWidth: double.infinity),
child: RaisedButton(...),
)
这篇关于如何使按钮宽度匹配父级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!