How to vertically center an Icon relative to a Text widget in Flutter? - Stack Overflow

admin2025-05-02  0

I'm trying to display a header in Flutter where the Icon is aligned to the center of the Text widget's height. Here's the widget structure I have so far:

//dart

Widget header() {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        "Welcome back",
        style: TextStyle(
          fontSize: 20,
        ),
      ),
      Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            "User name",
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              fontSize: 22,
              fontWeight: FontWeight.bold,
            ),
          ),
          Icon(
            kVerifiedIcon,
            color: kPrimaryColor,
            size: 15,
          ),
        ],
      ),
    ],
  );
}

Result:

Currently, the Icon appears slightly misaligned relative to the Text. I want the Icon to be vertically centered according to the height of the Text.

I've tried adjusting the size of the Icon and changing the crossAxisAlignment property in the Row, but it didn't work as expected.

I'm trying to display a header in Flutter where the Icon is aligned to the center of the Text widget's height. Here's the widget structure I have so far:

//dart

Widget header() {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        "Welcome back",
        style: TextStyle(
          fontSize: 20,
        ),
      ),
      Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            "User name",
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              fontSize: 22,
              fontWeight: FontWeight.bold,
            ),
          ),
          Icon(
            kVerifiedIcon,
            color: kPrimaryColor,
            size: 15,
          ),
        ],
      ),
    ],
  );
}

Result:

Currently, the Icon appears slightly misaligned relative to the Text. I want the Icon to be vertically centered according to the height of the Text.

I've tried adjusting the size of the Icon and changing the crossAxisAlignment property in the Row, but it didn't work as expected.

Share Improve this question asked Jan 2 at 2:54 Toxic PrinceToxic Prince 254 bronze badges 1
  • The above code is perfectly fine i think the other widget is causing the issue please provide the entire hierarchy – Akshay Gupta Commented Jan 2 at 6:58
Add a comment  | 

2 Answers 2

Reset to default 1

Add your Icon widget inside Container or Padding and provide top padding, I have try it using Container padding. You can also try with transform property as well instead of padding

Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "Welcome back",
          style: TextStyle(
            fontSize: 20,
          ),
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              "User name",
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 22,
                fontWeight: FontWeight.bold,
              ),
            ),
            Container(
              alignment: Alignment.center,
              padding: EdgeInsets.only(top: 1.5), // adjust padding on your need
              //transform: Matrix4.translationValues(0, 1.5, 0),
              child: Icon(
                Icons.verified_rounded,
                size: 15,
              ),
            ),
          ],
        ),
      ],
    ),

Result Screen -

In your provided code, the icon is centered within the Row, but it requires a slight padding from the top to align the icon vertically with the text. Try below code...

    Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            'Welcome back',
            style: TextStyle(
              fontSize: 20,
            ),
          ),
          Row(
            children: [
              Text(
                'User name',
                style: TextStyle(
                  fontSize: 22,
                ),
              ),
              SizedBox(width: 5),
              Container(
                padding: EdgeInsets.only(top: 2),
                child: Icon(
                  Icons.verified_rounded,
                  size: 15,
                ),
              ),
            ],
          ),
        ],
      ),
转载请注明原文地址:http://anycun.com/QandA/1746135811a92067.html