简体   繁体   中英

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 eg, 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?

    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),
            ],
          )
      ],
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM