根据身份验证状态交换MaterialApp是否有副作用?

时间:2020-02-29 10:03:34

标签: flutter bloc flutter-bloc

在所有教程和示例中,在runApp()中仅启动一个单个MaterialApp。进行下面的操作是否有任何副作用?在下面的代码中,AppWrapper返回包装了MaterialApp的四个StatelessWidget中的一个。

void main() async {
  ...
  runApp(
    BlocProvider(
      create: (context) => AuthenticationBloc(
        authenticationRepository: authenticationRepository,
      )..add(AppStarted()),
      child: AppWrapper(),
    ),
  );
}

class AppWrapper extends StatelessWidget {
  const AppWrapper({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthenticationBloc, AuthenticationState>(
      builder: (context, state) {
        if (state is AuthenticationInitial) {
          return SplashScreen();
        }
        if (state is AuthenticationNotAuthenticated) {
          return NotAuthenticatedApp();
        }
        if (state is AuthenticationIsAuthenticated) {
          return IsAuthenticatedApp();
        }
        return FourOFourScreen();
      },
    );
  }
}

0 个答案:

没有答案