splash_screen.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import 'package:app_flowy/welcome/domain/i_splash.dart';
  2. import 'package:app_flowy/welcome/domain/auth_state.dart';
  3. import 'package:app_flowy/startup/startup.dart';
  4. import 'package:app_flowy/welcome/application/splash_bloc.dart';
  5. import 'package:flowy_infra/flowy_logger.dart';
  6. import 'package:flowy_sdk/dispatch/dispatch.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_bloc/flutter_bloc.dart';
  9. import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart' as workspace;
  10. class SplashScreen extends StatelessWidget {
  11. const SplashScreen({Key? key}) : super(key: key);
  12. @override
  13. Widget build(BuildContext context) {
  14. return BlocProvider(
  15. create: (context) {
  16. return getIt<SplashBloc>()..add(const SplashEvent.getUser());
  17. },
  18. child: Scaffold(
  19. body: BlocListener<SplashBloc, SplashState>(
  20. listener: (context, state) {
  21. state.auth.map(
  22. authenticated: (r) => _handleAuthenticated(context, r),
  23. unauthenticated: (r) => _handleUnauthenticated(context, r),
  24. initial: (r) => {},
  25. );
  26. },
  27. child: const Body(),
  28. ),
  29. ),
  30. );
  31. }
  32. void _handleAuthenticated(BuildContext context, Authenticated result) {
  33. final userProfile = result.userProfile;
  34. WorkspaceEventReadCurWorkspace().send().then(
  35. (result) {
  36. return result.fold(
  37. (workspace) => getIt<ISplashRoute>()
  38. .pushHomeScreen(context, userProfile, workspace.id),
  39. (error) async {
  40. assert(error.code == workspace.ErrorCode.CurrentWorkspaceNotFound);
  41. getIt<ISplashRoute>().pushWelcomeScreen(context, userProfile);
  42. },
  43. );
  44. },
  45. );
  46. }
  47. void _handleUnauthenticated(BuildContext context, Unauthenticated result) {
  48. Log.error(result.error);
  49. getIt<ISplashRoute>().pushSignInScreen(context);
  50. }
  51. }
  52. class Body extends StatelessWidget {
  53. const Body({Key? key}) : super(key: key);
  54. @override
  55. Widget build(BuildContext context) {
  56. var size = MediaQuery.of(context).size;
  57. return Container(
  58. alignment: Alignment.center,
  59. child: SingleChildScrollView(
  60. child: Stack(
  61. alignment: Alignment.center,
  62. children: [
  63. Image(
  64. fit: BoxFit.cover,
  65. width: size.width,
  66. height: size.height,
  67. image: const AssetImage(
  68. 'assets/images/appflowy_launch_splash.jpg')),
  69. const CircularProgressIndicator.adaptive(),
  70. ],
  71. ),
  72. ),
  73. );
  74. }
  75. }