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.

194 lines
6.2KB

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