Search code examples
flutterdartrichtext

Add image to RichText element


I am trying to achieve following:

enter image description here

To achieve this I am using RichText. Using this I can add multiple text with different styles. But I am not getting idea how I can add image to this TextSpan. May be there is some other element.


Solution

  • You do not need third-party libraries to achieve this. With the latest stable release, Flutter has introduced WidgetSpan. This is similar to TextSpan but you can use any widget instead of just text. Sample code:

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