如果BlocProvider中有3个BLoC,是否可以在同一类中使用2个BLoCBuilder?

时间:2019-06-26 14:31:55

标签: flutter dart bloc

假设我在BlocProviderTree中有3个BLoC,这意味着这些类可用于该类。其中一个用作BlocListener,现在我希望将其他两个BLoC用作BlocBuilders,因为我需要在同一页面上构建两个状态。 BlocListener接受孩子而不是孩子。请帮忙!

BlocListener接受子对象,而不接受子对象。请帮忙!

BlocProviderTree(
            blocProviders: [

              BlocProvider<EvenBloc>(bloc: _evenBloc),
              BlocProvider<SumBloc>(bloc: _sumsBloc,),
              BlocProvider<SpacesBloc>(bloc: _spacesBloc,),

            ],
            child: BlocListener(
                bloc: _evenBloc,
                listener: (BuildContext context, EvenState state) {
                  if (state is NowEven) {
                    _sumsBloc.dispatch(LoadSums());
                  }
                },

                child: BlocBuilder<SumsEvent,SumsState>(
                  bloc: _programsBloc,
                  builder: (BuildContext context, SumsState state) {
                    if (state is SumsEmpty)
                      return Container(height: 0.0, width: 0.0,);
                    if (state is SumsLoading)
                      return Evens(
                        child: SpinKitDoubleBounce(
                          color: darkIndigo,
                          size: 70.0,
                        ),
                      );
                    else if (state is SumsLoaded)
                      return _body( context,state.sums);
                  },
                )
            )
        ),

如果项目数是偶数,我想要带和的SpaceNames。我正确地获得了总和,但是空格在其他集团中。如何定义两个BlocBuilder?

1 个答案:

答案 0 :(得分:0)

您可以将BlocBuilder彼此嵌套:

BlocBuilder(
  bloc: BlocProvider.of<Foo>(context),
  builder: (context, FooState fooState) {
    return BlocBuilder(
      bloc: BlocProvider.of<Bar>(context),
      builder: (context, BarState barState) {
        return Text('$fooState $barState');
      },
    );
  },
);