'How to show Icon in Text widget in flutter?

I want to show an icon in text widget. How do I do this ?

The following code only shows the IconData

Text("Click ${Icons.add} to add");


Solution 1:[1]

Flutter has WidgetSpan() to add a widget inside the RichText().

Example use:

RichText(
  text: TextSpan(
    children: [
      TextSpan(
        text: "Click ",
      ),
      WidgetSpan(
        child: Icon(Icons.add, size: 14),
      ),
      TextSpan(
        text: " to add",
      ),
    ],
  ),
)

Above code will produce:

image

You can treat the child of WidgetSpan like the usual widget.

Solution 2:[2]

An alternative might be to use emoji.

In Dart, strings supports escape sequences for special characters. For unicode emoji, you can use \u and curly braces with emoji hex code inside. Like this:

Text('Click \u{2795} to add')

The result is:

enter image description here

You can find a complete unicode emoji list here: http://unicode.org/Public/emoji/1.0/emoji-data.txt

Solution 3:[3]

Screenshot:

enter image description here


  • Using Wrap:

    Wrap(
      crossAxisAlignment: WrapCrossAlignment.center,
      children: [
        Text('Click'),
        Icon(Icons.add),
        Text('to add'),
      ],
    )
    
  • Using Row:

    Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text('Click'),
        Icon(Icons.add),
        Text('to add'),
      ],
    )
    
  • Using Text.rich:

    Text.rich(
      TextSpan(
        children: [
          TextSpan(text: 'Click'),
          WidgetSpan(child: Icon(Icons.add)),
          TextSpan(text: 'to add'),
        ],
      ),
    )
    

Solution 4:[4]

Row Widget can be one solution for this issue, but you have to use different widgets to make it happen.

Follow below example.

Row(
   children: <Widget>[
     Text("Hi"),
     Icon(Icons.add),
     Text("Hello")
   ]
 )

Solution 5:[5]

Another option is String.fromCharCode(int charCode)

This is icons.dart created by flutter team and charCodes are can found here.

enter image description here

RichText(
  text: TextSpan(
  style: TextStyle(
    color: Colors.black,
    fontSize: 24.0,
  ),
  text: 'This is alarm icon : ',
    children: <TextSpan>[
      TextSpan(
        text: String.fromCharCode(0xe190), //<-- charCode
        style: TextStyle(
        fontFamily: 'MaterialIcons', //<-- fontFamily
        fontSize: 24.0,
        color: Colors.red,
       ),
     )
   ],
  ),
)

And result :

enter image description here

Solution 6:[6]

I think it's better to use the Row widget. here is an example

Row(
  children: [
     Icon(Icons.download_rounded),
     Text(" Download")
  ],
)

Solution 7:[7]

You can use WidgetSpan() on Text.rich(). Here's a link for InlineSpan-class.

Solution 8:[8]

You can use this Package to achieve this.

I am not able to find the pub of this package.

Here is the Implementation.

    RealRichText(
      [
        TextSpan(
          text: "A Text Link",
          style: TextStyle(color: Colors.red, fontSize: 14),
          recognizer: TapGestureRecognizer()
            ..onTap = () {
              debugPrint("Link Clicked.");
            },
        ),
        ImageSpan(
          AssetImage("packages/real_rich_text/images/emoji_9.png"),
          imageWidth: 24,
          imageHeight: 24,
        ),
        ImageSpan(AssetImage("packages/real_rich_text/images/emoji_10.png"),
            imageWidth: 24,
            imageHeight: 24,
            margin: EdgeInsets.symmetric(horizontal: 10)),
        TextSpan(
          text: "???",
          style: TextStyle(color: Colors.yellow, fontSize: 14),
        ),
        TextSpan(
          text: "@Somebody",
          style: TextStyle(
              color: Colors.black, fontSize: 14, fontWeight: FontWeight.bold),
          recognizer: TapGestureRecognizer()
            ..onTap = () {
              debugPrint("Link Clicked");
            },
        ),
        TextSpan(
          text: " #RealRichText# ",
          style: TextStyle(color: Colors.blue, fontSize: 14),
          recognizer: TapGestureRecognizer()
            ..onTap = () {
              debugPrint("Link Clicked");
            },
        ),
        TextSpan(
          text: "showing a bigger image",
          style: TextStyle(color: Colors.black, fontSize: 14),
        ),
        ImageSpan(AssetImage("packages/real_rich_text/images/emoji_10.png"),
            imageWidth: 24,
            imageHeight: 24,
            margin: EdgeInsets.symmetric(horizontal: 5)),
        TextSpan(
          text: "and seems working perfect……",
          style: TextStyle(color: Colors.black, fontSize: 14),
        ),
      ],
    );

You can also check out below issue for more:

Flutter Issue #2022

Solution 9:[9]

For simple icons, You can use a Unicode character as regular text, just copy and paste.

You can search and copy Unicodes from this site

Text("Enjoy! ???????")

This method will work with any Text widget and package.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dennbagas
Solution 2
Solution 3
Solution 4 Saed Nabil
Solution 5 blokberg
Solution 6 Sammrafi
Solution 7 Agust
Solution 8 ibhavikmakwana
Solution 9