1

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',),
          }
      ]
    )
cueless
  • 79
  • 2
  • 7
  • I think this is what you are looking for. https://stackoverflow.com/questions/49713189/how-to-use-conditional-statement-within-child-attribute-of-a-flutter-widget-cen – Vraj Shah Mar 18 '22 at 14:37
  • Does this answer your question? [How to use conditional statement within child attribute of a Flutter Widget (Center Widget)](https://stackoverflow.com/questions/49713189/how-to-use-conditional-statement-within-child-attribute-of-a-flutter-widget-cen) – Ruchit Mar 18 '22 at 15:00
  • yes i does! But what i not specified in my question is that i also want to know how to write a loop inside and i understand that should had specified that but i thought the answer would answer that question too, but now i understand i can not use brackets – cueless Mar 18 '22 at 15:21

5 Answers5

2

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');
        ],
    ],
 ),
Manishyadav
  • 1,361
  • 1
  • 8
  • 26
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')
                  ]))
Nabin Dhakal
  • 1,949
  • 3
  • 20
  • 48
0

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',),
          }
      ]
    )
Kaleb
  • 541
  • 2
  • 7
  • 1
    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. – cueless Mar 18 '22 at 14:43
  • yes curly brackets cant be used in widget tree – Kaleb Mar 18 '22 at 14:44
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',),
              }
          ]
        )
PhilNue
  • 27
  • 6
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;

}

saeedmpt
  • 144
  • 1
  • 7