动画切换器和 Bloc Builder

Animated Switcher and Bloc Builder

我是 flutter 中的 bloc 模式的新手。

我的一个状态 类 有一个小部件列表和一个索引作为字段。我的目标是使用此状态的小部件更新 Animated Switcher 的子级。

return AnimatedSwitcher(
  duration: Duration(milliseconds: 500),
  child: BlocBuilder<WelcomeBloc, WelcomeBlocState>(
    builder: (context, state) {

      if(state is MyState)
        return state.widgetList[state.index];

      else return Container();

    },
  ),
);

我也试过相反的方法,在 bloc builder 中返回动画切换器,结果是一样的

调用yield时,widget发生了变化,但没有任何动画。

我错过了什么?

AnimatedSwitcher 的一个子部件必须更改:

return BlocBuilder<WelcomeBloc, WelcomeBlocState>(
  builder: (context, state) {
    return AnimatedSwitcher(
      duration: Duration(milliseconds: 500),
      child: state is MyState ? state.widgetList[state.index] : Container(key: Key('key2')),
    );
  },
);

并且不要忘记为子窗口小部件设置不同的键。