2

How do I create a loop or if-statement inside a widget in flutter? It seems that you only can make a single line condition like: if(condition) but if you try to use brackets: if(condition){ } it gives an error. The same thing happens whith loops.

I want to be able to:

 RichText( text: TextSpan( text: 'Logg\n', children: <TextSpan>[ if(condition){ TextSpan( text: 'Text1\n',), TextSpan( text: 'Text2\n',), }else{ TextSpan( text: 'Text3\n',), TextSpan( text: 'Text4\n',), } ] ) 
3

5 Answers 5

6

This one of the way that I found ,It only supports on Dart version 2.3.0 above.

For if/else

Column( children: [ if (_selectedIndex == 0) ...[ Text("Its one"), ] else ...[ Text("Its two"), ], ], ), 

for if / else if

Column( children: [ if (_selectedIndex == 0) ...[ Text('Its One'); ] else if(_selectedIndex == 1)...[ Text('Its One'); ], ], ), 
Sign up to request clarification or add additional context in comments.

1 Comment

I don't prefer this , but this is just a possibility if someone might like use it.
1

Try as follows:

RichText( text: TextSpan( text: 'Logg\n', children: value == true ? [ const TextSpan(text: 'Text1\n'), const TextSpan(text: 'Text2\n') ] : [ const TextSpan(text: 'Text3\n'), const TextSpan(text: 'Text4\n') ])) 

Comments

1

You cans use like this

if(condition) RichText( text: TextSpan( text: 'Logg\n', children: <TextSpan>[ TextSpan( text: 'Text1\n',), TextSpan( text: 'Text2\n',), } ] ) else RichText( text: TextSpan( text: 'Logg\n', children: <TextSpan>[ TextSpan( text: 'Text1\n',), TextSpan( text: 'Text2\n',), } ] ) 

2 Comments

ok thanks. So its not possible to add an if statment with brackets inside a widget? To for example use a loop inside one of the textspan that prints "Text1\n" and "Text2\n 10 times each.
yes curly brackets cant be used in widget tree
0

instead of ? you can use like == or smth the ? is currently true

condition ? RichText( text: TextSpan( text: 'Logg\n', children: <TextSpan>[ TextSpan( text: 'Text1\n',), TextSpan( text: 'Text2\n',), } ] ) : RichText( text: TextSpan( text: 'Logg\n', children: <TextSpan>[ TextSpan( text: 'Text1\n',), TextSpan( text: 'Text2\n',), } ] ) 

Comments

0
 RichText( textAlign: TextAlign.center, textDirection: TextDirection.rtl, text: TextSpan(children: getTextSpan(entity,themeData))), List<InlineSpan> getTextSpan(FlightEntity entity, ThemeData themeData) { List<InlineSpan> list = []; if (entity.aircraft != null && entity.aircraft!.isNotEmpty) { list.add(TextSpan(text: "نوع هواپیما: ", style: themeData.textTheme.bodyText2!.apply(fontSizeFactor: 0.7))); list.add(TextSpan( text: (entity.aircraft.toString()) + " ", style: themeData.textTheme.bodyText2, )); } if (entity.flightNumber != null && entity.flightNumber!.isNotEmpty) { TextSpan(text: "شماره هواپیما: ", style: themeData.textTheme.bodyText2!.apply(fontSizeFactor: 0.7)); TextSpan( text: (entity.flightNumber.toString()) + " ", style: themeData.textTheme.bodyText2, ); } return list; 

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.