sign_in_screen.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import 'package:app_flowy/startup/startup.dart';
  2. import 'package:app_flowy/user/application/sign_in_bloc.dart';
  3. import 'package:app_flowy/user/presentation/router.dart';
  4. import 'package:app_flowy/user/presentation/widgets/background.dart';
  5. import 'package:easy_localization/easy_localization.dart';
  6. import 'package:flowy_infra/size.dart';
  7. import 'package:flowy_infra/theme.dart';
  8. import 'package:flowy_infra_ui/widget/rounded_button.dart';
  9. import 'package:flowy_infra_ui/widget/rounded_input_field.dart';
  10. import 'package:flowy_infra_ui/widget/spacing.dart';
  11. import 'package:flowy_infra_ui/style_widget/snap_bar.dart';
  12. import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
  13. import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart' show UserProfile;
  14. import 'package:flutter/material.dart';
  15. import 'package:flutter_bloc/flutter_bloc.dart';
  16. import 'package:dartz/dartz.dart';
  17. import 'package:flowy_infra/image.dart';
  18. import 'package:app_flowy/generated/locale_keys.g.dart';
  19. class SignInScreen extends StatelessWidget {
  20. final AuthRouter router;
  21. const SignInScreen({Key? key, required this.router}) : super(key: key);
  22. @override
  23. Widget build(BuildContext context) {
  24. return BlocProvider(
  25. create: (context) => getIt<SignInBloc>(),
  26. child: BlocListener<SignInBloc, SignInState>(
  27. listener: (context, state) {
  28. state.successOrFail.fold(
  29. () => null,
  30. (result) => _handleSuccessOrFail(result, context),
  31. );
  32. },
  33. child: Scaffold(
  34. body: SignInForm(router: router),
  35. ),
  36. ),
  37. );
  38. }
  39. void _handleSuccessOrFail(Either<UserProfile, FlowyError> result, BuildContext context) {
  40. result.fold(
  41. (user) => router.pushWelcomeScreen(context, user),
  42. (error) => showSnapBar(context, error.msg),
  43. );
  44. }
  45. }
  46. class SignInForm extends StatelessWidget {
  47. final AuthRouter router;
  48. const SignInForm({
  49. Key? key,
  50. required this.router,
  51. }) : super(key: key);
  52. @override
  53. Widget build(BuildContext context) {
  54. return Align(
  55. alignment: Alignment.center,
  56. child: AuthFormContainer(
  57. children: [
  58. FlowyLogoTitle(
  59. title: LocaleKeys.signIn_loginTitle.tr(),
  60. logoSize: const Size(60, 60),
  61. ),
  62. const VSpace(30),
  63. const EmailTextField(),
  64. const PasswordTextField(),
  65. ForgetPasswordButton(router: router),
  66. const VSpace(30),
  67. const LoginButton(),
  68. const VSpace(10),
  69. SignUpPrompt(router: router),
  70. if (context.read<SignInBloc>().state.isSubmitting) ...[
  71. const SizedBox(height: 8),
  72. const LinearProgressIndicator(value: null),
  73. ]
  74. ],
  75. ),
  76. );
  77. }
  78. }
  79. class SignUpPrompt extends StatelessWidget {
  80. const SignUpPrompt({
  81. Key? key,
  82. required this.router,
  83. }) : super(key: key);
  84. final AuthRouter router;
  85. @override
  86. Widget build(BuildContext context) {
  87. final theme = context.watch<AppTheme>();
  88. return Row(
  89. children: [
  90. Text(LocaleKeys.signIn_dontHaveAnAccount.tr(), style: TextStyle(color: theme.shader3, fontSize: 12)),
  91. TextButton(
  92. style: TextButton.styleFrom(
  93. textStyle: const TextStyle(fontSize: 12),
  94. ),
  95. onPressed: () => router.pushSignUpScreen(context),
  96. child: Text(
  97. LocaleKeys.signUp_buttonText.tr(),
  98. style: TextStyle(color: theme.main1),
  99. ),
  100. ),
  101. ],
  102. mainAxisAlignment: MainAxisAlignment.center,
  103. );
  104. }
  105. }
  106. class LoginButton extends StatelessWidget {
  107. const LoginButton({
  108. Key? key,
  109. }) : super(key: key);
  110. @override
  111. Widget build(BuildContext context) {
  112. final theme = context.watch<AppTheme>();
  113. return RoundedTextButton(
  114. title: LocaleKeys.signIn_loginButtonText.tr(),
  115. height: 48,
  116. borderRadius: Corners.s10Border,
  117. color: theme.main1,
  118. onPressed: () {
  119. context.read<SignInBloc>().add(const SignInEvent.signedInWithUserEmailAndPassword());
  120. },
  121. );
  122. }
  123. }
  124. class ForgetPasswordButton extends StatelessWidget {
  125. const ForgetPasswordButton({
  126. Key? key,
  127. required this.router,
  128. }) : super(key: key);
  129. final AuthRouter router;
  130. @override
  131. Widget build(BuildContext context) {
  132. final theme = context.watch<AppTheme>();
  133. return TextButton(
  134. style: TextButton.styleFrom(
  135. textStyle: const TextStyle(fontSize: 12),
  136. ),
  137. onPressed: () => router.pushForgetPasswordScreen(context),
  138. child: Text(
  139. LocaleKeys.signIn_forgotPassword.tr(),
  140. style: TextStyle(color: theme.main1),
  141. ),
  142. );
  143. }
  144. }
  145. class PasswordTextField extends StatelessWidget {
  146. const PasswordTextField({
  147. Key? key,
  148. }) : super(key: key);
  149. @override
  150. Widget build(BuildContext context) {
  151. final theme = context.watch<AppTheme>();
  152. return BlocBuilder<SignInBloc, SignInState>(
  153. buildWhen: (previous, current) => previous.passwordError != current.passwordError,
  154. builder: (context, state) {
  155. return RoundedInputField(
  156. obscureText: true,
  157. style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
  158. obscureIcon: svgWidget("home/hide"),
  159. obscureHideIcon: svgWidget("home/show"),
  160. hintText: LocaleKeys.signIn_passwordHint.tr(),
  161. normalBorderColor: theme.shader4,
  162. errorBorderColor: theme.red,
  163. cursorColor: theme.main1,
  164. errorText: context.read<SignInBloc>().state.passwordError.fold(() => "", (error) => error),
  165. onChanged: (value) => context.read<SignInBloc>().add(SignInEvent.passwordChanged(value)),
  166. );
  167. },
  168. );
  169. }
  170. }
  171. class EmailTextField extends StatelessWidget {
  172. const EmailTextField({
  173. Key? key,
  174. }) : super(key: key);
  175. @override
  176. Widget build(BuildContext context) {
  177. final theme = context.watch<AppTheme>();
  178. return BlocBuilder<SignInBloc, SignInState>(
  179. buildWhen: (previous, current) => previous.emailError != current.emailError,
  180. builder: (context, state) {
  181. return RoundedInputField(
  182. hintText: LocaleKeys.signIn_emailHint.tr(),
  183. style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
  184. normalBorderColor: theme.shader4,
  185. errorBorderColor: theme.red,
  186. cursorColor: theme.main1,
  187. errorText: context.read<SignInBloc>().state.emailError.fold(() => "", (error) => error),
  188. onChanged: (value) => context.read<SignInBloc>().add(SignInEvent.emailChanged(value)),
  189. );
  190. },
  191. );
  192. }
  193. }