Deborah
Deborah

Reputation: 4575

Flutter - how to convert an image definition into an icon definition?

In a Flutter app, initially many icons were implemented as png images in their 3 sizes like so:

child: new Image(
  image: new AssetImage(widget.featureNavMenu.image),
),

which expects a string

image: "assets/images/superCheckMark.png",

Now, I want to convert the children to a custom icon font (font glyphs).

However, changing to this...

child: new Icon((icon), size: 25.0,), 

and trying to get it to accept this...

new Icon(MyIcons.superCheckMark, size: 30.0, color: Colors.white,),

breaks the app.

What is the correct way to get the app to accept an icon instead of image? I've actually tried many different things according to Flutter's somewhat general documentation and am stumped.

Upvotes: 19

Views: 23141

Answers (2)

Collin Jackson
Collin Jackson

Reputation: 116838

Instead of Image, you can use the ImageIcon class. This will give you a widget that behaves like an Icon.

ImageIcon(
     AssetImage("images/icon_more.png"),
     color: Color(0xFF3A5A98),
),

Upvotes: 26

Mathieu J.
Mathieu J.

Reputation: 2125

I was looking for this because I thought the leading element of a ListTile needed to be an Icon. So when I encountered issues creating an Icon from an Image, I got led here.

finally I found out ListTile will take an Image just fine

              ListTile(
                leading: Image.network(friend_map['picture_url']),
                title: Text(full_name),
                subtitle: Text(uuid_shorten),
              ),

I recommend you look at the parent object that required an Icon, see if it does not support Image object instead. or has a sibling widget that does..

hope this helps someone.

Upvotes: 2

Related Questions