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.

191 lines
6.6KB

  1. import 'package:farm_tpf/authentication/authentication.dart';
  2. import 'package:farm_tpf/data/repository/authentication_repository.dart';
  3. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
  5. import 'package:farm_tpf/presentation/screens/forgot_password/sc_forgot_password.dart';
  6. import 'package:farm_tpf/presentation/screens/login/bloc/login_bloc.dart';
  7. import 'package:farm_tpf/utils/const_color.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:formz/formz.dart';
  10. import 'package:flutter_bloc/flutter_bloc.dart';
  11. class LoginForm extends StatelessWidget {
  12. AuthenticationBloc _authenticationBloc;
  13. FocusNode _usernameFocus = FocusNode();
  14. FocusNode _passwordFocus = FocusNode();
  15. @override
  16. Widget build(BuildContext context) {
  17. _authenticationBloc = BlocProvider.of<AuthenticationBloc>(context);
  18. return BlocListener<LoginBloc, LoginState>(
  19. listener: (context, state) {
  20. if (state.status.isSubmissionFailure) {
  21. LoadingDialog.hideLoadingDialog(context);
  22. Utils.showSnackBarError(
  23. message: 'Tài khoản hoặc mật khẩu không đúng.');
  24. }
  25. if (state.status.isSubmissionSuccess) {
  26. LoadingDialog.hideLoadingDialog(context);
  27. _authenticationBloc.add(
  28. AuthenticationStatusChanged(AuthenticationStatus.authenticated));
  29. }
  30. if (state.status.isSubmissionInProgress) {
  31. LoadingDialog.showLoadingDialog(context);
  32. }
  33. },
  34. child: Align(
  35. child: Column(
  36. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  37. mainAxisSize: MainAxisSize.min,
  38. children: [
  39. _UsernameInput(
  40. usernameFocus: _usernameFocus, nextFocus: _passwordFocus),
  41. const Padding(padding: EdgeInsets.all(12)),
  42. _PasswordInput(
  43. passwordFocus: _passwordFocus,
  44. ),
  45. const Padding(padding: EdgeInsets.all(16)),
  46. _LoginButton(),
  47. const Padding(padding: EdgeInsets.all(6)),
  48. _FogotPasswordButton(),
  49. ],
  50. ),
  51. ),
  52. );
  53. }
  54. }
  55. class _FogotPasswordButton extends StatelessWidget {
  56. @override
  57. Widget build(BuildContext context) {
  58. return Align(
  59. alignment: Alignment.center,
  60. child: FlatButton(
  61. child: Text('Quên mật khẩu',
  62. style: TextStyle(color: AppColors.BLUE, fontSize: 16)),
  63. onPressed: () {
  64. Navigator.of(context).push(
  65. MaterialPageRoute(builder: (_) => ForgotPasswordScreen()));
  66. }));
  67. }
  68. }
  69. class _UsernameInput extends StatelessWidget {
  70. final FocusNode usernameFocus;
  71. final FocusNode nextFocus;
  72. _UsernameInput({@required this.usernameFocus, @required this.nextFocus});
  73. @override
  74. Widget build(BuildContext context) {
  75. return BlocBuilder<LoginBloc, LoginState>(
  76. buildWhen: (previous, current) => previous.username != current.username,
  77. builder: (context, state) {
  78. return Container(
  79. height: 50,
  80. padding: EdgeInsets.symmetric(horizontal: 8),
  81. child: Center(
  82. child: TextFormField(
  83. focusNode: usernameFocus,
  84. textInputAction: TextInputAction.next,
  85. onChanged: (username) => context
  86. .bloc<LoginBloc>()
  87. .add(LoginUsernameChanged(username)),
  88. autovalidate: true,
  89. validator: (_) {
  90. return state.username.invalid
  91. ? 'Vui lòng nhập tài khoản'
  92. : null;
  93. },
  94. onFieldSubmitted: (_) {
  95. usernameFocus.unfocus();
  96. FocusScope.of(context).requestFocus(nextFocus);
  97. },
  98. maxLines: 1,
  99. keyboardType: TextInputType.text,
  100. obscureText: false,
  101. textAlign: TextAlign.left,
  102. decoration: InputDecoration(
  103. hintText: 'Tài khoản',
  104. ),
  105. ),
  106. ));
  107. },
  108. );
  109. }
  110. }
  111. class _PasswordInput extends StatelessWidget {
  112. final FocusNode passwordFocus;
  113. _PasswordInput({this.passwordFocus});
  114. @override
  115. Widget build(BuildContext context) {
  116. return BlocBuilder<LoginBloc, LoginState>(
  117. buildWhen: (previous, current) => previous.password != current.password,
  118. builder: (context, state) {
  119. return Container(
  120. height: 50,
  121. padding: EdgeInsets.symmetric(horizontal: 8),
  122. child: Center(
  123. child: TextFormField(
  124. focusNode: passwordFocus,
  125. onChanged: (password) => context
  126. .bloc<LoginBloc>()
  127. .add(LoginPasswordChanged(password)),
  128. autovalidate: true,
  129. validator: (_) {
  130. return state.password.invalid
  131. ? 'Vui lòng nhập mật khẩu'
  132. : null;
  133. },
  134. onFieldSubmitted: (_) {
  135. passwordFocus.unfocus();
  136. if (state.status.isValidated) {
  137. context.bloc<LoginBloc>().add(const LoginSubmitted());
  138. }
  139. },
  140. maxLines: 1,
  141. keyboardType: TextInputType.text,
  142. obscureText: true,
  143. textAlign: TextAlign.left,
  144. decoration: InputDecoration(
  145. hintText: 'Mật khẩu',
  146. ),
  147. ),
  148. ));
  149. },
  150. );
  151. }
  152. }
  153. class _LoginButton extends StatelessWidget {
  154. @override
  155. Widget build(BuildContext context) {
  156. return BlocBuilder<LoginBloc, LoginState>(
  157. buildWhen: (previous, current) => previous.status != current.status,
  158. builder: (context, state) {
  159. return SizedBox(
  160. width: double.infinity,
  161. height: 55,
  162. child: FlatButton(
  163. onPressed: () {
  164. if (state.status.isValidated) {
  165. context.bloc<LoginBloc>().add(const LoginSubmitted());
  166. }
  167. },
  168. color: state.status.isValidated
  169. ? AppColors.DEFAULT
  170. : AppColors.GRAY1_50,
  171. shape: RoundedRectangleBorder(
  172. borderRadius: new BorderRadius.circular(7.0),
  173. ),
  174. child: Text('Đăng nhập'.toUpperCase(),
  175. style: TextStyle(
  176. fontWeight: FontWeight.bold,
  177. color: AppColors.WHITE,
  178. fontSize: 18)),
  179. ),
  180. );
  181. },
  182. );
  183. }
  184. }