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.

324 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 = '1234567890';
  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.logoWithSlogan,
  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(
  212. errorMessage)) {
  213. return Container(
  214. decoration: BoxDecoration(
  215. color: AppColors.semantic7,
  216. borderRadius: BorderRadius.circular(8),
  217. ),
  218. margin: const EdgeInsets.only(
  219. top: 16,
  220. ),
  221. padding: const EdgeInsets.symmetric(
  222. vertical: 4, horizontal: 8),
  223. child: Row(
  224. children: [
  225. SvgPicture.asset(AssetSVG.icWarning),
  226. const SizedBox(
  227. width: 4,
  228. ),
  229. Text(
  230. errorMessage,
  231. style: StylesText.caption3.copyWith(
  232. color: AppColors.semantic6,
  233. ),
  234. ),
  235. ],
  236. ),
  237. );
  238. } else {
  239. return const SizedBox.shrink();
  240. }
  241. },
  242. ),
  243. SizedBox(
  244. height: 24.h,
  245. ),
  246. Row(
  247. children: [
  248. // ValueListenableBuilder<bool>(
  249. // valueListenable: loginBloc.isRemember,
  250. // builder: (context, isRemember, _) {
  251. // return FlutterSwitch(
  252. // width: 36.w,
  253. // height: 19.h,
  254. // toggleSize: 14.r,
  255. // value: isRemember,
  256. // borderRadius: 18.r,
  257. // padding: 3.r,
  258. // showOnOff: false,
  259. // activeColor: AppColors.primary1,
  260. // onToggle: (val) {
  261. // onPressedRememberLogin(val);
  262. // },
  263. // );
  264. // },
  265. // ),
  266. // const SizedBox(
  267. // width: 10,
  268. // ),
  269. // Expanded(
  270. // child: Text(
  271. // 'Ghi nhớ đăng nhập',
  272. // style: StylesText.caption3,
  273. // ),
  274. // ),
  275. Expanded(child: const SizedBox.shrink()),
  276. GhostButtonWidget(
  277. title: 'Quên Mật Khẩu',
  278. onPressed: () {
  279. Get.to(
  280. () => ForgotPasswordScreen(),
  281. );
  282. },
  283. ),
  284. ],
  285. ),
  286. SizedBox(
  287. height: 24.h,
  288. ),
  289. PrimaryButtonWidget(
  290. title: 'Đăng Nhập',
  291. onPressed: () => onPressedLogin(),
  292. ),
  293. ],
  294. ),
  295. ),
  296. ),
  297. ),
  298. ),
  299. ],
  300. ),
  301. ),
  302. ),
  303. );
  304. }
  305. void onPressedRememberLogin(bool isRemember) {
  306. loginBloc.rememberMe(isRemember);
  307. }
  308. void onPressedForgotPassword() {}
  309. void onPressedLogin() {
  310. loginBloc.loginWithCredential();
  311. }
  312. }