Files
Neo_wallet/neowallet_mobile/lib/Login/LoginValidate.dart
Manasit.K 43c32ef6cf init
2024-10-31 15:57:57 +07:00

293 lines
9.2 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:cathaypay_mobile/Login/forgot_password.dart';
import 'package:cathaypay_mobile/PinCode.dart';
import 'package:cathaypay_mobile/model/token_model.dart';
import 'package:cathaypay_mobile/utils/utils.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../api/api.dart';
TokenModel? tokenModel;
class LoginValidate extends StatefulWidget {
const LoginValidate({Key? key}) : super(key: key);
@override
State<LoginValidate> createState() => _LoginValidateState();
}
class _LoginValidateState extends State<LoginValidate> {
@override
void initState() {
deviceId();
getVersion();
super.initState();
}
String version = "";
getVersion() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
String v = packageInfo.version;
String buildNumber = packageInfo.buildNumber;
setState(() {
version = "$v ($buildNumber)";
});
}
AndroidDeviceInfo? androidDeviceInfo;
IosDeviceInfo? iosDeviceInfo;
deviceId() async {
DeviceInfoPlugin device = DeviceInfoPlugin();
if (Platform.isAndroid) {
androidDeviceInfo = await device.androidInfo;
} else if (Platform.isIOS) {
iosDeviceInfo = await device.iosInfo;
}
}
authen(BuildContext context) {
var device = "";
if (Platform.isAndroid) {
device = androidDeviceInfo?.id ?? "";
} else if (Platform.isIOS) {
device = iosDeviceInfo?.name ?? "";
}
var param = jsonEncode(<dynamic, dynamic>{
"UserName": username.text,
"Password": password.text,
"MobileDeviceId": device,
});
Api.postLogin(context, Api.login, param).then((value) => {
if (value != null)
{
// storeProfile(json.encode(value["token"])),
// tokenModel = TokenModel.fromJson(value["token"]),
tokenModel = TokenModel.fromJson(value),
if (tokenModel?.token != null)
{
storeProfile(json.encode(value)),
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PinCodePage(
login: true,
),
),
)
}
else
{Utils.showAlertDialog(context, "Username หรือ Password ไม่ถูกต้อง")}
// checkUserType()
}
else
{}
});
}
storeProfile(var jsonString) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('access_token', jsonString);
}
refresh() {
setState(() {});
}
// TextEditingController username = TextEditingController(text: "admin");
// TextEditingController password = TextEditingController(text: "P@ssword1");
// TextEditingController username = TextEditingController(text: "toontest");
// TextEditingController password = TextEditingController(text: "123456Aa");
TextEditingController username = TextEditingController();
TextEditingController password = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
SizedBox(
height: 100,
),
Container(
width: 262.83,
child: Image(
image: AssetImage('images/neopay_logo.png'),
),
),
SizedBox(
height: 30,
),
Text(
"${"Welcome".tr()} ${"to NEO TRAVEL".tr()}",
style: TextStyle(
color: Colors.black,
fontSize: 18,
fontWeight: FontWeight.w300,
),
),
SizedBox(
height: 50,
),
Container(
child: Row(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
width: 10,
),
Center(
child: FaIcon(
FontAwesomeIcons.user,
color: Color(0xff65676b),
)),
Expanded(
child: TextField(
// keyboardType: TextInputType.number,
// inputFormatters: [
// FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),
// ],
controller: username,
decoration: InputDecoration(
hintText: 'Username'.tr(),
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(horizontal: 16),
),
),
),
],
),
height: 48,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Color(0xfff2f2f2),
),
),
SizedBox(
height: 10,
),
Container(
child: Row(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
width: 10,
),
Center(
child: FaIcon(
FontAwesomeIcons.lock,
color: Color(0xff65676b),
)),
Expanded(
child: TextField(
obscureText: true,
// keyboardType: TextInputType.number,
// inputFormatters: [
// FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),
// ],
controller: password,
decoration: InputDecoration(
hintText: 'Password'.tr(),
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(horizontal: 16),
),
),
),
],
),
height: 48,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Color(0xfff2f2f2),
),
),
SizedBox(
height: 60,
),
InkWell(
onTap: () {
if (username.text.isEmpty || password.text.isEmpty) {
Utils.showAlertDialog(context, "Please fill in all information.".tr());
return;
}
authen(context);
},
child: Container(
width: 374,
height: 48,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Color(0xff9d001b),
),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"Next".tr(),
textAlign: TextAlign.center,
style: GoogleFonts.kanit(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.w500,
),
),
],
),
),
),
SizedBox(
height: 40,
),
InkWell(
child: Text(
"Forgot Password".tr(),
textAlign: TextAlign.center,
style: GoogleFonts.kanit(
color: Colors.black,
fontSize: 18,
fontWeight: FontWeight.w500,
),
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ForgotPassword()),
);
},
),
Expanded(child: Container()),
Text(
version,
textAlign: TextAlign.center,
style: GoogleFonts.kanit(
color: Colors.black,
fontSize: 14,
),
),
SizedBox(height: 20),
],
),
),
);
}
}