How to use conditional statement within child attribute of a Flutter Widget (Center Widget)

前端 未结 18 2101
再見小時候
再見小時候 2020-11-30 18:50

So far whenever I needed to use a conditional statement within a Widget I have done the following (Using Center and Containers as simplified dummy examples):



        
18条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 19:39

    I personally use if/else statement in children with this kind of block statement. It only supports on Dart version 2.3.0 above.

    if / else

    Column(
        children: [
            if (_selectedIndex == 0) ...[
              DayScreen(),
            ] else ...[
              StatsScreen(),
            ],
        ],
     ),
    

    if / else if

    Column(
        children: [
            if (_selectedIndex == 0) ...[
              DayScreen(),
            ] else if(_selectedIndex == 1)...[
              StatsScreen(),
            ],
        ],
     ),
    

提交回复
热议问题