sign_in_screen.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import 'package:app_flowy/startup/startup.dart';
  2. import 'package:app_flowy/user/application/sign_in/sign_in_bloc.dart';
  3. import 'package:app_flowy/user/domain/i_auth.dart';
  4. import 'package:app_flowy/user/presentation/sign_in/widgets/background.dart';
  5. import 'package:flowy_infra_ui/widget/rounded_button.dart';
  6. import 'package:flowy_infra_ui/widget/rounded_input_field.dart';
  7. import 'package:flowy_infra_ui/widget/spacing.dart';
  8. import 'package:flowy_sdk/protobuf/flowy-user/errors.pb.dart';
  9. import 'package:flowy_sdk/protobuf/flowy-user/user_detail.pb.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:flutter_bloc/flutter_bloc.dart';
  12. import 'package:dartz/dartz.dart';
  13. class SignInScreen extends StatelessWidget {
  14. final IAuthRouter router;
  15. const SignInScreen({Key? key, required this.router}) : super(key: key);
  16. @override
  17. Widget build(BuildContext context) {
  18. return BlocProvider(
  19. create: (context) => getIt<SignInBloc>(),
  20. child: BlocListener<SignInBloc, SignInState>(
  21. listener: (context, state) {
  22. state.successOrFail.fold(
  23. () => null,
  24. (result) => _handleSuccessOrFail(result, context),
  25. );
  26. },
  27. child: Scaffold(
  28. body: SignInForm(router: router),
  29. ),
  30. ),
  31. );
  32. }
  33. void _handleSuccessOrFail(
  34. Either<UserDetail, UserError> result, BuildContext context) {
  35. result.fold(
  36. (user) => router.showHomeScreen(context, user),
  37. (error) => _showErrorMessage(context, error.msg),
  38. );
  39. }
  40. void _showErrorMessage(BuildContext context, String msg) {
  41. ScaffoldMessenger.of(context).showSnackBar(
  42. SnackBar(
  43. content: Text(msg),
  44. ),
  45. );
  46. }
  47. }
  48. class SignInForm extends StatelessWidget {
  49. final IAuthRouter router;
  50. const SignInForm({
  51. Key? key,
  52. required this.router,
  53. }) : super(key: key);
  54. @override
  55. Widget build(BuildContext context) {
  56. return Align(
  57. alignment: Alignment.center,
  58. child: SignInFormContainer(
  59. children: [
  60. const SignInTitle(
  61. title: 'Login to Appflowy',
  62. logoSize: Size(60, 60),
  63. ),
  64. const VSpace(30),
  65. const EmailTextField(),
  66. const PasswordTextField(),
  67. ForgetPasswordButton(router: router),
  68. const LoginButton(),
  69. const VSpace(10),
  70. SignUpPrompt(router: router),
  71. if (context.read<SignInBloc>().state.isSubmitting) ...[
  72. const SizedBox(height: 8),
  73. const LinearProgressIndicator(value: null),
  74. ]
  75. ],
  76. ),
  77. );
  78. }
  79. }
  80. class SignUpPrompt extends StatelessWidget {
  81. const SignUpPrompt({
  82. Key? key,
  83. required this.router,
  84. }) : super(key: key);
  85. final IAuthRouter router;
  86. @override
  87. Widget build(BuildContext context) {
  88. return Row(
  89. children: [
  90. const Text("Dont't have an account",
  91. style: TextStyle(color: Colors.blueGrey, fontSize: 12)),
  92. TextButton(
  93. style: TextButton.styleFrom(
  94. textStyle: const TextStyle(fontSize: 12),
  95. ),
  96. onPressed: () => router.showSignUpScreen(context),
  97. child: const Text(
  98. 'Sign Up',
  99. style: TextStyle(color: Colors.lightBlue),
  100. ),
  101. ),
  102. ],
  103. mainAxisAlignment: MainAxisAlignment.center,
  104. );
  105. }
  106. }
  107. class LoginButton extends StatelessWidget {
  108. const LoginButton({
  109. Key? key,
  110. }) : super(key: key);
  111. @override
  112. Widget build(BuildContext context) {
  113. return RoundedButton(
  114. title: 'Login',
  115. height: 65,
  116. borderRadius: BorderRadius.circular(10),
  117. color: Colors.lightBlue,
  118. press: () {
  119. context
  120. .read<SignInBloc>()
  121. .add(const SignInEvent.signedInWithUserEmailAndPassword());
  122. },
  123. );
  124. }
  125. }
  126. class ForgetPasswordButton extends StatelessWidget {
  127. const ForgetPasswordButton({
  128. Key? key,
  129. required this.router,
  130. }) : super(key: key);
  131. final IAuthRouter router;
  132. @override
  133. Widget build(BuildContext context) {
  134. return TextButton(
  135. style: TextButton.styleFrom(
  136. textStyle: const TextStyle(fontSize: 12),
  137. ),
  138. onPressed: () => router.showForgetPasswordScreen(context),
  139. child: const Text(
  140. 'Forgot Password?',
  141. style: TextStyle(color: Colors.lightBlue),
  142. ),
  143. );
  144. }
  145. }
  146. class PasswordTextField extends StatelessWidget {
  147. const PasswordTextField({
  148. Key? key,
  149. }) : super(key: key);
  150. @override
  151. Widget build(BuildContext context) {
  152. return BlocBuilder<SignInBloc, SignInState>(
  153. buildWhen: (previous, current) =>
  154. previous.passwordError != current.passwordError,
  155. builder: (context, state) {
  156. return RoundedInputField(
  157. obscureText: true,
  158. hintText: 'password',
  159. normalBorderColor: Colors.green,
  160. highlightBorderColor: Colors.red,
  161. errorText: context
  162. .read<SignInBloc>()
  163. .state
  164. .passwordError
  165. .fold(() => "", (error) => error),
  166. onChanged: (value) => context
  167. .read<SignInBloc>()
  168. .add(SignInEvent.passwordChanged(value)),
  169. );
  170. },
  171. );
  172. }
  173. }
  174. class EmailTextField extends StatelessWidget {
  175. const EmailTextField({
  176. Key? key,
  177. }) : super(key: key);
  178. @override
  179. Widget build(BuildContext context) {
  180. return BlocBuilder<SignInBloc, SignInState>(
  181. buildWhen: (previous, current) =>
  182. previous.emailError != current.emailError,
  183. builder: (context, state) {
  184. return RoundedInputField(
  185. hintText: 'email',
  186. normalBorderColor: Colors.green,
  187. highlightBorderColor: Colors.red,
  188. errorText: context
  189. .read<SignInBloc>()
  190. .state
  191. .emailError
  192. .fold(() => "", (error) => error),
  193. onChanged: (value) =>
  194. context.read<SignInBloc>().add(SignInEvent.emailChanged(value)),
  195. );
  196. },
  197. );
  198. }
  199. }