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.

211 lines
6.9KB

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