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();
}
},
),
);
...
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();
);
}
),
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With