我想知道如何设置一个宽度,以匹配父布局宽度

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,
  ),
),

我知道一点点关于扩展小部件,但扩展扩展视图到两个方向,我不知道如何做到这一点。


当前回答

您可以设置fixedSize。将ButtonStyle的宽度设置为一个非常大的数字,如double.maxFinite。如果你不想指定高度,你也可以使用Size.fromWidth()构造函数:

ElevatedButton(
  child: const Text('Button'),
  style: ElevatedButton.styleFrom(
    fixedSize: const Size.fromWidth(double.maxFinite),
  ),
),

现场演示

其他回答

最基本的方法是通过将容器的宽度定义为无穷大来使用容器。参见下面的代码示例

Container(
    width: double.infinity,
    child:FlatButton(
        onPressed: () {
            //your action here
        },
        child: Text("Button"),

    )
)
 new SizedBox(
  width: 100.0,
     child: new RaisedButton(...),
)
Container(
  width: double.infinity,
  child: RaisedButton(...),
),

对于match_parent,您可以使用

SizedBox(
  width: double.infinity, // match_parent
  child: RaisedButton(...)
)

对于任何可以使用的特定值

SizedBox(
  width: 100, // specific value
  child: RaisedButton(...)
)

size属性可以使用ButtonTheme和minWidth: double.infinity来提供

ButtonTheme(
  minWidth: double.infinity,
  child: MaterialButton(
    onPressed: () {},
    child: Text('Raised Button'),
  ),
),

或者在https://github.com/flutter/flutter/pull/19416登陆后

MaterialButton(
  onPressed: () {},
  child: SizedBox.expand(
    width: double.infinity, 
    child: Text('Raised Button'),
  ),
),