66

I am trying to create square buttons, but Expanded doesn't seem to work the same as it does with containers. Take the following code for example

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

 ....

It displays two buttons that are expanded horizontally, but not vertically. At the same time the containers will expand both horizontally and vertically. The same effect occurs if I do the following:

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

 ....

Where I've changed the Row to Column. The buttons will expand vertically, but not horizontally, while the containers will do both.

Is there a way have my buttons expand to fit their parent both vertically and horizontally?

user823447
  • 147
  • 2
  • 11
fuzzylogical
  • 852
  • 1
  • 7
  • 9

10 Answers10

104

Add the crossAxisAlignment property to your Row;

crossAxisAlignment: CrossAxisAlignment.stretch
Shady Aziza
  • 50,824
  • 20
  • 115
  • 113
29

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'),
    ),
  ),
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
12

In Flutter 2.+ consider this solution:


ElevatedButton(
  style: ElevatedButton.styleFrom(
    minimumSize: const Size(double.infinity, double.infinity), // <--- this line helped me
  ),
  onPressed: () {},
  child: Icon(
    Icons.keyboard_return
  ),
)
Rodion Mostovoi
  • 1,205
  • 12
  • 16
6

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"),
    )
    
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
6

We can add Button insider Container.

Solution:

Container(
            width: double.infinity,
            child: RaisedButton(
              onPressed: null,
              child: Text('NEXT'),
            ),
          )
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • 2
    First, you should always use `SizedBox` instead of `Container` if you are not going to use any of the `Container` property except the `width` and `height`. Second, instead of directly jumping to `double.infinity`, you should try `double.maxFinite`, I can't think of an example at this time, but `double.infinity` can sometimes land you in trouble. – CopsOnRoad May 25 '20 at 12:57
  • See this about the diff. I think double.infinite should be used. https://stackoverflow.com/questions/61706455/whats-the-difference-between-double-infinity-and-double-maxfinite-in-dart – Rasmus Christensen Aug 27 '21 at 10:33
4

you can insert under child:column

crossAxisAlignment: CrossAxisAlignment.stretch

My code

My result

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
thaihoang305
  • 49
  • 1
  • 1
0
MaterialButton(
          minWidth: MediaQuery.of(context).size.width, // set minWidth to width of the device screen
          onPressed: () {},
          child: Text("Button"),
        )

for reference https://api.flutter.dev/flutter/widgets/MediaQuery-class.html

0

From documentation and using button theme with styleFrom utility static method you can code like this:

/// To specify buttons with a fixed width and the default height use
/// `fixedSize: Size.fromWidth(320)`. Similarly, to specify a fixed
/// height and the default width use `fixedSize: Size.fromHeight(100)`.
ElevatedButton(
          onPressed: () {},
          child: Text('text'),
          style: ElevatedButton.styleFrom(
      fixedSize: const Size.fromWidth(double.infinity),
    ),
  )

zex_rectooor
  • 692
  • 7
  • 26
0

I have been able to achieve this using Flexible widget.

Wrap your button in a Flexiable widget and set the fit to Flexfit.tight.

Kyros
  • 72
  • 4
-1

VisualDensity is the thing you are looking for. Add it to the ButtonStyle object.

ElevatedButton(
  style: const ButtonStyle(
    visualDensity: VisualDensity(
      horizontal: VisualDensity.maximumDensity,
      vertical: VisualDensity.maximumDensity,
    ),
  ),
  ...
),