我正在尝试使用StreamBuilder生成屏幕内容。它沿集团模式运行得很好,但是我正在努力解决一件事。
如何为生成的内容(WidgetA和WidgetB)之间的变化(例如淡入淡出或滑动效果)制作动画?
...
return AnimatedSwitcher(
duration: Duration(seconds: 4),
child: BlocBuilder<ContentEvent, int>(
bloc: bloc,
builder: (context, contentID) {
if (contentID == 1) {
return WidgetA();
} else {
return WidgetB();
}
},
),
);
...
最佳答案
您的BlocBuilder
应该包装AnimatedSwitcher
,而不是相反。AnimatedSwitcher
的动画在其直接子代更改时发生。但在您的情况下,直接子代始终为BlocBuilder
。
StreamBuilder(
stream: stream,
builder: (context, snapshot) {
return AnimatedSwitcher(
duration: const Duration(seconds: 4),
child: snapshot.hasData
? Text(snapshot.data)
: CircularProgressIndicator();
);
}
),