0

I have a row with 2 children in a row an icon(location) and text

here is sample image enter image description here

              Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Icon(
                      Icons.location_on,
                      color: Colors.white,
                    ),
                    Expanded(
                      child: Text(
                        "Kyla Olsen Ap #651-8679 Sodales Av.Tamuning PA 10855 (492) 709-6392", //  a long address
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 16,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  ],
                ),

This has some space between icon and text. If I align text start then it does not have space but second line also start from start, I want text in center

I want location iocn and text to not have any space like phone number or email address.

Sunny
  • 14,522
  • 15
  • 84
  • 129

2 Answers2

0

Quick fix, change you Expanded Widget wrapping your Text to a Flexible Widget like so:

Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Icon(
                      Icons.location_on,
                      color: Colors.white,
                    ),
                    Flexible(
                      child: Text(
                        "Kyla Olsen Ap #651-8679 Sodales Av.Tamuning PA 10855 (492) 709-6392", //  a long address
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 16,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  ],
                ),
Chichebe
  • 580
  • 5
  • 12
0

It is like this because Expanded widget is expanding a width of widget to be as max as possible be for start a new line for text. You can check it by assign colour into container widget to see the size of each container like this:

enter image description here

As you can see that the container of text(the green one) is expanded and causing a space between an icon and text.

To temporary fix this problem you can just set:

textAlign: TextAlign.center, -> textAlign: TextAlign.left,

Or you can just change it into a column like this

Column(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Icon(
                  Icons.location_on,
                  color: Colors.white,
                ),
                Text(
                  "Kyla Olsen", // split some short text here
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.bold),
                ),
              ],
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.only(left: 10.0), //Based on icon size, I assume that it is 10px
                child: Text(
                  "Ap #651-8679 Sodales Av.Tamuning PA 10855 (492) 709-6392", //  a long address
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.bold),
                ),
              ),
            ),
          ]
        ),

and the out put will be like this

enter image description here

I know that it is a dumb solution but it does work.

Net Natnicha
  • 286
  • 2
  • 5