GSI - Employe Self Service Mobile
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.

386 lines
14 KiB

2 months ago
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:firebase_messaging/firebase_messaging.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter_udid/flutter_udid.dart';
  7. import 'package:fluttertoast/fluttertoast.dart';
  8. import 'package:google_fonts/google_fonts.dart';
  9. import 'package:employee_selfservice_mobile/Screens/ForgotPassword/forgotPassword_screen.dart';
  10. import 'package:employee_selfservice_mobile/Screens/Login/background.dart';
  11. import 'package:employee_selfservice_mobile/Screens/Login/login_post.dart';
  12. import 'package:employee_selfservice_mobile/Screens/Splash/splash_screen.dart';
  13. import 'package:progress_dialog_null_safe/progress_dialog_null_safe.dart';
  14. import 'package:shared_preferences/shared_preferences.dart';
  15. import '../Home/home_screen.dart';
  16. import 'inputWidget.dart';
  17. import 'dart:developer' as logDev;
  18. //import 'package:plain_notification_token/plain_notification_token.dart';
  19. class LoginView extends StatefulWidget {
  20. @override
  21. _LoginView createState() => _LoginView();
  22. }
  23. class _LoginView extends State<LoginView> {
  24. @override
  25. Widget build(BuildContext context) {
  26. return Scaffold(
  27. resizeToAvoidBottomInset: false,
  28. backgroundColor: Colors.white,
  29. body: Stack(
  30. children: <Widget>[
  31. Background(),
  32. LoginScreen(),
  33. ],
  34. ));
  35. }
  36. }
  37. class LoginScreen extends StatefulWidget {
  38. @override
  39. State<LoginScreen> createState() => _LoginScreenState();
  40. }
  41. class _LoginScreenState extends State<LoginScreen> {
  42. late LoginPostResult loginPostResult;
  43. String version = "1.0.0";
  44. String notif_token = "";
  45. late StreamSubscription onTokenRefreshSubscription;
  46. @override
  47. void initState() {
  48. super.initState();
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. ProgressDialog loading = ProgressDialog(context);
  53. loading = ProgressDialog(context,
  54. type: ProgressDialogType.normal, isDismissible: false, showLogs: true);
  55. loading.style(
  56. message: 'Please Wait .....',
  57. borderRadius: 5,
  58. backgroundColor: Colors.white,
  59. progressWidget: CircularProgressIndicator(),
  60. elevation: 10.0,
  61. padding: EdgeInsets.all(10),
  62. insetAnimCurve: Curves.easeInOut,
  63. progress: 0.0,
  64. maxProgress: 100.0,
  65. progressTextStyle: TextStyle(
  66. color: Colors.black, fontSize: 10.0, fontWeight: FontWeight.w400),
  67. messageTextStyle: TextStyle(
  68. color: Colors.black, fontSize: 15.0, fontWeight: FontWeight.w600));
  69. return SingleChildScrollView(
  70. reverse: true,
  71. padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
  72. child: Column(
  73. children: <Widget>[
  74. Padding(
  75. padding:
  76. EdgeInsets.only(top: MediaQuery.of(context).size.height / 2.45),
  77. ),
  78. Column(
  79. children: <Widget>[
  80. Column(
  81. crossAxisAlignment: CrossAxisAlignment.center,
  82. children: <Widget>[
  83. Padding(
  84. padding: EdgeInsets.only(top: 0),
  85. child: Text(
  86. "Login",
  87. style: GoogleFonts.knewave(
  88. color: Colors.blueAccent, fontSize: 25),
  89. ),
  90. ),
  91. ],
  92. ),
  93. Column(
  94. crossAxisAlignment: CrossAxisAlignment.start,
  95. children: <Widget>[
  96. Padding(
  97. padding: EdgeInsets.only(left: 40, bottom: 5, top: 20),
  98. child: Text(
  99. "Email",
  100. style: TextStyle(fontSize: 16, color: Colors.black87),
  101. ),
  102. ),
  103. Stack(
  104. alignment: Alignment.bottomRight,
  105. children: <Widget>[
  106. InputWidgetEmail(20.0, 20.0),
  107. Padding(
  108. padding: EdgeInsets.only(right: 50),
  109. ),
  110. ],
  111. ),
  112. ],
  113. ),
  114. Column(
  115. crossAxisAlignment: CrossAxisAlignment.start,
  116. children: <Widget>[
  117. Padding(
  118. padding: EdgeInsets.only(left: 40, bottom: 5),
  119. child: Text(
  120. "Password",
  121. style: TextStyle(fontSize: 16, color: Colors.black87),
  122. ),
  123. ),
  124. Stack(
  125. alignment: Alignment.bottomRight,
  126. children: <Widget>[
  127. InputWidgetPassword(20.0, 20.0),
  128. Padding(
  129. padding: EdgeInsets.only(right: 15),
  130. child: Row(
  131. children: <Widget>[
  132. Expanded(
  133. child: Padding(
  134. padding: EdgeInsets.only(top: 0),
  135. )),
  136. InkWell(
  137. child: Container(
  138. padding: EdgeInsets.all(10),
  139. decoration: ShapeDecoration(
  140. shape: CircleBorder(),
  141. gradient: LinearGradient(
  142. colors: Gradients2,
  143. begin: Alignment.topLeft,
  144. end: Alignment.bottomRight),
  145. ),
  146. child: ImageIcon(
  147. AssetImage("assets/images/ic_forward.png"),
  148. size: 40,
  149. color: Colors.white,
  150. ),
  151. ),
  152. onTap: () async {
  153. /*String imei;
  154. try {
  155. imei = await FlutterUdid.udid;
  156. } on PlatformException {
  157. imei = 'Failed to get UDID.';
  158. }
  159. logDev.log(imei, name: "IMEI");*/
  160. if (!validateForm(context)) {
  161. return;
  162. } else {
  163. await loading.show();
  164. final notif_token = await FirebaseMessaging.instance.getToken();
  165. logDev.log(notif_token.toString(), name: "NOTIFICATION TOKEN");
  166. LoginPostResult.connectToAPI(
  167. emailController.text.toString(),
  168. passwordController.text.toString(),
  169. notif_token!,
  170. version,
  171. platform(),)
  172. .then((valueResult) async {
  173. Map<String, dynamic> object = json.decode(valueResult);
  174. if (object.containsKey("result").toString() == "true") {
  175. String status = object['result']['status'].toString();
  176. if (status == "success") {
  177. Fluttertoast.showToast(
  178. msg: "Login Success",
  179. toastLength: Toast.LENGTH_SHORT,
  180. gravity: ToastGravity.CENTER,
  181. timeInSecForIosWeb: 1,
  182. textColor: Colors.white,
  183. fontSize: 16.0);
  184. var prefs = await SharedPreferences.getInstance();
  185. await prefs.setString('version', version);
  186. await prefs.setString('device', object['result']['device'].toString());
  187. await prefs.setString('session', object['result']['hash'].toString());
  188. await prefs.setString('name', object['result']['name'].toString());
  189. await prefs.setString('notif_token', notif_token);
  190. emailController.clear();
  191. passwordController.clear();
  192. await loading.hide();
  193. Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomeView()));
  194. } else if (status == "failed") {
  195. String message = object['result']['message'].toString();
  196. Fluttertoast.showToast(
  197. msg: message,
  198. toastLength: Toast.LENGTH_SHORT,
  199. gravity: ToastGravity.CENTER,
  200. timeInSecForIosWeb: 1,
  201. textColor: Colors.white,
  202. fontSize: 16.0);
  203. await loading.hide();
  204. }
  205. } else {
  206. await loading.hide();
  207. alertDialogFailedResponse(context);
  208. }
  209. });
  210. }
  211. },
  212. )
  213. ],
  214. ),
  215. ),
  216. /*Padding(
  217. padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom)
  218. )*/
  219. ],
  220. ),
  221. ],
  222. ),
  223. Padding(
  224. padding: EdgeInsets.only(bottom: 10),
  225. ),
  226. InkWell(
  227. child:
  228. roundedRectButton("Forgot Password?", Gradients1, false),
  229. onTap: () {
  230. Navigator.push(
  231. context,
  232. MaterialPageRoute(
  233. builder: (context) => ForgotPasswordView()));
  234. })
  235. ],
  236. )
  237. ],
  238. ),
  239. );
  240. }
  241. }
  242. Widget roundedRectButton(
  243. String title, List<Color> gradient, bool isEndIconVisible) {
  244. return Builder(builder: (BuildContext mContext) {
  245. return Align(
  246. alignment: Alignment.centerLeft,
  247. child: Stack(
  248. children: <Widget>[
  249. Container(
  250. alignment: Alignment.centerRight,
  251. width: MediaQuery.of(mContext).size.width / 2.45,
  252. decoration: ShapeDecoration(
  253. shape: RoundedRectangleBorder(
  254. borderRadius: BorderRadius.only(
  255. topRight: Radius.circular(20.0),
  256. bottomRight: Radius.circular(20.0))),
  257. gradient: LinearGradient(
  258. colors: gradient,
  259. begin: Alignment.topLeft,
  260. end: Alignment.bottomRight),
  261. ),
  262. child: Text(title,
  263. style: TextStyle(
  264. decoration: TextDecoration.underline,
  265. color: Colors.white,
  266. fontSize: 15,
  267. fontWeight: FontWeight.w500)),
  268. padding: EdgeInsets.all(10),
  269. ),
  270. Visibility(
  271. visible: isEndIconVisible,
  272. child: Padding(
  273. padding: EdgeInsets.only(right: 10),
  274. child: ImageIcon(
  275. AssetImage("assets/images/ic_forward.png"),
  276. size: 30,
  277. color: Colors.white,
  278. )),
  279. ),
  280. ],
  281. ),
  282. );
  283. });
  284. }
  285. const List<Color> Gradients1 = [
  286. /*Color(0xFFFFFFFF),
  287. Color(0xFFFFFFFF),*/
  288. Color(0xFF03A0FE),
  289. Colors.pink,
  290. ];
  291. const List<Color> Gradients2 = [
  292. Color(0xFFFF9945),
  293. Color(0xFFFc6076),
  294. ];
  295. bool validateForm(BuildContext context) {
  296. bool result = true;
  297. if (emailController.text.toString().isEmpty) {
  298. /*final snackBar = SnackBar(content: Text("Email Required"));
  299. ScaffoldMessenger.of(context).showSnackBar(snackBar);*/
  300. Fluttertoast.showToast(
  301. msg: "Email Required",
  302. toastLength: Toast.LENGTH_SHORT,
  303. gravity: ToastGravity.CENTER,
  304. timeInSecForIosWeb: 1,
  305. textColor: Colors.white,
  306. fontSize: 16.0);
  307. result = false;
  308. } else if (!emailController.text.toString().contains("@")) {
  309. Fluttertoast.showToast(
  310. msg: "Incorrect email format",
  311. toastLength: Toast.LENGTH_SHORT,
  312. gravity: ToastGravity.CENTER,
  313. timeInSecForIosWeb: 1,
  314. textColor: Colors.white,
  315. fontSize: 16.0);
  316. result = false;
  317. } else if (passwordController.text.toString().isEmpty) {
  318. Fluttertoast.showToast(
  319. msg: "Password Required",
  320. toastLength: Toast.LENGTH_SHORT,
  321. gravity: ToastGravity.CENTER,
  322. timeInSecForIosWeb: 1,
  323. textColor: Colors.white,
  324. fontSize: 16.0);
  325. result = false;
  326. }
  327. return result;
  328. }
  329. alertDialogFailedResponse(BuildContext context){
  330. Widget okButton = TextButton(
  331. child: Text("Refresh"),
  332. onPressed: () {
  333. Navigator.of(context, rootNavigator: true).pop();
  334. Navigator.pushReplacement(context, MaterialPageRoute(
  335. builder: (context) => SplashScreen()));
  336. },
  337. );
  338. Widget noButton = TextButton(
  339. child: Text("Back"),
  340. onPressed: () {
  341. Navigator.of(context, rootNavigator: true).pop();
  342. Navigator.pop(context);
  343. },
  344. );
  345. // set up the AlertDialog
  346. AlertDialog alert = AlertDialog(
  347. title: Text("Employee Self Service"),
  348. content: Text("Server Response Error"),
  349. actions: [
  350. noButton,
  351. okButton,
  352. ],
  353. );
  354. // show the dialog
  355. showDialog(
  356. context: context,
  357. barrierDismissible: false,
  358. builder: (BuildContext context) {
  359. return alert;
  360. },
  361. );
  362. }