颤动:如何使按钮扩展到它的父级大小?

fuz*_*cal 21 flutter flutter-layout

我正在尝试创建方形按钮,但Expanded似乎与容器的工作方式不同.以下面的代码为例

new Expanded(
 flex: 2,
   child: new Column(
     children: <Widget>[
       new Expanded(
         child:new Row(
           children: <Widget>[
             new Expanded(child: new MaterialButton(...)),
             new Expanded(child: new MaterialButton(....)),

             new Expanded(child: new Container(color: Colors.red)),
             new Expanded(child: new Container(color: Colors.green)),
           ]
         )
       )
     ],
   )
 )

 ....
Run Code Online (Sandbox Code Playgroud)

它显示两个水平扩展但不垂直的按钮.同时,容器将水平和垂直扩展.如果我执行以下操作,则会产生相同的效果:

new Expanded(
 flex: 2,
   child: new Column(
     children: <Widget>[
       new Expanded(
         child:new Column(
           children: <Widget>[
             new Expanded(child: new MaterialButton(...)),
             new Expanded(child: new MaterialButton(....)),

             new Expanded(child: new Container(color: Colors.red)),
             new Expanded(child: new Container(color: Colors.green)),
           ]
         )
       )
     ],
   )
 )

 ....
Run Code Online (Sandbox Code Playgroud)

我把Row改为Column的地方.按钮将垂直展开,但不会水平展开,而容器将同时展开.

有没有办法让我的按钮扩展到垂直和水平适合他们的父母?

azi*_*iza 39

将crossAxisAlignment属性添加到您的Row;

crossAxisAlignment: CrossAxisAlignment.stretch
Run Code Online (Sandbox Code Playgroud)

  • 还要考虑SizedBox (3认同)

Rod*_*voi 13

在 Flutter 2.+ 中考虑这个解决方案:


ElevatedButton(
  style: ElevatedButton.styleFrom(
    minimumSize: const Size(double.infinity, double.infinity), // <--- this line helped me
  ),
  onPressed: () {},
  child: Icon(
    Icons.keyboard_return
  ),
)
Run Code Online (Sandbox Code Playgroud)


Gün*_*uer 12

包装用ButtonTheme与minWidth: double.infinity允许提供约束

ButtonTheme(
  minWidth: double.infinity,
  child: MaterialButton(
    onPressed: () {},
    child: Text('Raised Button'),
  ),
),
Run Code Online (Sandbox Code Playgroud)

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

  MaterialButton(
    onPressed: () {},
    child: SizedBox.expand(
      width: double.infinity, 
      child: Text('Raised Button'),
    ),
  ),
Run Code Online (Sandbox Code Playgroud)


Cop*_*oad 7

你也可以尝试一下

  1. 使用SizedBox

    SizedBox(
      width: double.maxFinite, // set width to maxFinite
      child: RaisedButton(...),
    )
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用MaterialButton的minWidth财产。

    MaterialButton(
      minWidth: double.maxFinite, // set minWidth to maxFinite
      color: Colors.blue,
      onPressed: () {},
      child: Text("Button"),
    )
    
    Run Code Online (Sandbox Code Playgroud)


jit*_*555 7

我们可以添加 Button insider Container。

解决方案:

Container(
            width: double.infinity,
            child: RaisedButton(
              onPressed: null,
              child: Text('NEXT'),
            ),
          )
Run Code Online (Sandbox Code Playgroud)

  • 首先,如果您不打算使用除“宽度”和“高度”之外的任何“Container”属性,则应始终使用“SizedBox”而不是“Container”。其次,不要直接跳到“double.infinity”,你应该尝试“double.maxFinite”,我现在想不出例子,但“double.infinity”有时会给你带来麻烦。 (2认同)

小智 5

您可以在 child:column 下插入

crossAxisAlignment:CrossAxisAlignment.stretch

我的代码

我的结果