body.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import 'package:app_flowy/home/presentation/home_screen.dart';
  2. import 'package:app_flowy/startup/startup.dart';
  3. import 'package:app_flowy/user/application/sign_in/sign_in_bloc.dart';
  4. import 'package:app_flowy/user/presentation/sign_in/widgets/background.dart';
  5. import 'package:dartz/dartz.dart';
  6. import 'package:flowy_infra_ui/widget/rounded_button.dart';
  7. import 'package:flowy_infra_ui/widget/rounded_input_field.dart';
  8. import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter_bloc/flutter_bloc.dart';
  11. class Body extends StatelessWidget {
  12. const Body({Key? key}) : super(key: key);
  13. @override
  14. Widget build(BuildContext context) {
  15. return BlocProvider(
  16. create: (context) => getIt<SignInBloc>(),
  17. child: const SignInBackground(
  18. child: SignInForm(),
  19. ),
  20. );
  21. }
  22. }
  23. class SignInForm extends StatelessWidget {
  24. const SignInForm({
  25. Key? key,
  26. }) : super(key: key);
  27. @override
  28. Widget build(BuildContext context) {
  29. return BlocConsumer<SignInBloc, SignInState>(
  30. listenWhen: (p, c) => p != c,
  31. listener: (context, state) {
  32. state.signInFailure.fold(
  33. () {},
  34. (result) => _handleStateErrors(result, context),
  35. );
  36. },
  37. builder: (context, state) {
  38. return SignInFormBackground(
  39. children: [
  40. const SizedBox(height: 30),
  41. RoundedInputField(
  42. icon: Icons.person,
  43. hintText: 'email',
  44. onChanged: (value) => context
  45. .read<SignInBloc>()
  46. .add(SignInEvent.emailChanged(value)),
  47. ),
  48. RoundedInputField(
  49. icon: Icons.lock,
  50. obscureText: true,
  51. hintText: 'password',
  52. onChanged: (value) => context
  53. .read<SignInBloc>()
  54. .add(SignInEvent.passwordChanged(value)),
  55. ),
  56. RoundedButton(
  57. title: 'LOGIN',
  58. press: () {
  59. context
  60. .read<SignInBloc>()
  61. .add(const SignInEvent.signedInWithUserEmailAndPassword());
  62. },
  63. ),
  64. if (state.isSubmitting) ...[
  65. const SizedBox(height: 8),
  66. const LinearProgressIndicator(value: null),
  67. ]
  68. ],
  69. );
  70. },
  71. );
  72. }
  73. void _handleStateErrors(
  74. Either<UserDetail, UserError> some, BuildContext context) {
  75. some.fold(
  76. (userDetail) => showHomeScreen(context, userDetail),
  77. (result) => _showErrorMessage(context, result.msg),
  78. );
  79. }
  80. void _showErrorMessage(BuildContext context, String msg) {
  81. ScaffoldMessenger.of(context).showSnackBar(
  82. SnackBar(
  83. content: Text(msg),
  84. ),
  85. );
  86. }
  87. void showHomeScreen(BuildContext context, UserDetail userDetail) {
  88. Navigator.pushReplacement(
  89. context,
  90. MaterialPageRoute(
  91. builder: (context) {
  92. return HomeScreen(userDetail);
  93. },
  94. ),
  95. );
  96. }
  97. }
  98. class SignInFormBackground extends StatelessWidget {
  99. final List<Widget> children;
  100. const SignInFormBackground({
  101. Key? key,
  102. required this.children,
  103. }) : super(key: key);
  104. @override
  105. Widget build(BuildContext context) {
  106. final size = MediaQuery.of(context).size;
  107. return Container(
  108. width: size.width * 0.4,
  109. alignment: Alignment.center,
  110. child: SingleChildScrollView(
  111. child: Column(
  112. mainAxisAlignment: MainAxisAlignment.center, children: children),
  113. ),
  114. );
  115. }
  116. }