212

I want to know that how can I set a width to match parent layout width

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

I know about little bit on Expanded widget but Expanded expands view to both direction, i dont know how to do it.

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67

25 Answers25

438

Update:

With Flutter 2.0 RaisedButton is deprecated and replaced by ElevatedButton. you can use minimumSize like this:

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

Old answer for Flutter less than 2.0:

The correct solution would be to use the SizedBox.expand widget, which enforces its child to match its parent's size.

SizedBox.expand(
  child: RaisedButton(...),
)

There are many alternatives, which allows for more or less customization:

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

or using a ConstrainedBox

ConstrainedBox(
  constraints: const BoxConstraints(minWidth: double.infinity),
  child: RaisedButton(...),
)
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • 55
    SizedBox.expand will make the button take full width and height, which is not the question about. The question is about a button covering full width only not height. – Vinoth Kumar May 28 '18 at 10:35
  • 3
    @RémiRousselet Vinoth's comment is valid. Since this is now the accepted answer, could you add the right constructors for SizedBox to specifically expand only the width? – user823447 Apr 21 '19 at 00:04
  • I'm receiving this error `Failed assertion: line 1645 pos 12: '!_debugDoingThisLayout': is not true.` – minato Jan 18 '20 at 09:40
  • 4
    Why couldn't we just change the width of the container like this: `Container(width: double.infinity)` instead of wrapping it with another widget like `SizedBox`. It's much simpler to read and brings the same results. – Sameen Jun 19 '20 at 16:51
  • So if I have to understand what the Size.fromHeight exactly does, what's a good place to learn? – Harsha May 31 '22 at 06:17
  • ` double.infinity` will getting the full width thank you – Mohamed Raza Sep 12 '22 at 17:14
52
Container(
  width: double.infinity,
  child: RaisedButton(...),
),
Vinoth Kumar
  • 12,637
  • 5
  • 32
  • 38
44

After some research, I found out some solution, and thanks to @Günter Zöchbauer,

I used column instead of Container and

set the property to column CrossAxisAlignment.stretch to Fill match parent of Button

    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,
                ),
              ],
            ),
Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
  • 11
    `Column`/`Row` shouldn't be used for single-child layout. Use the single-child alternative instead. Such as `Align`, `SizedBox`, and similar. – Rémi Rousselet Apr 25 '18 at 08:10
42

The size attribute can be provided using ButtonTheme with minWidth: double.infinity

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'),
  ),
),
HardlyNoticeable
  • 497
  • 9
  • 26
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
32

The simplest way is to use a FlatButton wrapped inside a Container, The button by default takes the size of its parent and so assign a desired width to the Container.

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

Output:

enter image description here

iDecode
  • 22,623
  • 19
  • 99
  • 186
Mahesh Jamdade
  • 17,235
  • 8
  • 110
  • 131
13

You can set match parent of the widget by

1) set width to double.infinity :

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) Use 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,
          ),
        ),
iDecode
  • 22,623
  • 19
  • 99
  • 186
Hetal
  • 471
  • 4
  • 14
7

@Mohit Suthar,

Found one of the best solution for match parent to width as well as height as below

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

Here you can check that the Expanded Controller acquire whole remain width and height.

TejaDroid
  • 6,561
  • 4
  • 31
  • 38
7

For match_parent you can use

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

For any particular value you can use

SizedBox(
  width: 100, // specific value
  child: RaisedButton(...)
)
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
6

The simplest way to give match-parent width or height in the given code above.

...
width: double.infinity,
height: double.infinity,
...
iDecode
  • 22,623
  • 19
  • 99
  • 186
bunny
  • 1,316
  • 2
  • 13
  • 22
5

There are many ways to make full width button. But I think you should understand the concept of making full width widgets in different scenarios:

When you are using nested widgets then it is hard to identify width of parent widget. Simply you can't specify width in nested widgets. So you should use either Expanded or Column with CrossAxisAlignment as Strech.

In other cases, you can use media query width or double.infinity.

Here are some examples for Nested widgets and single widget:

First:

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

Second:

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

Third:

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

Forth:

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

Fifth:

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

From my point of view, recommended will be the First one. Also you can change MaterialButton to ElevatedButton or TextButton or RaisedButton (Depreciated) or any other widget.

Cheers!

Naimatullah
  • 3,749
  • 2
  • 13
  • 12
3

you can do that with MaterialButton

MaterialButton(
     padding: EdgeInsets.all(12.0),
     minWidth: double.infinity,
     onPressed: () {},
    child: Text("Btn"),
)
iDecode
  • 22,623
  • 19
  • 99
  • 186
Mahmoud Abu Alheja
  • 3,343
  • 2
  • 24
  • 41
3

You can set the fixedSize.width of the ButtonStyle to a very large number, like double.maxFinite. You can also use Size.fromWidth() constructor if you don't want to specify the height:

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

Live Demo

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
2

The most basic approach is using Container by define its width to infinite. See below example of code

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

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

Something like this works for me.

Adam Smaka
  • 5,977
  • 3
  • 50
  • 55
1

The Following code work for me

       ButtonTheme(
            minWidth: double.infinity,
            child: RaisedButton(child: Text("Click!!", style: TextStyle(color: Colors.white),), color: Colors.pink, onPressed: () {}))
Rinkesh
  • 3,150
  • 28
  • 32
1

This is working for me in a self contained widget.

  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.
Anh Pham
  • 2,108
  • 9
  • 18
  • 29
PaulDef515
  • 11
  • 3
1

This is working for me.

    SizedBox(
             width: double.maxFinite,
             child: RaisedButton(
                 materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                 child: new Text("Button 2"),
                 color: Colors.lightBlueAccent,
                 onPressed: () => debugPrint("Button 2"),
              ),
     ), 
kelvincer
  • 5,929
  • 6
  • 27
  • 32
1

RaisedButton( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [Text('Submit')], ) ) It works for me.

TaoZang
  • 1,690
  • 2
  • 15
  • 15
1

With Flutter 2.0 RaisedButton is deprecated and replaced by ElevatedButton.

minimumSize property of ElevatedButton widget exactly does that.

Example code:

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'),
          )
RuslanBek
  • 1,592
  • 1
  • 14
  • 30
0

Using a ListTile also works as well, since a list fills the entire width:

ListTile(
  title: new RaisedButton(...),
),
cokeman19
  • 2,405
  • 1
  • 25
  • 40
Oush
  • 3,090
  • 23
  • 22
0
 new SizedBox(
  width: 100.0,
     child: new RaisedButton(...),
)
Raj008
  • 3,539
  • 2
  • 28
  • 26
0

This one worked for me

width: MediaQuery.of(context).size.width-100,
minato
  • 2,028
  • 1
  • 18
  • 30
0

Wrap your (child widget having a fixed width) with a center widget. This will center your widget:

Center(child:Container(width:250,child:TextButton(child:Text("Button Name),),)
mousetail
  • 7,009
  • 4
  • 25
  • 45
A.K.J.94
  • 492
  • 6
  • 14
0
TextButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colors.blue),
                fixedSize: MaterialStateProperty.all(
                  Size(double.maxFinite, 50.0),
                ),
              ),
              onPressed: () {},
              child: Text('Upgrade to Premium'),
            ),
Syed Umair
  • 1,480
  • 1
  • 13
  • 14
-1

TRY

Flexible(
 child: Row(
  children: [
    ElevatedButton(
     onPressed: () {},
     child: Text("love you}"),
   ),
  ],
 ),
),
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Nov 05 '22 at 11:09