Himanshu Sharma
Himanshu Sharma

Reputation: 1094

Flutter for loop to generate a Row of widgets

I have a string which contains text with multiple \n, I want to display that text in the new line but with a leading icon.

For e.g, If I have a String str="Hello\nThere\nUser". I want to display the text like this:

Icon  Hello
Icon  There
Icon  User

How can I achieve this?

Upvotes: 1

Views: 2219

Answers (1)

Viktor K.
Viktor K.

Reputation: 393

    String str = "Hello\nThere\nUser";
    List<String> list = str.split('\n');
    list.forEach((s) {
      widgetList.add(Row(
        children: <Widget>[
          Icon(Icons.directions_car),
          Text(s),
        ],
      ));
    });

    return Column(children: widgetList);

or

    String str = "Hello\nThere\nUser";
    return Column(
      children: [
        for (var s in str.split('\n'))
          Row(
            children: <Widget>[
              Icon(Icons.directions_run),
              Text(s),
            ],
          )
      ],
    );

Upvotes: 4

Related Questions