0

我正在使用 flutter_bloc 库来管理应用程序的状态,因为我在 bloc 类中按下了不同类状态更改中的按钮,但无法在构建方法中侦听 BlocProvider 。

BlocBuilder<AppBarBloc, bool>(
          builder: (context, isEnabled) {
            print('state detected');
            return Container();
          },)

//in main.dart were providing the Blocs


MultiBlocProvider(
      providers: [
        BlocProvider<PagesBloc>(
          builder: (BuildContext context) => PagesBloc(),
        ),
        BlocProvider<AppBarBloc>(
          builder: (BuildContext context) => AppBarBloc(),
        ),
      ],
      child: Column(
        children: <Widget>[
          Expanded(
            child: MaterialApp(
              title: 'Flutter Demo',
              initialRoute: '/',
              onGenerateRoute: RouteGenerator.generateRoute,
              theme: ThemeData(
                primarySwatch: Colors.blue,
              ),
            ),
          ),
        ],
      ),
    );
4

1 回答 1

1

也许您可以查看有关bloc listener的链接。

但就像你可以做的一个例子:

BlocBuilder<CustomerRegisterBloc, CustomerRegisterState>
      buildCustomerRegisterOnState() {
    return BlocBuilder<CustomerRegisterBloc, CustomerRegisterState>(
      builder: (context, state) {
        if (state is InitialCustomerRegisterState) {
          return PersonalDataForm();
        }
        if (state is PasswordCustomerRegisterState) {}
        if (state is PaymentFormCustomerRegisterState) {}
        if (state is TermsCustomerRegisterState) {}
        return Container();
      },
    );
  }
于 2019-12-03T19:20:23.363 回答