简体   繁体   中英

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 略偏中心

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.

在此输入图像描述

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 .

在此输入图像描述

Then did the same with MaterialIcons-Regular.ttf

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM