123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- import 'dart:async';
- import 'dart:convert';
- import 'package:firebase_messaging/firebase_messaging.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter_udid/flutter_udid.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- import 'package:google_fonts/google_fonts.dart';
- import 'package:employee_selfservice_mobile/Screens/ForgotPassword/forgotPassword_screen.dart';
- import 'package:employee_selfservice_mobile/Screens/Login/background.dart';
- import 'package:employee_selfservice_mobile/Screens/Login/post_result_model.dart';
- import 'package:employee_selfservice_mobile/Screens/Splash/splash_screen.dart';
- import 'package:progress_dialog_null_safe/progress_dialog_null_safe.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import '../Home/home_screen.dart';
- import 'inputWidget.dart';
- import 'dart:developer' as logDev;
- //import 'package:plain_notification_token/plain_notification_token.dart';
-
- class LoginView extends StatefulWidget {
- @override
- _LoginView createState() => _LoginView();
- }
-
- class _LoginView extends State<LoginView> {
-
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- resizeToAvoidBottomInset: false,
- backgroundColor: Colors.white,
- body: Stack(
- children: <Widget>[
- Background(),
- LoginScreen(),
- ],
- ));
- }
- }
-
- class LoginScreen extends StatefulWidget {
- @override
- State<LoginScreen> createState() => _LoginScreenState();
- }
-
- class _LoginScreenState extends State<LoginScreen> {
- late LoginPostResult loginPostResult;
- String version = "1.0.0";
- String notif_token = "";
-
- late StreamSubscription onTokenRefreshSubscription;
-
- @override
- void initState() {
- super.initState();
- }
-
- @override
- Widget build(BuildContext context) {
- ProgressDialog loading = ProgressDialog(context);
- loading = ProgressDialog(context,
- type: ProgressDialogType.normal, isDismissible: false, showLogs: true);
- loading.style(
- message: 'Please Wait .....',
- borderRadius: 5,
- backgroundColor: Colors.white,
- progressWidget: CircularProgressIndicator(),
- elevation: 10.0,
- padding: EdgeInsets.all(10),
- insetAnimCurve: Curves.easeInOut,
- progress: 0.0,
- maxProgress: 100.0,
- progressTextStyle: TextStyle(
- color: Colors.black, fontSize: 10.0, fontWeight: FontWeight.w400),
- messageTextStyle: TextStyle(
- color: Colors.black, fontSize: 15.0, fontWeight: FontWeight.w600));
-
- return SingleChildScrollView(
- reverse: true,
- padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
- child: Column(
- children: <Widget>[
- Padding(
- padding:
- EdgeInsets.only(top: MediaQuery.of(context).size.height / 2.45),
- ),
- Column(
- children: <Widget>[
- Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: <Widget>[
- Padding(
- padding: EdgeInsets.only(top: 0),
- child: Text(
- "Login",
- style: GoogleFonts.knewave(
- color: Colors.blueAccent, fontSize: 25),
- ),
- ),
- ],
- ),
- Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Padding(
- padding: EdgeInsets.only(left: 40, bottom: 5, top: 20),
- child: Text(
- "Email",
- style: TextStyle(fontSize: 16, color: Colors.black87),
- ),
- ),
- Stack(
- alignment: Alignment.bottomRight,
- children: <Widget>[
- InputWidgetEmail(20.0, 20.0),
- Padding(
- padding: EdgeInsets.only(right: 50),
- ),
- ],
- ),
- ],
- ),
- Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Padding(
- padding: EdgeInsets.only(left: 40, bottom: 5),
- child: Text(
- "Password",
- style: TextStyle(fontSize: 16, color: Colors.black87),
- ),
- ),
- Stack(
- alignment: Alignment.bottomRight,
- children: <Widget>[
- InputWidgetPassword(20.0, 20.0),
- Padding(
- padding: EdgeInsets.only(right: 15),
- child: Row(
- children: <Widget>[
- Expanded(
- child: Padding(
- padding: EdgeInsets.only(top: 0),
- )),
- InkWell(
- child: Container(
- padding: EdgeInsets.all(10),
- decoration: ShapeDecoration(
- shape: CircleBorder(),
- gradient: LinearGradient(
- colors: Gradients2,
- begin: Alignment.topLeft,
- end: Alignment.bottomRight),
- ),
- child: ImageIcon(
- AssetImage("assets/images/ic_forward.png"),
- size: 40,
- color: Colors.white,
- ),
- ),
- onTap: () async {
- String imei;
- try {
- imei = await FlutterUdid.udid;
- } on PlatformException {
- imei = 'Failed to get UDID.';
- }
- logDev.log(imei, name: "IMEI");
- if (!validateForm(context)) {
- return;
- } else {
- await loading.show();
-
- final notif_token = await FirebaseMessaging.instance.getToken();
- logDev.log(notif_token.toString(), name: "NOTIFICATION TOKEN");
-
- LoginPostResult.connectToAPI(
- emailController.text.toString(),
- passwordController.text.toString(),
- notif_token!,
- version,
- platform(),
- imei)
- .then((valueResult) async {
- Map<String, dynamic> object = json.decode(valueResult);
- if (object.containsKey("result").toString() == "true") {
- String status = object['result']['status'].toString();
- if (status == "success") {
- Fluttertoast.showToast(
- msg: "Login Success",
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.CENTER,
- timeInSecForIosWeb: 1,
- textColor: Colors.white,
- fontSize: 16.0);
-
- var prefs = await SharedPreferences.getInstance();
- await prefs.setString('version', version);
- await prefs.setString('device', object['result']['device'].toString());
- await prefs.setString('session', object['result']['hash'].toString());
- await prefs.setString('name', object['result']['name'].toString());
- await prefs.setString('notif_token', notif_token);
-
- emailController.clear();
- passwordController.clear();
-
- await loading.hide();
- Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomeView()));
- } else if (status == "failed") {
- String message = object['result']['message'].toString();
- Fluttertoast.showToast(
- msg: message,
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.CENTER,
- timeInSecForIosWeb: 1,
- textColor: Colors.white,
- fontSize: 16.0);
- await loading.hide();
- }
- } else {
- await loading.hide();
- alertDialogFailedResponse(context);
- }
- });
- }
- },
- )
- ],
- ),
- ),
- /*Padding(
- padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom)
- )*/
- ],
- ),
- ],
- ),
- Padding(
- padding: EdgeInsets.only(bottom: 10),
- ),
- InkWell(
- child:
- roundedRectButton("Forgot Password?", Gradients1, false),
- onTap: () {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => ForgotPasswordView()));
- })
- ],
- )
- ],
- ),
- );
- }
- }
-
- Widget roundedRectButton(
- String title, List<Color> gradient, bool isEndIconVisible) {
- return Builder(builder: (BuildContext mContext) {
- return Align(
- alignment: Alignment.centerLeft,
- child: Stack(
- children: <Widget>[
- Container(
- alignment: Alignment.centerRight,
- width: MediaQuery.of(mContext).size.width / 2.45,
- decoration: ShapeDecoration(
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.only(
- topRight: Radius.circular(20.0),
- bottomRight: Radius.circular(20.0))),
- gradient: LinearGradient(
- colors: gradient,
- begin: Alignment.topLeft,
- end: Alignment.bottomRight),
- ),
- child: Text(title,
- style: TextStyle(
- decoration: TextDecoration.underline,
- color: Colors.white,
- fontSize: 15,
- fontWeight: FontWeight.w500)),
- padding: EdgeInsets.all(10),
- ),
- Visibility(
- visible: isEndIconVisible,
- child: Padding(
- padding: EdgeInsets.only(right: 10),
- child: ImageIcon(
- AssetImage("assets/images/ic_forward.png"),
- size: 30,
- color: Colors.white,
- )),
- ),
- ],
- ),
- );
- });
- }
-
- const List<Color> Gradients1 = [
- /*Color(0xFFFFFFFF),
- Color(0xFFFFFFFF),*/
- Color(0xFF03A0FE),
- Colors.pink,
- ];
-
- const List<Color> Gradients2 = [
- Color(0xFFFF9945),
- Color(0xFFFc6076),
- ];
-
- bool validateForm(BuildContext context) {
- bool result = true;
- if (emailController.text.toString().isEmpty) {
- /*final snackBar = SnackBar(content: Text("Email Required"));
- ScaffoldMessenger.of(context).showSnackBar(snackBar);*/
- Fluttertoast.showToast(
- msg: "Email Required",
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.CENTER,
- timeInSecForIosWeb: 1,
- textColor: Colors.white,
- fontSize: 16.0);
- result = false;
- } else if (!emailController.text.toString().contains("@")) {
- Fluttertoast.showToast(
- msg: "Incorrect email format",
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.CENTER,
- timeInSecForIosWeb: 1,
- textColor: Colors.white,
- fontSize: 16.0);
- result = false;
- } else if (passwordController.text.toString().isEmpty) {
- Fluttertoast.showToast(
- msg: "Password Required",
- toastLength: Toast.LENGTH_SHORT,
- gravity: ToastGravity.CENTER,
- timeInSecForIosWeb: 1,
- textColor: Colors.white,
- fontSize: 16.0);
- result = false;
- }
-
- return result;
- }
-
- alertDialogFailedResponse(BuildContext context){
- Widget okButton = TextButton(
- child: Text("Refresh"),
- onPressed: () {
- Navigator.of(context, rootNavigator: true).pop();
- Navigator.pushReplacement(context, MaterialPageRoute(
- builder: (context) => SplashScreen()));
- },
- );
-
- Widget noButton = TextButton(
- child: Text("Back"),
- onPressed: () {
- Navigator.of(context, rootNavigator: true).pop();
- Navigator.pop(context);
-
- },
- );
-
- // set up the AlertDialog
- AlertDialog alert = AlertDialog(
- title: Text("Employee Self Service"),
- content: Text("Server Response Error"),
- actions: [
- noButton,
- okButton,
- ],
- );
-
- // show the dialog
- showDialog(
- context: context,
- barrierDismissible: false,
- builder: (BuildContext context) {
- return alert;
- },
- );
- }
|