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

前端 未结 18 2157
再見小時候
再見小時候 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:38

    Actually you can use if/else and switch and any other statement inline in dart / flutter.

    Use an immediate anonymous function

    class StatmentExample extends StatelessWidget {
      Widget build(BuildContext context) {
        return Text((() {
          if(true){
            return "tis true";}
    
          return "anything but true";
        })());
      }
    }
    

    ie wrap your statements in a function

    (() {
      // your code here
    }())
    

    I would heavily recommend against putting too much logic directly with your UI 'markup' but I found that type inference in Dart needs a little bit of work so it can be sometimes useful in scenarios like that.

    Use the ternary operator

    condition ? Text("True") : null,
    

    Use If or For statements or spread operators in collections

    children: [
      ...manyItems,
      oneItem,
      if(canIKickIt)
        ...kickTheCan
      for (item in items)
        Text(item)
    

    Use a method

    child: getWidget()
    
    Widget getWidget() {
      if (x > 5) ...
      //more logic here and return a Widget
    

    Redefine switch statement

    As another alternative to the ternary operator, you could create a function version of the switch statement such as in the following post https://stackoverflow.com/a/57390589/1058292.

      child: case2(myInput,
      {
        1: Text("Its one"),
        2: Text("Its two"),
      }, Text("Default"));
    

提交回复
热议问题