Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How create Container with width match parent?

Tags:

flutter

dart

I use this simple widget in my Flutter app:

  FlatButton(
   child: Column(
          children: <Widget>[
            Image.asset(assets, height: 40, width: 40),
            Text('Title'),
            //my color line
            Container(
              height: 5,
              width: ?,
              color: Colors.blue[800],
            )
          ],
        )
  )

I need color line (in buttom), with width match parent. but I don’t know how to do it.

like image 783
FetFrumos Avatar asked Aug 08 '19 18:08

FetFrumos


People also ask

How do you make a container full width in Flutter?

Full-width container using MediaQuery You can also use MediaQuery to assign 100% width to a Flutter Container widget. We can calculate the full width of the device using MediaQuery. A simple Flutter example to make a Container occupy the full width.

How do you make an elevated Button match parent on Flutter?

You need to use an Expanded widget. But, if your button is on a column, the Expanded widget fills the rest of the column. So, you need to enclose the Expanded Widget within a row. My preferred way to make RaiseButton with match parent is that wrap it with Container.


3 Answers

Container(
    height: 5,
    width: double.infinity,
    color: Colors.blue[800],
)
like image 187
Lewis Weng Avatar answered Oct 08 '22 15:10

Lewis Weng


Use IntrinsicWidth

 FlatButton(
   child: IntrinsicWidth(
   child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Image.asset(assets, height: 40, width: 40),
            Text('Title'),
            //my color line
            Container(
              height: 5,
              width: ?,
              color: Colors.blue[800],
            )
          ],
        ))
  )

Some cheatsheet

like image 36
Doc Avatar answered Oct 08 '22 15:10

Doc


Two ways of doing this

ConstrainedBox:

It Creates a widget that imposes additional constraints on its child, which internally uses SingleChildRenderObjectWidget to add constraints over the child widget.

ConstrainedBox(
        constraints:
            const BoxConstraints(minWidth: double.infinity, maxHeight: 10),
        child: Container(
          color: Colors.blue,
        ),
      ),

SizedBox:

SizedBox simply creates a box with a given width/height and doesn't allow the child to go beyond given dimensions. It also used SingleChildRenderObjectWidget to decide the child render area

SizedBox(
        width: double.infinity,
        height: 5,
        // height: double.infinity,
        child: Container(
          color: Colors.blue,
        ),
      ),
like image 13
Jitesh Mohite Avatar answered Oct 08 '22 16:10

Jitesh Mohite