AppFlowy/app_flowy/lib/user/presentation/sign_in_screen.dart

230 lines
6.3 KiB
Dart
Raw Normal View History

2021-07-13 13:14:49 +08:00
import 'package:app_flowy/startup/startup.dart';
2021-09-05 22:52:20 +08:00
import 'package:app_flowy/user/application/sign_in_bloc.dart';
import 'package:app_flowy/user/domain/i_auth.dart';
2021-09-06 16:18:34 +08:00
import 'package:app_flowy/user/presentation/widgets/background.dart';
import 'package:flowy_infra/theme.dart';
2021-07-25 16:07:53 +08:00
import 'package:flowy_infra_ui/widget/rounded_button.dart';
import 'package:flowy_infra_ui/widget/rounded_input_field.dart';
import 'package:flowy_infra_ui/widget/spacing.dart';
import 'package:flowy_sdk/protobuf/flowy-user/errors.pb.dart';
2021-09-04 16:53:58 +08:00
import 'package:flowy_sdk/protobuf/flowy-user/user_profile.pb.dart';
2021-07-13 13:14:49 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2021-07-25 16:07:53 +08:00
import 'package:dartz/dartz.dart';
import 'package:flowy_infra/image.dart';
2021-07-13 13:14:49 +08:00
class SignInScreen extends StatelessWidget {
final IAuthRouter router;
const SignInScreen({Key? key, required this.router}) : super(key: key);
2021-07-13 13:14:49 +08:00
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => getIt<SignInBloc>(),
child: BlocListener<SignInBloc, SignInState>(
listener: (context, state) {
state.successOrFail.fold(
() => null,
(result) => _handleSuccessOrFail(result, context),
);
},
child: Scaffold(
body: SignInForm(router: router),
2021-07-25 16:07:53 +08:00
),
2021-07-13 13:14:49 +08:00
),
);
}
2021-07-25 16:07:53 +08:00
void _handleSuccessOrFail(
2021-09-04 16:53:58 +08:00
Either<UserProfile, UserError> result, BuildContext context) {
result.fold(
2021-09-06 16:18:34 +08:00
(user) => router.pushWelcomeScreen(context, user),
(error) => _showErrorMessage(context, error.msg),
2021-07-25 16:07:53 +08:00
);
}
void _showErrorMessage(BuildContext context, String msg) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(msg),
),
);
}
}
class SignInForm extends StatelessWidget {
final IAuthRouter router;
2021-07-25 16:07:53 +08:00
const SignInForm({
Key? key,
required this.router,
2021-07-25 16:07:53 +08:00
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.center,
2021-09-05 22:52:20 +08:00
child: AuthFormContainer(
2021-07-25 16:07:53 +08:00
children: [
2021-09-05 22:52:20 +08:00
const AuthFormTitle(
2021-07-25 16:07:53 +08:00
title: 'Login to Appflowy',
logoSize: Size(60, 60),
),
const VSpace(30),
const EmailTextField(),
const PasswordTextField(),
ForgetPasswordButton(router: router),
const VSpace(30),
const LoginButton(),
2021-07-25 16:07:53 +08:00
const VSpace(10),
SignUpPrompt(router: router),
2021-07-25 16:07:53 +08:00
if (context.read<SignInBloc>().state.isSubmitting) ...[
const SizedBox(height: 8),
const LinearProgressIndicator(value: null),
]
],
),
);
}
}
class SignUpPrompt extends StatelessWidget {
const SignUpPrompt({
Key? key,
required this.router,
}) : super(key: key);
final IAuthRouter router;
@override
Widget build(BuildContext context) {
final theme = context.watch<AppTheme>();
return Row(
children: [
Text("Dont't have an account",
style: TextStyle(color: theme.shader3, fontSize: 12)),
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 12),
),
2021-09-06 16:18:34 +08:00
onPressed: () => router.pushSignUpScreen(context),
child: Text(
'Sign Up',
style: TextStyle(color: theme.main1),
),
),
],
mainAxisAlignment: MainAxisAlignment.center,
);
}
}
class LoginButton extends StatelessWidget {
const LoginButton({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final theme = context.watch<AppTheme>();
2021-07-25 22:09:52 +08:00
return RoundedTextButton(
title: 'Login',
2021-09-05 22:52:20 +08:00
height: 48,
borderRadius: BorderRadius.circular(10),
color: theme.main1,
press: () {
context
.read<SignInBloc>()
.add(const SignInEvent.signedInWithUserEmailAndPassword());
},
);
}
}
class ForgetPasswordButton extends StatelessWidget {
const ForgetPasswordButton({
Key? key,
required this.router,
}) : super(key: key);
final IAuthRouter router;
@override
Widget build(BuildContext context) {
final theme = context.watch<AppTheme>();
return TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 12),
),
2021-09-06 16:18:34 +08:00
onPressed: () => router.pushForgetPasswordScreen(context),
child: Text(
'Forgot Password?',
style: TextStyle(color: theme.main1),
),
);
}
}
2021-07-25 16:07:53 +08:00
class PasswordTextField extends StatelessWidget {
const PasswordTextField({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final theme = context.watch<AppTheme>();
return BlocBuilder<SignInBloc, SignInState>(
buildWhen: (previous, current) =>
previous.passwordError != current.passwordError,
builder: (context, state) {
return RoundedInputField(
obscureText: true,
2021-09-06 16:18:34 +08:00
fontSize: 14,
obscureIcon: svgWidgetWithName("home/Hide.svg"),
obscureHideIcon: svgWidgetWithName("home/Show.svg"),
2021-09-05 22:52:20 +08:00
hintText: 'Password',
normalBorderColor: theme.shader4,
highlightBorderColor: theme.red,
errorText: context
.read<SignInBloc>()
.state
.passwordError
.fold(() => "", (error) => error),
onChanged: (value) => context
.read<SignInBloc>()
.add(SignInEvent.passwordChanged(value)),
);
},
);
}
}
class EmailTextField extends StatelessWidget {
const EmailTextField({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final theme = context.watch<AppTheme>();
return BlocBuilder<SignInBloc, SignInState>(
buildWhen: (previous, current) =>
previous.emailError != current.emailError,
builder: (context, state) {
return RoundedInputField(
2021-09-05 22:52:20 +08:00
hintText: 'Email',
2021-09-06 16:18:34 +08:00
fontSize: 14,
normalBorderColor: theme.shader4,
highlightBorderColor: theme.red,
errorText: context
.read<SignInBloc>()
.state
.emailError
.fold(() => "", (error) => error),
onChanged: (value) =>
context.read<SignInBloc>().add(SignInEvent.emailChanged(value)),
);
},
);
2021-07-25 16:07:53 +08:00
}
2021-07-13 13:14:49 +08:00
}