Search code examples
flutterbloc

What is the preferred way to use BlocBuilder - if I want to rebuild two widgets in a column separated by other widgets?


I am new to the Flutter Bloc concept and I was wondering what would be more efficient to app performance if I have two or three widgets in a column that should be rebuilt on state change, and between them there are 3 or 4 more unchanging widgets.

there are three options: 1- create two/three bloc builders (around the changing widgets) in order to not rebuild unchanging widgets for no reason.

2- create one bloc builder around the column even though it will also rebuild static unchanging widgets. but will save the spare bloc builders.

3- wrap the inner changing items in the column in a smaller column which in turn we will wrap with one bloc builder (disadvantage- widget tree may become less elegant with many nested columns)

I tried to find discussion about it and could not find a preferred approach

thanks in advance


Solution

  • It really depends:

    • If your Column is heavy to rebuild because there are some nasty calculation in your build method, or those widget that doesn't change are quite big and complex, than you should rebuild the changing widgets separately;
    • If your Column is light weight with simple children, than you can prefer keeping your code easy to read and rebuild all at once.

    I would consider the second option even if your changing widgets need to be rebuild always together because they follow the same logic. Performance should be followed only when it generates problems in UX, otherwise you should always aim to clean and easy to read code.

    Here you can find some performance guideline for flutter: link

    And in this video some useful tip from flutter developers: link