Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter expand Container to fill remaining space of Row

I have one image in a row and a text next to that image. I want the row to expand fully and image takes as its size, then remain full area should be taken by Container.

Here is my code snippet:

Row(
        children: <Widget>[
          Container(
            margin: EdgeInsets.fromLTRB(10, 50, 10, 8),
            decoration: BoxDecoration(
              color: Colors.white,
              boxShadow: [
                BoxShadow(
                  color: Color.fromARGB(100, 0, 0, 0),
                  blurRadius: 5,
                ),
              ],
            ),
            child: Row(
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Column(children: <Widget>[
                  Image.asset(
                    'assets/card.png',
                    height: 62,
                    width: 62,
                    fit: BoxFit.cover,
                  )
                ]),
                Container(
                  child: Text("Hello world"),
                )
              ],
            ),
          ),
        ],
      ),

I want it like this: Flutter UI

like image 967
Sami Avatar asked Feb 23 '19 08:02

Sami


People also ask

How do you give a full width container 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 a expanded Flutter on a Row?

You can make the children of Row widget, expand to the available horizontal space. All you need to do is, wrap each child of Row widget around Expanded widget.


2 Answers

I recommend you to start building layouts only with Row and Column not to get confusing. I often build layouts as follows.

  1. Layout objects(eg. Text, Image) only with Row and Column. And Set mainAxisAlignment and crossAxisAlignment property in Row and Column.
  2. Set styles with Padding or Align, Expanded etc. You can also use Container.
  3. Decorate in addition.

Reference:

Layout basic:

https://flutter.dev/docs/development/ui/layout

Tips when building layouts:

https://medium.com/@liewjuntung/tips-on-using-android-studio-to-develop-flutter-apps-9e42c047b7f4

I hope you this will help you.

example code:


    Widget buildCard() {
      return Container(
        margin: EdgeInsets.all(10.0),
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: Color.fromARGB(100, 0, 0, 0),
              blurRadius: 5,
            ),
          ],
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Image.asset(
              'assets/card.png',
              height: 62,
              width: 62,
              fit: BoxFit.cover,
            ),
            Padding(
              padding: const EdgeInsets.only(top: 12.0, left: 12.0),
              child: Text(
                "Hello world",
                style: TextStyle(
                  fontWeight: FontWeight.w500,
                  letterSpacing: 0.8,
                ),
              ),
            )
          ],
        ),
      );
    }

like image 62
HeavenOSK Avatar answered Oct 17 '22 07:10

HeavenOSK


Use Expanded instead of the Container around the Text.

like image 2
Martin Růžek Avatar answered Oct 17 '22 08:10

Martin Růžek