0

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
  • 27
  • 5
  • 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

4 Answers4

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
  • 371
  • 1
  • 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

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,418
  • 2
  • 17
  • 39
0

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
  • 647
  • 1
  • 1
  • 17
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
  • 11
  • 5