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

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

更新:

在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(...),
)

Container(
  width: double.infinity,
  child: RaisedButton(...),
),

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

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

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

下面的代码为我工作

       ButtonTheme(
            minWidth: double.infinity,
            child: RaisedButton(child: Text("Click!!", style: TextStyle(color: Colors.white),), color: Colors.pink, onPressed: () {}))

@Mohit Suthar,

找到了一个最好的解决方案,匹配父宽以及高度如下

new Expanded(
          child: new Container(
              padding: EdgeInsets.all(16.0),
              margin: EdgeInsets.all(16.0),
              decoration: new BoxDecoration(
                  color: Colors.white,
                  borderRadius:
                      const BorderRadius.all(const Radius.circular(8.0)),
                  border: new Border.all(color: Colors.black, width: 1.0)),
              child: new Text("TejaDroid")),
        ), 

在这里,您可以检查扩展控制器获得完整的宽度和高度。


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

  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.

最简单的方法是使用一个包装在容器中的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,
                      ),
                    ),
                  ),
                )

输出:


在上面给定的代码中给出match-parent宽度或高度的最简单方法。

...
width: double.infinity,
height: double.infinity,
...

这对我很有用。

    SizedBox(
             width: double.maxFinite,
             child: RaisedButton(
                 materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                 child: new Text("Button 2"),
                 color: Colors.lightBlueAccent,
                 onPressed: () => debugPrint("Button 2"),
              ),
     ), 

对于match_parent,您可以使用

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

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

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

这个方法对我很管用

width: MediaQuery.of(context).size.width-100,

你可以用MaterialButton来做

MaterialButton(
     padding: EdgeInsets.all(12.0),
     minWidth: double.infinity,
     onPressed: () {},
    child: Text("Btn"),
)

您可以通过设置小部件的匹配父项

1)设置宽度为两倍。无穷:

new Container(
          width: double.infinity,
          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,
          ),
        ),

2)使用MediaQuery:

new Container(
          width: MediaQuery.of(context).size.width,
          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,
          ),
        ),

RaisedButton ( 孩子:行( mainAxisAlignment: MainAxisAlignment.center, 孩子们:文本(“提交”), ) ) 这对我很管用。


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

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

    )
)

         OutlineButton(
              onPressed: () {
                logInButtonPressed(context);
              },
              child: Container(
                width: MediaQuery.of(context).size.width / 2,
                child: Text(
                  “Log in”,
                  textAlign: TextAlign.center,
                ),
              ),
            )

这样的东西对我很有用。


用一个中心小部件包装您的(具有固定宽度的子小部件)。这将使你的小部件居中:

Center(child:Container(width:250,child:TextButton(child:Text("Button Name),),)

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

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

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

现场演示


制作全宽按钮的方法有很多种。但我认为你应该理解在不同场景下制作全宽度小部件的概念:

当你使用嵌套小部件时,很难识别父小部件的宽度。简单地说,您不能在嵌套的小部件中指定宽度。所以你应该使用扩展或列与CrossAxisAlignment拉伸。

在其他情况下,您可以使用媒体查询宽度或double.infinity。

这里有一些嵌套小部件和单个小部件的例子:

第一:

Expanded(   // This will work for nested widgets and will take width of first parent widget.
  child: MaterialButton(
    onPressed: () {},
    child: const Text("Button Text"),
    color: Colors.indigo,
    textColor: Colors.white,
  )
)

第二:

Column( // This will not work if parent widget Row.
  crossAxisAlignment: CrossAxisAlignment.stretch,
  children: <Widget>[
    MaterialButton(
      onPressed: () {},
      child: const Text("Button Text"),
      color: Colors.indigo,
      textColor: Colors.white,
    )
  ]
)

第三:

ButtonTheme( // if use media query, then will not work for nested widgets.
  minWidth: double.infinity,  //Or use 'width: MediaQuery.of(context).size.width'
  child: MaterialButton(
    onPressed: () {},
    child: const Text("Button Text"),
    color: Colors.indigo,
    textColor: Colors.white,
  )
)

出:

SizedBox( // if use media query, then will not work for nested widgets.
  width: double.infinity, //Or use 'width: MediaQuery.of(context).size.width'
  child: MaterialButton(
    onPressed: () {},
    child: const Text("Button Text"),
    color: Colors.indigo,
    textColor: Colors.white,
  )
)

第五:

Container( // if use media query, then will not work for nested widgets.
  width: double.infinity, //Or use 'width: MediaQuery.of(context).size.width'
  child: MaterialButton(
    onPressed: () {},
    child: const Text("Button Text"),
    color: Colors.indigo,
    textColor: Colors.white,
  )
)

从我的角度来看,推荐的将是第一个。你也可以改变MaterialButton为ElevatedButton或TextButton或RaisedButton(折旧)或任何其他小部件。

干杯!


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

TRY

Flexible(
 child: Row(
  children: [
    ElevatedButton(
     onPressed: () {},
     child: Text("love you}"),
   ),
  ],
 ),
),