我想知道如何设置一个宽度,以匹配父布局宽度
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,
),
),
我知道一点点关于扩展小部件,但扩展扩展视图到两个方向,我不知道如何做到这一点。
经过一番研究,我找到了一些解决方案,感谢@Günter Zöchbauer,
我用了列而不是容器和
将属性设置为列CrossAxisAlignment。拉伸填充匹配按钮的父元素
new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new RaisedButton(
child: new Text(
"Submit",
style: new TextStyle(
color: Colors.white,
)
),
colorBrightness: Brightness.dark,
onPressed: () {
_loginAttempt(context);
},
color: Colors.blue,
),
],
),
最简单的方法是使用一个包装在容器中的FlatButton,默认情况下,按钮接受其父的大小,并为容器分配所需的宽度。
Container(
color: Colors.transparent,
width: MediaQuery.of(context).size.width,
height: 60,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0),
),
onPressed: () {},
color: Colors.red[300],
child: Text(
"Button",
style: TextStyle(
color: Colors.black,
fontFamily: 'Raleway',
fontSize: 22.0,
),
),
),
)
输出:
更新:
在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'),
)
颤振小于2.0的旧答案:
正确的解决方案是使用sizebox。扩展小部件,它强制其子部件与其父部件的大小相匹配。
SizedBox.expand(
child: RaisedButton(...),
)
有很多选择,允许或多或少的定制:
SizedBox(
width: double.infinity,
// height: double.infinity,
child: RaisedButton(...),
)
或者使用ConstrainedBox
ConstrainedBox(
constraints: const BoxConstraints(minWidth: double.infinity),
child: RaisedButton(...),
)