Flutter Icon Developer Guide

This guide will help you implementing icons in your flutter app.
You will also learn some basics about color channels, alpha channel(transparency) etc.
In this guide will learn how to use :

  • Flutter Icon Class (also known as Icon Widget) and Icons Class
  • Flutter ImageIcon Class and AssetImage Class
Flutter Icon Developer Guide
Flutter Icon Developer Guide

How to use Flutter Icon Widget or Flutter Icon Class ?

Let us start with the simplest way to implement icons in flutter.

Icon Class - Documentation

Also refer official class documentation with this guide to deeply understand the concept.

Icon Class Constructor

Icon(IconData icon, { Key key, double size, Color color, String semanticLabel, TextDirection textDirection })

Create a child widget of Icon Class.

Now, To get the IconData, Use Material Icons Class - Documentation

In the below screenshot, we can clearly see that IntelliSense auto-completion recommends us a list of icons present in the Icons Class.

To know more about VS Code IntelliSense - Refer VS Code Docs

Flutter - Icons Class
Flutter - Icons Class

“info” icon selected for this demo guide

Info Icon
Info Icon

Output : Flutter UI

Flutter Info Icon UI
Flutter Info Icon UI

We can also change different properties like color,size, semantic label and text direction

Icon class properties

  • icon : IconData [required]
  • color
  • size
  • semantic label
  • textDirection

What is Semantic Label ?

  • The text which will be announced in accessibility modes (example, TalkBack/VoiceOver).
  • This label will not be shown in the UI.
Flutter Semantic Label Icon
Flutter Semantic Label Icon

Output : Flutter UI

Flutter Icon Demo
Flutter Icon Demo

Recommended icon sizes(double value) are 15.0, 20.0, 25.0 and 50.0

This is the simplest way to implement icons in flutter app using Icon Class and Icons Class

How to use ImageIcon Class ?

Using this class (ImageIcon class) we can use our own custom designed images as icons in our flutter app.

ImageIcon Class Widget - Documentation

Also refer official class documentation with this guide to deeply understand the concept.

step 1: let us add our custom images to our flutter project

  • Create assets folder in your project directory
  • We have added two images into assets folder for this demo guide.
  • You may add images required for your project
Image Assets
Image Assets

Also, add the image assets to your application using pubspec.yaml file

Flutter pubspec.yaml
Flutter pubspec.yaml

step 2 : Alpha Channel (Transparency)

Let us first understand basics of image : color channels

Images have 4 color channels

  • Red Channel
  • Green Channel
  • Blue Channel
  • Alpha Channel (Transparency)

Note

To use ImageIcon, we must have transparency — alpha channel in our custom images.

  • Use PNG images with alpha(transparency).
  • Do not use JPEG(.jpg or .jpeg) as it does not support transparency.

We have added two images to our project, in step 1

Image One : Image with white background named bluetruck.png

Image with white background
Image with white background

Image Two : Image with transparent background — bluetruck alpha.png

Image with transparent background
Image with transparent background

step 3 : Let us understand this with an example that why we must have transparency (alpha channel) in our images

To implement the ImageIcon class, lets start with the ImageIcon class constructor

ImageIcon(ImageProvider image, { Key key, double size, Color color, String semanticLabel })

In this case, we do not have any IconData so we cannot use the Icons Class, instead we have ImageProvider.

for ImageProvider, use AssetImage classs [AssetImage Class Documentation] to get your image.

Create a child widget named AssetImage and pass the relative image path

Case One : We use the image with white background (bluetruck.png)

AssetImage White Background
AssetImage White Background

Output UI : for AssetImage with White Background

Flutter AssetImage White Background UI
Flutter AssetImage White Background UI

Note

We get a square box of red color because ImageIcon class traces and paints the icon borders.With a white background image it traces the complete square image path and paints it red as we have set color property as red.

But, this is not the desired result.

Case Two : We use the image with the transparent background (bluetruck alpha.png)

Flutter AssetImage Transparent Background
Flutter AssetImage Transparent Background

Output UI : for AssetImage with Transparent Background

Flutter AssetImage Transparent Background UI
Flutter AssetImage Transparent Background UI

Note

Now, with the transparent background image we get the desired UI output of a truck icon using our custom image.

Image Icon Class properties

We can also change different properties like color,size, semantic label…

  1. Change Icon Color to Green
Flutter ImageIcon Green
Flutter ImageIcon Green

Output UI : Flutter Image Icon Color Green

Flutter ImageIcon Green UI
Flutter ImageIcon Green UI

2. Change icon size

Flutter ImageIcon Size
Flutter ImageIcon Size

Output UI : Flutter Icon Size 50

Flutter ImageIcon Size UI
Flutter ImageIcon Size UI

Using ImageIcon Class we can use our custom image icons.
This is how we can implement icons in flutter app using ImageIcon Class and AssetImage Class.

Coding : Sample Flutter App Code

Bonus Tip : There is one more class for Interactive icons named IconButton.
But, we will learn about IconButton class in the our Flutter Button Developer Guide.

Useful Resources

Learn more about flutter at AkshatApp.com

Originally published at https://blog.akshatapp.com on November 21, 2019.

Helping designers and developers build high quality websites and cross platform mobile apps with our step-by-step developer guides 📚 at akshatapp.com