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

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

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


当前回答

TextButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colors.blue),
                fixedSize: MaterialStateProperty.all(
                  Size(double.maxFinite, 50.0),
                ),
              ),
              onPressed: () {},
              child: Text('Upgrade to Premium'),
            ),

其他回答

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'),
  ),
),
 new SizedBox(
  width: 100.0,
     child: new RaisedButton(...),
)

使用ListTile也可以,因为列表填充了整个宽度:

ListTile(
  title: new RaisedButton(...),
),

在Flutter 2.0中,RaisedButton已弃用,并被ElevatedButton取代。

ElevatedButton小部件的minimumSize属性正是这样做的。

示例代码:

ElevatedButton(
            style: ElevatedButton.styleFrom(
              primary: Colors.green,
              onPrimary: Colors.white,
              shadowColor: Colors.greenAccent,
              elevation: 3,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(20.0)),
              minimumSize: Size(100, 40), //////// HERE
            ),
            onPressed: () {},
            child: Text('MyButton'),
          )

这在一个自包含的小部件中为我工作。

  Widget signinButton() {
    return ButtonTheme(
      minWidth: double.infinity,
      child: new FlatButton(
        onPressed: () {},
        color: Colors.green[400],
        textColor: Colors.white,
        child: Text('Flat Button'),
      ),
    );
  }

// It can then be used in a class that contains a widget tree.