Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a cupertino icon in flutter?

First of all, I have tried using Center(), With that out the way, it seems cupertino icons are off center

             IconButton(
              icon: const Icon(
                CupertinoIcons.add_circled,
                color: Colors.black,
              ),
              padding: const EdgeInsets.all(0),
              onPressed: () {}
              ),

wrapping the icon directly in a Center() widget doesnt change anything either I this is how it looks slightly off center

like image 978
martinseal1987 Avatar asked Nov 06 '22 19:11

martinseal1987


1 Answers

CupertinoIcons are actually centered in Flutter.

If you take a look at the source icons map for Cupertino here you'll see that the icons are aligned to the bottom inside the square that Flutter loads (and even slightly aligned to the left). Therefore, you can't adjust it's alignment in your code as the resource is already centered.

enter image description here

UPDATE

I went a step further and found the CupertinoIcons.ttf font on the flutter sdk source. Then opened it with https://www.glyphrstudio.com.

enter image description here

Then did the same with MaterialIcons-Regular.ttf

enter image description here

You can clearly see how the icon is aligned against the baseline on Cupertino (as characters do on any font), and perfectly centered on Material (as icons should be).

There's more information about font metrics on this Medium article

You can find CupertinoIcons.ttf here: https://github.com/flutter/cupertino_icons/tree/master/assets

like image 163
GaboBrandX Avatar answered Nov 23 '22 05:11

GaboBrandX