Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate widget generated by StreamBuilder?

I'm trying to generate content of my screen by using StreamBuilder. It's working pretty well along bloc pattern, but there is one thing that I'm struggling with. How to animate changes between generated content (WidgetA and WidgetB) like fade or slide effect?

    ...
    return AnimatedSwitcher(
      duration: Duration(seconds: 4),
      child: BlocBuilder<ContentEvent, int>(
        bloc: bloc,
        builder: (context, contentID) {
          if (contentID == 1) {
            return WidgetA();
          } else {
            return WidgetB();
          }
        },
      ),
    );
    ...
like image 353
EvilMonkeyPL Avatar asked Mar 15 '19 07:03

EvilMonkeyPL


2 Answers

Your BlocBuilder should wrap the AnimatedSwitcher and not the opposite.

The animation of AnimatedSwitcher happens when its direct child change. But in your case, the direct child is always BlocBuilder.

StreamBuilder(
  stream: stream,
  builder: (context, snapshot) {
    return AnimatedSwitcher(
      duration: const Duration(seconds: 4),
      child: snapshot.hasData
        ? Text(snapshot.data)
        : CircularProgressIndicator();
    );
  }
),
like image 99
Rémi Rousselet Avatar answered Sep 20 '22 23:09

Rémi Rousselet


You could try with AnimatedCrossFade It accepts 2 children, duration and state (AnimatedCrossFadeState.showFirst and AnimatedCrossFadeState.showSecond) and it will animate fade between two children.

documentation: https://docs.flutter.io/flutter/widgets/AnimatedCrossFade-class.html

like image 25
knezzz Avatar answered Sep 22 '22 23:09

knezzz