body.dart 3.5 KB

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