AppFlowy/app_flowy/lib/welcome/presentation/welcome_screen.dart

79 lines
2.4 KiB
Dart
Raw Normal View History

2021-07-21 07:43:05 +00:00
import 'package:app_flowy/welcome/domain/i_welcome.dart';
2021-07-12 15:27:58 +00:00
import 'package:app_flowy/welcome/domain/auth_state.dart';
2021-06-19 15:41:19 +00:00
import 'package:app_flowy/startup/startup.dart';
import 'package:app_flowy/welcome/application/welcome_bloc.dart';
2021-07-13 00:47:26 +00:00
import 'package:flowy_infra_ui/widget/route/animation.dart';
2021-07-13 02:59:51 +00:00
import 'package:flowy_infra/flowy_logger.dart';
2021-06-19 15:41:19 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2021-07-13 00:47:26 +00:00
import 'package:flowy_infra/time/prelude.dart';
2021-06-19 15:41:19 +00:00
class WelcomeScreen extends StatelessWidget {
const WelcomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) {
2021-07-12 15:27:58 +00:00
return getIt<WelcomeBloc>()..add(const WelcomeEvent.getUser());
},
2021-06-19 15:41:19 +00:00
child: Scaffold(
body: BlocListener<WelcomeBloc, WelcomeState>(
listener: (context, state) {
state.auth.map(
2021-07-12 15:27:58 +00:00
authenticated: (r) => _handleAuthenticated(context, r),
unauthenticated: (r) => _handleUnauthenticated(context, r),
initial: (r) => {},
2021-06-19 15:41:19 +00:00
);
},
child: const Body(),
),
),
);
}
void _pushToScreen(BuildContext context, Widget screen) {
/// Let the splash view sit for a bit. Mainly for aesthetics and to ensure a smooth intro animation.
2021-07-12 15:27:58 +00:00
Navigator.push(
context,
PageRoutes.fade(
() => screen, RouteDurations.slow.inMilliseconds * .001));
}
void _handleAuthenticated(BuildContext context, Authenticated result) {
2021-09-04 08:53:58 +00:00
getIt<IWelcomeRoute>().pushHomeScreen(context, result.userProfile);
2021-07-12 15:27:58 +00:00
}
void _handleUnauthenticated(BuildContext context, Unauthenticated result) {
Log.error(result.error);
_pushToScreen(context, getIt<IWelcomeRoute>().pushSignInScreen());
}
}
class Body extends StatelessWidget {
const Body({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Container(
alignment: Alignment.center,
child: SingleChildScrollView(
child: Stack(
alignment: Alignment.center,
children: [
Image(
fit: BoxFit.cover,
width: size.width,
height: size.height,
image: const AssetImage(
'assets/images/appflowy_launch_splash.jpg')),
const CircularProgressIndicator.adaptive(),
],
),
),
);
2021-06-19 15:41:19 +00:00
}
}