Ashtav
Ashtav

Reputation: 2966

How to put icon on string in flutter?

I have array string and I want to put icons inside that string like this,

var arr = ['lorem ipsum dolor select icon '+Icons(Icons.search)+' blablabla...'];

But I got error

The argument type 'Icons' can't be assigned to the parameter type 'String'

How I can solve this problem? thanks

Upvotes: 5

Views: 8146

Answers (3)

Khôi Nguyễn
Khôi Nguyễn

Reputation: 51

Just solved the same problem by using RichText and TextSpan

new RichText(
    text: new TextSpan(children: [
        TextSpan(text: 'Some random string', style: TextStyle(color: Colors.black)),
        WidgetSpan(
            child: new Icon(Icons.home, color: Colors.black45),
        ),
        TextSpan(text: 'Some random string.', style: TextStyle(color: Colors.black)),
    ]),
)

Hope this help.

Upvotes: 5

ikben
ikben

Reputation: 4625

Try declaring a list with the type of dynamic

List<dynamic> myList = <dynamic>[Text('Hello'),Icon(Icons.add),Text('World')];

if you wish to use it inside your widget tree. you can to it by using the row or column widget:

`Column(children: myList)` OR `Row(children: myList)`

Upvotes: 0

diegoveloper
diegoveloper

Reputation: 103351

You can't use Widgets inside your String variable, what you can do is use the Row widget, like this:

Row(
  children: [
    Text('lorem ipsum dolor select icon '),
    Icon(Icons.search),
    Text(' blablabla...')
  ],
);

More info here: https://api.flutter.dev/flutter/widgets/Row-class.html

Upvotes: 6

Related Questions