You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.1KB

  1. import 'package:farm_tpf/presentation/screens/login/bloc/login_bloc.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:formz/formz.dart';
  4. import 'package:flutter_bloc/flutter_bloc.dart';
  5. class LoginForm extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. return BlocListener<LoginBloc, LoginState>(
  9. listener: (context, state) {
  10. if (state.status.isSubmissionFailure) {
  11. Scaffold.of(context)
  12. ..hideCurrentSnackBar()
  13. ..showSnackBar(
  14. const SnackBar(content: Text('Authentication Failure')),
  15. );
  16. }
  17. },
  18. child: Align(
  19. alignment: const Alignment(0, -1 / 3),
  20. child: Column(
  21. mainAxisSize: MainAxisSize.min,
  22. children: [
  23. _UsernameInput(),
  24. const Padding(padding: EdgeInsets.all(12)),
  25. _PasswordInput(),
  26. const Padding(padding: EdgeInsets.all(12)),
  27. _LoginButton(),
  28. ],
  29. ),
  30. ),
  31. );
  32. }
  33. }
  34. class _UsernameInput extends StatelessWidget {
  35. @override
  36. Widget build(BuildContext context) {
  37. return BlocBuilder<LoginBloc, LoginState>(
  38. buildWhen: (previous, current) => previous.username != current.username,
  39. builder: (context, state) {
  40. return TextField(
  41. key: const Key('loginForm_usernameInput_textField'),
  42. onChanged: (username) =>
  43. context.bloc<LoginBloc>().add(LoginUsernameChanged(username)),
  44. decoration: InputDecoration(
  45. labelText: 'Tên đăng nhập',
  46. errorText:
  47. state.username.invalid ? 'Vui lòng nhập tên đăng nhập' : null,
  48. ),
  49. );
  50. },
  51. );
  52. }
  53. }
  54. class _PasswordInput extends StatelessWidget {
  55. @override
  56. Widget build(BuildContext context) {
  57. return BlocBuilder<LoginBloc, LoginState>(
  58. buildWhen: (previous, current) => previous.password != current.password,
  59. builder: (context, state) {
  60. return TextField(
  61. key: const Key('loginForm_passwordInput_textField'),
  62. onChanged: (password) =>
  63. context.bloc<LoginBloc>().add(LoginPasswordChanged(password)),
  64. obscureText: true,
  65. decoration: InputDecoration(
  66. labelText: 'Mật khẩu',
  67. errorText: state.password.invalid ? 'Vui lòng nhập mật khẩu' : null,
  68. ),
  69. );
  70. },
  71. );
  72. }
  73. }
  74. class _LoginButton extends StatelessWidget {
  75. @override
  76. Widget build(BuildContext context) {
  77. return BlocBuilder<LoginBloc, LoginState>(
  78. buildWhen: (previous, current) => previous.status != current.status,
  79. builder: (context, state) {
  80. return state.status.isSubmissionInProgress
  81. ? const CircularProgressIndicator()
  82. : RaisedButton(
  83. key: const Key('loginForm_continue_raisedButton'),
  84. child: const Text('Đăng nhập'),
  85. onPressed: state.status.isValidated
  86. ? () {
  87. context.bloc<LoginBloc>().add(const LoginSubmitted());
  88. }
  89. : null,
  90. );
  91. },
  92. );
  93. }
  94. }