Bloc: Using animation when BlocBuilder state changes

Created on 24 Sep 2020  路  4Comments  路  Source: felangel/bloc

Is your feature request related to a problem? Please describe.
Not really. I just can't figure out a clean way to solve my problem.

Describe the solution you'd like
As a states changes inside BlocBuildder, I wish to be able to change the state using animations such as "fade animation". Normally without using flutter_bloc, AnimatedSwitcher is used for this purpose but I wasn't able to find a clean way to achieve it.

Describe alternatives you've considered
I tried calling setState() each time the state is changed but that's not a clean way to do it.

Additional context
Let me know if I can provide further context.

question

Most helpful comment

thanks @RollyPeres and @felangel

All 4 comments

Hi @samramez 馃憢
Thanks for opening an issue!

I would highly recommend using BlocListener to manage animations (as long as there's business logic involved). If there's no business logic involved then I don't recommend creating a bloc just for animation state (you can use a StatefulWidget for that). If you do have business logic and it makes sense to use a bloc, then you can interact with an AnimationController in the BlocListener's listener callback to manage the animation.

Let me know if that helps! If you still have questions, it'd be great if you could put together a simple sample app which illustrates the problem you're facing 馃憤

Thanks @felangel for always being responsive.

good to know.
Do you happen to have access to an example for this which you can share here?

For example in the simple example below:

              BlocConsumer<OwnerCubit, OwnerState>(builder: (context, state) {

                    if (state is Success) {
                      return OwnerWidgets();
                    } 

                    else if (state is Loading) {
                      return CircularProgressIndicator();
                    } 

                   else {
                      // initial state. We will return empty view.
                      return Container();
                    }
                  },
                   listener: (context, state) {      

                   }
                 )

Are you suggesting that I wrap my OwnerWidget() with AnimatedSwitcher inside the listener ?

listener: (context, state) {      
       if (state is Success) {
            return AnimatedSwitcher(
                   child: OwnerWidget(),
            )            
        }
 }           

Hi @samramez 馃憢

In case of an implicitly animated widget like AnimatedSwitcher you can make use of the builder instead.

BlocBuilder<OwnerCubit, OwnerState>(
      builder: (context, state) {
        return AnimatedSwitcher(
          child: state is Success
              ? OwnerWidget()
              : const CircularProgressIndicator(),
        );
      },
    )

thanks @RollyPeres and @felangel

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sawankumarbundelkhandi picture sawankumarbundelkhandi  路  108Comments

hahai96 picture hahai96  路  40Comments

bernaferrari picture bernaferrari  路  43Comments

zs-dima picture zs-dima  路  34Comments

mariaszek9003 picture mariaszek9003  路  29Comments