iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐙

[Flutter] Using conditional branching for widget children

に公開
2023/07/15
Column(
    children: [
        if (_selectedIndex == 0) ...[
          DayScreen(),
        ] else if(_selectedIndex == 1)...[
          StatsScreen(),
        ],
    ],
 ),
Column(
    children: [
        if (_selectedIndex == 0) ...[
          DayScreen(),
          AboutScreen(),
          InfoScreen(),
        ] else if(_selectedIndex == 1)...[
          HomeScreen(),
          StatsScreen(),
        ],
    ],
 ),

If you want to use a ternary operator like this, here is how!

Column(
    children: [
        ...(_selectedIndex == 0) ? [
          DayScreen(),
          AboutScreen(),
          InfoScreen(),
        ] : [
          HomeScreen(),
          StatsScreen(),
        ],
    ],
 ),

In scenarios where there is no Fragment (as in React), it might be a good idea to wrap them in a List.

References

https://stackoverflow.com/a/60584979

Discussion