Flutter: How to make a button expand to the size of its parent?

半城伤御伤魂 提交于 2019-12-03 09:53:52

Add the crossAxisAlignment property to your Row;

crossAxisAlignment: CrossAxisAlignment.stretch
Günter Zöchbauer

Wrapping with a ButtonTheme with minWidth: double.infinity allows to provide constraints

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

or after https://github.com/flutter/flutter/pull/19416 landed

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

You can also try it

  1. Using SizedBox

    SizedBox(
      width: double.maxFinite, // set width to maxFinite
      child: RaisedButton(...),
    )
    
  2. Use MaterialButton's minWidth property.

    MaterialButton(
      minWidth: double.maxFinite, // set minWidth to maxFinite
      color: Colors.blue,
      onPressed: () {},
      child: Text("Button"),
    )
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!