anisalibegic / separated_column

Flutter package for rendering separated Column children.
MIT License
4 stars 1 forks source link

Exclude null widgets #1

Closed AhmedNourJamalElDin closed 4 years ago

AhmedNourJamalElDin commented 4 years ago

Hi,

Is it possible to filter out all null values?

To reproduce:

Widget build(BuildContext context) {
         return SeparatedColumn(
                   children: [
                          Text("Hello"),
                           _buildWorldText(),
                   ]
          );
}

Widget _buildWorldText() {
           if(true) { return null; }

          return Text("World");
}

Now it throws an exception by flutter where null values are not allowed.

Column's children must not contain any null values, but a null value was found at index 1
anisalibegic commented 4 years ago

I would suggest you using if statement inside the tree instead of using it in build methods. Like this:

Widget build(BuildContext context) {
    return SeparatedColumn(
        children: [
            Text("Hello"),
            if(shouldBuildWorldText)
                _buildWorldText(),
        ],
    );
}

Widget _buildWorldText() {
    return Text("World");
}

This way is much more efficient and _buildWorldText won't be called unless condition is true.

Hint: Don't use methods for building widgets because it will refresh all widgets inside that method even tho they don't need refreshing. You can read more at: https://blog.codemagic.io/how-to-improve-the-performance-of-your-flutter-app./

AhmedNourJamalElDin commented 4 years ago

Thanks for sharing this useful article.

I'm aware of the issues, but I'm literally having the second one where I have my widgets splitted:

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(15.0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            HeaderWidget(),
            MainWidget(),
            FooterWidget(),
          ],
        ),
      ),
    );
  }
}

but I want to hide or show the footer based on backend flag. if the backend returned "showFooter = false" I would like to hide the footer.

Do you suggest or recommend any better way to do so?