Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Center Align Text Widget and then add a leading icon to the text?

In a Row widget, I added a text Widget and I am able to center align the text by using mainAxisAlignment.center but when I add an icon before the text, the text obviously is now not centre-aligned because both Icon and Text get centre-aligned together.

Find below the screenshot for better understanding.

My designer suggested to have the "Morning" text centre-aligned to the screen and the icon should be prefixed to the text, how can I achieve that?

enter image description here

like image 202
Udit Chugh Avatar asked Oct 26 '25 04:10

Udit Chugh


1 Answers

You can use a transparent color trick:

      Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Icon(Icons.wb_sunny, color: Colors.yellow),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8.0),
            child: Text('Morning'),
          ),
          Icon(Icons.wb_sunny, color: Colors.transparent),
        ],
      )

The right icon is needed only for balance, and has a transparent color.

like image 160
Spatz Avatar answered Oct 27 '25 18:10

Spatz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!