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.

322 lines
12KB

  1. import 'package:farm_tpf/presentation/custom_widgets/button/ghost_button_widget.dart';
  2. import 'package:farm_tpf/presentation/screens/forgot_password/sc_forgot_password.dart';
  3. import 'package:farm_tpf/themes/app_dimension.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. import 'package:flutter_svg/svg.dart';
  8. import 'package:flutter_switch/flutter_switch.dart';
  9. import 'package:get/get.dart';
  10. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  11. import '../../../authentication/bloc/authentication_bloc.dart';
  12. import '../../../themes/app_colors.dart';
  13. import '../../../themes/styles_text.dart';
  14. import '../../../utils/app_images.dart';
  15. import '../../../utils/const_enum.dart';
  16. import '../../../utils/helpers.dart';
  17. import '../../../utils/local_storage.dart';
  18. import '../../../utils/utils.dart';
  19. import '../../../utils/validators.dart';
  20. import '../../custom_widgets/button/primary_button_widget.dart';
  21. import '../../custom_widgets/textfield/text_field_normal.dart';
  22. import 'cubit/login_cubit.dart';
  23. class LoginPage extends StatefulWidget {
  24. const LoginPage({Key? key}) : super(key: key);
  25. @override
  26. State<LoginPage> createState() => _LoginPageState();
  27. }
  28. class _LoginPageState extends State<LoginPage> {
  29. var loginBloc = LoginCubit();
  30. var loginErrorMessage = ValueNotifier('');
  31. @override
  32. void initState() {
  33. super.initState();
  34. // if (kDebugMode) {
  35. // loginBloc.usernameCtl.text = 'quanly1';
  36. // loginBloc.passwordCtl.text = 'Abcd@1234';
  37. // }
  38. prepareData();
  39. }
  40. @override
  41. void dispose() {
  42. super.dispose();
  43. loginBloc.dispose();
  44. }
  45. void prepareData() {
  46. var username = LocalStorage.getString(LocalStorageKey.username);
  47. loginBloc.usernameCtl.text = username;
  48. }
  49. @override
  50. Widget build(BuildContext context) {
  51. return BlocListener<LoginCubit, LoginState>(
  52. bloc: loginBloc,
  53. listener: (context, state) {
  54. if (state is LoginLoading) {
  55. Helpers.hideKeyboard(context);
  56. UtilWidget.showLoading();
  57. } else if (state is LoginFailure) {
  58. UtilWidget.hideLoading();
  59. loginErrorMessage.value = state.errorMessage;
  60. loginErrorMessage.notifyListeners();
  61. } else if (state is LoginSuccess) {
  62. UtilWidget.hideLoading();
  63. BlocProvider.of<AuthenticationBloc>(context).add(
  64. const AuthenticationStatusChanged(
  65. AuthenticationStatus.authenticated,
  66. ),
  67. );
  68. } else {
  69. UtilWidget.hideLoading();
  70. }
  71. },
  72. child: _widgetMainBody(),
  73. );
  74. }
  75. Widget _widgetMainBody() {
  76. return Scaffold(
  77. body: KeyboardDismisser(
  78. child: Container(
  79. decoration: BoxDecoration(
  80. //color: AppColors.primary1.withOpacity(0.2),
  81. color: AppColors.primary1,
  82. gradient: LinearGradient(
  83. colors: [
  84. AppColors.primary1,
  85. AppColors.primary1.withOpacity(0),
  86. ],
  87. begin: Alignment.bottomCenter,
  88. end: Alignment.topCenter,
  89. ),
  90. ),
  91. child: Stack(
  92. children: <Widget>[
  93. // Positioned(
  94. // top: 0,
  95. // right: 0,
  96. // left: 0,
  97. // child: Container(
  98. // height: 370.h,
  99. // decoration: BoxDecoration(
  100. // color: AppColors.background1,
  101. // image: const DecorationImage(
  102. // image: AssetImage(AssetPNG.backgroundLogin),
  103. // fit: BoxFit.cover,
  104. // ),
  105. // ),
  106. // ),
  107. // ),
  108. // Positioned(
  109. // top: 0,
  110. // right: 0,
  111. // left: 0,
  112. // child: Container(
  113. // height: 371.h,
  114. // decoration: BoxDecoration(
  115. // color: AppColors.primary1,
  116. // gradient: LinearGradient(
  117. // colors: [
  118. // AppColors.primary1,
  119. // AppColors.primary1.withOpacity(0),
  120. // ],
  121. // begin: Alignment.bottomCenter,
  122. // end: Alignment.topCenter,
  123. // ),
  124. // ),
  125. // ),
  126. // ),
  127. Positioned(
  128. top: 100.h,
  129. right: 40.w,
  130. left: 40.w,
  131. child: Image.asset(
  132. AssetPNG.logo,
  133. height: 150,
  134. ),
  135. ),
  136. Positioned(
  137. bottom: 8,
  138. right: 8,
  139. left: 8,
  140. child: SafeArea(
  141. child: Container(
  142. width: double.infinity,
  143. padding: EdgeInsets.all(32.r),
  144. decoration: BoxDecoration(
  145. color: Colors.white,
  146. borderRadius: BorderRadius.circular(16.r),
  147. ),
  148. child: Form(
  149. key: loginBloc.formKey,
  150. child: Column(
  151. crossAxisAlignment: CrossAxisAlignment.start,
  152. mainAxisSize: MainAxisSize.min,
  153. children: [
  154. Text(
  155. 'Đăng Nhập',
  156. style: StylesText.header1,
  157. ),
  158. const SizedBox(
  159. height: 9,
  160. ),
  161. Text(
  162. 'Nhập tài khoản và mật khẩu để đăng nhập',
  163. style: StylesText.body6.copyWith(
  164. color: AppColors.neutral2,
  165. ),
  166. ),
  167. SizedBox(
  168. height: 24.h,
  169. ),
  170. Text(
  171. 'Tài Khoản',
  172. style: StylesText.body6,
  173. ),
  174. const SizedBox(
  175. height: 4,
  176. ),
  177. TextFieldNormal(
  178. controller: loginBloc.usernameCtl,
  179. hint: 'Nhập tài khoản',
  180. validator: (val) {
  181. return Validators.validateNotNullOrEmpty(
  182. val,
  183. 'Nhập tài khoản',
  184. );
  185. },
  186. ),
  187. const SizedBox(
  188. height: 16,
  189. ),
  190. Text(
  191. 'Mật Khẩu',
  192. style: StylesText.body6,
  193. ),
  194. const SizedBox(
  195. height: 4,
  196. ),
  197. TextFieldNormal(
  198. controller: loginBloc.passwordCtl,
  199. hint: 'Nhập Mật Khẩu',
  200. isPasswordField: true,
  201. validator: (val) {
  202. return Validators.validateNotNullOrEmpty(
  203. val,
  204. 'Nhập Mật Khẩu',
  205. );
  206. },
  207. ),
  208. ValueListenableBuilder<String>(
  209. valueListenable: loginErrorMessage,
  210. builder: (context, errorMessage, _) {
  211. if (Validators.stringNotNullOrEmpty(errorMessage)) {
  212. return Container(
  213. decoration: BoxDecoration(
  214. color: AppColors.semantic7,
  215. borderRadius: BorderRadius.circular(8),
  216. ),
  217. margin: const EdgeInsets.only(
  218. top: 16,
  219. ),
  220. padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
  221. child: Row(
  222. children: [
  223. SvgPicture.asset(AssetSVG.icWarning),
  224. const SizedBox(
  225. width: 4,
  226. ),
  227. Text(
  228. errorMessage,
  229. style: StylesText.caption3.copyWith(
  230. color: AppColors.semantic6,
  231. ),
  232. ),
  233. ],
  234. ),
  235. );
  236. } else {
  237. return const SizedBox.shrink();
  238. }
  239. },
  240. ),
  241. SizedBox(
  242. height: 24.h,
  243. ),
  244. Row(
  245. children: [
  246. // ValueListenableBuilder<bool>(
  247. // valueListenable: loginBloc.isRemember,
  248. // builder: (context, isRemember, _) {
  249. // return FlutterSwitch(
  250. // width: 36.w,
  251. // height: 19.h,
  252. // toggleSize: 14.r,
  253. // value: isRemember,
  254. // borderRadius: 18.r,
  255. // padding: 3.r,
  256. // showOnOff: false,
  257. // activeColor: AppColors.primary1,
  258. // onToggle: (val) {
  259. // onPressedRememberLogin(val);
  260. // },
  261. // );
  262. // },
  263. // ),
  264. // const SizedBox(
  265. // width: 10,
  266. // ),
  267. // Expanded(
  268. // child: Text(
  269. // 'Ghi nhớ đăng nhập',
  270. // style: StylesText.caption3,
  271. // ),
  272. // ),
  273. Expanded(child: const SizedBox.shrink()),
  274. GhostButtonWidget(
  275. title: 'Quên Mật Khẩu',
  276. onPressed: () {
  277. Get.to(
  278. () => ForgotPasswordScreen(),
  279. );
  280. },
  281. ),
  282. ],
  283. ),
  284. SizedBox(
  285. height: 24.h,
  286. ),
  287. PrimaryButtonWidget(
  288. title: 'Đăng Nhập',
  289. onPressed: () => onPressedLogin(),
  290. ),
  291. ],
  292. ),
  293. ),
  294. ),
  295. ),
  296. ),
  297. ],
  298. ),
  299. ),
  300. ),
  301. );
  302. }
  303. void onPressedRememberLogin(bool isRemember) {
  304. loginBloc.rememberMe(isRemember);
  305. }
  306. void onPressedForgotPassword() {}
  307. void onPressedLogin() {
  308. loginBloc.loginWithCredential();
  309. }
  310. }