633 lines
27 KiB
Dart
633 lines
27 KiB
Dart
import 'dart:io';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
|
||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||
import 'package:flutter_svg/flutter_svg.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:regroup/Common/base_manager.dart';
|
||
import 'package:regroup/Login/ViewModel/LoginApi.dart';
|
||
import 'package:regroup/Main_Screens/Community/ViewModel/getmethod.dart';
|
||
import 'package:regroup/Utils/Common/AppleOAuthService.dart';
|
||
import 'package:regroup/Utils/Common/CustomTextformfield.dart';
|
||
import 'package:regroup/Utils/Common/googleOAuthService.dart';
|
||
import 'package:regroup/Utils/Common/sized_box.dart';
|
||
import 'package:regroup/Utils/dialogs.dart';
|
||
import 'package:regroup/Utils/texts.dart';
|
||
import 'package:regroup/onboarding/Signup/view_model/getUserprofile.dart';
|
||
import 'package:regroup/resources/routes/route_name.dart';
|
||
import 'package:remove_emoji_input_formatter/remove_emoji_input_formatter.dart';
|
||
|
||
class LoginScreen extends StatefulWidget {
|
||
const LoginScreen({super.key});
|
||
|
||
@override
|
||
State<LoginScreen> createState() => _LoginScreenState();
|
||
}
|
||
|
||
class _LoginScreenState extends State<LoginScreen> {
|
||
final TextEditingController _emailController = TextEditingController();
|
||
RxBool isObscured = true.obs;
|
||
final TextEditingController _passwordController = TextEditingController();
|
||
GoogleAuthService googleSigninController = Get.put(GoogleAuthService());
|
||
bool isLoading = false;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
}
|
||
|
||
_loginWithApple() {
|
||
AppleOAuthService().logIn();
|
||
}
|
||
|
||
_loginWithGoogle() {
|
||
googleSigninController.handleGoogleSignIn().then((value) async {
|
||
final resp = await LoginAPI().storeGoogleSignin(
|
||
{"google_access_token": value, "one_signal_player_id": "ABCD"});
|
||
if (value != 'Google Sign-In canceled') {
|
||
if (resp.message == "go-to-signin-via-oauth") {
|
||
Get.toNamed(RouteName.verifygoogleapplepage,
|
||
arguments: {"email": resp.data});
|
||
} else if (value == null) {
|
||
Get.snackbar(
|
||
'Error',
|
||
resp.data["message"][0],
|
||
snackPosition: SnackPosition.BOTTOM,
|
||
backgroundColor: Colors.red,
|
||
colorText: Colors.white,
|
||
);
|
||
} else {
|
||
Get.snackbar(
|
||
"Success!",
|
||
'Login successful!',
|
||
duration: const Duration(seconds: 2),
|
||
colorText: Colors.white,
|
||
backgroundColor: Colors.green,
|
||
margin: const EdgeInsets.all(8),
|
||
snackStyle: SnackStyle.FLOATING,
|
||
snackPosition: SnackPosition.BOTTOM,
|
||
);
|
||
// Get.toNamed(RouteName.mainscreen,arguments: 0);
|
||
await Getuserdetails().Getuser().then((value) {
|
||
if (getuserobj?.data?.userData?.isProfileUpdated == 0) {
|
||
String? accountype =
|
||
getuserobj?.data?.userData?.principalTypeXid.toString();
|
||
|
||
if (accountype == "1") {
|
||
Get.toNamed(RouteName.tellusindividualscreen,
|
||
arguments: {'pageroute': "mainscreen"});
|
||
} else if (accountype == "2") {
|
||
Get.toNamed(RouteName.tellusbusinessscreen,
|
||
arguments: {'pageroute': "mainscreen"});
|
||
}
|
||
} else {
|
||
Get.toNamed(RouteName.mainscreen, arguments: 0);
|
||
}
|
||
});
|
||
logger.d("go to login");
|
||
}
|
||
} else {
|
||
Get.snackbar(
|
||
'Error',
|
||
resp.data["message"][0],
|
||
snackPosition: SnackPosition.BOTTOM,
|
||
backgroundColor: Colors.red,
|
||
colorText: Colors.white,
|
||
);
|
||
}
|
||
});
|
||
}
|
||
|
||
_checkIfisLoggedIn() async {
|
||
await _logout();
|
||
final accessToken = await FacebookAuth.instance.accessToken;
|
||
if (accessToken != null) {
|
||
print("/////////////////////////////////////////xcheck");
|
||
|
||
final userData = await FacebookAuth.instance.getUserData();
|
||
|
||
logger.i(accessToken.token);
|
||
logger.i(userData.toString());
|
||
} else {
|
||
print("/////////////////////////////////////////check");
|
||
_login();
|
||
}
|
||
}
|
||
|
||
_login() async {
|
||
final LoginResult result = await FacebookAuth.instance.login();
|
||
|
||
if (result.status == LoginStatus.success) {
|
||
final userData = await FacebookAuth.instance.getUserData();
|
||
logger.i(userData['email']);
|
||
logger.i(userData['id']);
|
||
|
||
await LoginAPI().storeFacebookSignin(
|
||
{"facebook_auth_token": userData['id']},
|
||
emailReceived: userData['email']);
|
||
} else {
|
||
print(result.status);
|
||
print(result.message);
|
||
}
|
||
}
|
||
|
||
_logout() async {
|
||
await FacebookAuth.instance.logOut();
|
||
setState(() {});
|
||
}
|
||
|
||
_logincheck() async {
|
||
if (_emailController.text.isBlank! || _passwordController.text.isBlank!) {
|
||
Get.snackbar(
|
||
'Error',
|
||
'Enter your credentials',
|
||
snackPosition: SnackPosition.BOTTOM,
|
||
backgroundColor: Colors.red,
|
||
colorText: Colors.white,
|
||
);
|
||
} else if (!_emailController.text.isEmail) {
|
||
Get.snackbar(
|
||
'Error',
|
||
'Enter a valid e-mail address',
|
||
snackPosition: SnackPosition.BOTTOM,
|
||
backgroundColor: Colors.red,
|
||
colorText: Colors.white,
|
||
);
|
||
} else {
|
||
Map<String, String> updata = {
|
||
"email_address": _emailController.text,
|
||
"password": _passwordController.text,
|
||
// "access-token" : ""
|
||
//
|
||
// "player_id": global.player_id!,
|
||
};
|
||
final data = await LoginAPI().loginApi(updata);
|
||
if (data.status == ResponseStatus.SUCCESS) {
|
||
// await global.setname();
|
||
// Get.snackbar(
|
||
// "Success!",
|
||
// 'Login successful!',
|
||
// duration: const Duration(seconds: 2),
|
||
// colorText: Colors.white,
|
||
// backgroundColor: Colors.green,
|
||
// margin: const EdgeInsets.all(8),
|
||
// snackStyle: SnackStyle.FLOATING,
|
||
// snackPosition: SnackPosition.BOTTOM,
|
||
// );
|
||
return utils.showToast(data.message);
|
||
} else if (data.status == ResponseStatus.FAILED) {
|
||
Get.snackbar(
|
||
"Error!",
|
||
data.message,
|
||
duration: const Duration(seconds: 2),
|
||
colorText: Colors.white,
|
||
backgroundColor: Colors.red,
|
||
margin: const EdgeInsets.all(8),
|
||
snackStyle: SnackStyle.FLOATING,
|
||
snackPosition: SnackPosition.BOTTOM,
|
||
);
|
||
// _controller.isTextFieldEnabled.value = true;
|
||
} else {
|
||
// btnController.reset();
|
||
Get.snackbar(
|
||
"Error!",
|
||
data.data['message'],
|
||
duration: const Duration(seconds: 2),
|
||
colorText: Colors.white,
|
||
backgroundColor: Colors.red,
|
||
margin: const EdgeInsets.all(8),
|
||
snackStyle: SnackStyle.FLOATING,
|
||
snackPosition: SnackPosition.BOTTOM,
|
||
);
|
||
// _controller.isTextFieldEnabled.value = true;
|
||
}
|
||
}
|
||
}
|
||
|
||
Future<bool> _onBackButtonPressed(
|
||
BuildContext context,
|
||
TextEditingController emailController,
|
||
TextEditingController passwordController) async {
|
||
bool? exitApp = await showDialog(
|
||
barrierDismissible: false,
|
||
context: context,
|
||
builder: (context) {
|
||
return AlertDialog(
|
||
backgroundColor: const Color(0xff222935),
|
||
title: Text(
|
||
'Exit App',
|
||
style: TextStyle(
|
||
color: Colors.white,
|
||
fontSize: 16.sp,
|
||
fontWeight: FontWeight.w500),
|
||
),
|
||
content: Text(
|
||
'Do you really want to close the app?',
|
||
style: TextStyle(
|
||
color: Colors.white,
|
||
fontSize: 14.sp,
|
||
fontWeight: FontWeight.w500),
|
||
),
|
||
actions: [
|
||
TextButton(
|
||
onPressed: () {
|
||
Navigator.of(context).pop(false);
|
||
},
|
||
child: const Text(
|
||
'No',
|
||
style: TextStyle(
|
||
color: Colors.white,
|
||
),
|
||
),
|
||
),
|
||
TextButton(
|
||
onPressed: () {
|
||
emailController.clear();
|
||
passwordController.clear();
|
||
|
||
// Reset form state to remove error borders
|
||
|
||
SystemNavigator.pop();
|
||
Navigator.pop(context);
|
||
},
|
||
child: const Text(
|
||
'Yes',
|
||
style: TextStyle(
|
||
color: Color(0xFFD90B2E),
|
||
),
|
||
),
|
||
)
|
||
],
|
||
);
|
||
});
|
||
return exitApp ?? false;
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return WillPopScope(
|
||
onWillPop: () async {
|
||
_onBackButtonPressed(context, _emailController, _passwordController);
|
||
|
||
return true;
|
||
},
|
||
child: GestureDetector(
|
||
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
|
||
child: Scaffold(
|
||
resizeToAvoidBottomInset:
|
||
false, // Prevent resizing when the keyboard opens
|
||
backgroundColor: const Color.fromARGB(255, 18, 32, 47),
|
||
body: Stack(
|
||
clipBehavior: Clip.none,
|
||
children: [
|
||
Container(
|
||
decoration: const BoxDecoration(
|
||
image: DecorationImage(
|
||
image:
|
||
AssetImage("assets/images/png/Choice screen.png"),
|
||
fit: BoxFit.fill)),
|
||
),
|
||
Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
SizedBox(
|
||
height: 130.h,
|
||
),
|
||
Padding(
|
||
padding: EdgeInsets.symmetric(horizontal: 16.w),
|
||
child: Align(
|
||
alignment: Alignment.center,
|
||
child: SizedBox(
|
||
width: 107.w,
|
||
height: 86.h,
|
||
child: SvgPicture.asset(
|
||
"assets/images/svg/onboarding2.svg",
|
||
fit: BoxFit.cover,
|
||
),
|
||
),
|
||
),
|
||
),
|
||
sizedBoxHeight(20.h),
|
||
Align(
|
||
alignment: Alignment.center,
|
||
child: text22400FCFCFC("Find your community")),
|
||
sizedBoxHeight(40.h),
|
||
Expanded(
|
||
child: Padding(
|
||
padding: EdgeInsets.symmetric(horizontal: 16.w),
|
||
child: SingleChildScrollView(
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
text16400white('Email address'),
|
||
sizedBoxHeight(10.h),
|
||
CustomTextFormField(
|
||
textEditingController: _emailController,
|
||
hintText: "Enter your email address",
|
||
leadingIcon:
|
||
// const Icon(Icons.mail_outline),
|
||
SizedBox(
|
||
width: 22.w,
|
||
height: 17.h,
|
||
child: Image.asset(
|
||
'assets/images/png/mail.png',
|
||
width: 22.w,
|
||
height: 17.h,
|
||
),
|
||
),
|
||
// validationMessage: errormsg.value,
|
||
validator: (value) {
|
||
if (value!.isEmpty) {
|
||
return 'Enter your e-mail address';
|
||
}
|
||
if (!RegExp(
|
||
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$')
|
||
.hasMatch(value)) {
|
||
return 'Enter a valid e-mail address';
|
||
}
|
||
return null;
|
||
},
|
||
|
||
inputFormatters: [
|
||
LengthLimitingTextInputFormatter(30),
|
||
RemoveEmojiInputFormatter()
|
||
],
|
||
),
|
||
sizedBoxHeight(10.h),
|
||
text16400white('Password'),
|
||
sizedBoxHeight(10.h),
|
||
CustomTextFormField(
|
||
isInputPassword: true,
|
||
textEditingController: _passwordController,
|
||
hintText: 'Enter your password',
|
||
leadingIcon: Image.asset(
|
||
'assets/images/png/lock.png',
|
||
width: 22.w,
|
||
height: 17.h,
|
||
),
|
||
// validationMessage: errormsgpass.value,
|
||
validator: (value) {
|
||
if (value!.isEmpty) {
|
||
return 'Please enter your password';
|
||
}
|
||
if (!RegExp(
|
||
r'^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$')
|
||
.hasMatch(value)) {
|
||
return 'Enter a valid password';
|
||
}
|
||
|
||
return null;
|
||
},
|
||
// onInput: (p0) {
|
||
// if (p0 == null || p0.isEmpty) {
|
||
// errormsgpass.value =
|
||
// 'Please enter your password';
|
||
// }
|
||
// return null;
|
||
// },
|
||
inputFormatters: [
|
||
LengthLimitingTextInputFormatter(20),
|
||
RemoveEmojiInputFormatter()
|
||
],
|
||
),
|
||
sizedBoxHeight(10.h),
|
||
Align(
|
||
alignment: Alignment.centerRight,
|
||
child: Padding(
|
||
padding: EdgeInsets.only(right: 6.w),
|
||
child: GestureDetector(
|
||
onTap: () {
|
||
Get.toNamed(RouteName.forgotpass);
|
||
},
|
||
child: text14400white('Forgot password ?'),
|
||
),
|
||
),
|
||
),
|
||
sizedBoxHeight(40.h),
|
||
Center(
|
||
child: SizedBox(
|
||
width: isLoading ? 50 : double.infinity,
|
||
height: 50,
|
||
child: ElevatedButton(
|
||
style: ElevatedButton.styleFrom(
|
||
backgroundColor: const Color(0xFFD90B2E),
|
||
padding: EdgeInsets.zero),
|
||
onPressed: () {
|
||
setState(
|
||
() {
|
||
isLoading = true;
|
||
},
|
||
);
|
||
Future.delayed(
|
||
const Duration(seconds: 1),
|
||
() {
|
||
setState(() {
|
||
isLoading = false;
|
||
});
|
||
_logincheck();
|
||
},
|
||
);
|
||
},
|
||
child: isLoading
|
||
? const Row(
|
||
mainAxisAlignment:
|
||
MainAxisAlignment.center,
|
||
crossAxisAlignment:
|
||
CrossAxisAlignment.center,
|
||
children: [
|
||
SizedBox(
|
||
width: 30,
|
||
height: 30,
|
||
child: CircularProgressIndicator(
|
||
color: Colors.white,
|
||
strokeWidth: 2,
|
||
),
|
||
),
|
||
],
|
||
)
|
||
: Text(
|
||
'Login',
|
||
textAlign: TextAlign.center,
|
||
style: TextStyle(
|
||
fontFamily: "Helvetica",
|
||
fontWeight: FontWeight.w400,
|
||
fontSize: 16.sp,
|
||
color: const Color(0xFFFCFCFC),
|
||
),
|
||
),
|
||
),
|
||
),
|
||
),
|
||
sizedBoxHeight(20.h),
|
||
GestureDetector(
|
||
onTap: () {
|
||
Get.toNamed(RouteName.signupscreen);
|
||
},
|
||
child: Row(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
text14400white('Don’t have account ? '),
|
||
sizedBoxWidth(5.w),
|
||
text14700white('Sign up')
|
||
],
|
||
),
|
||
),
|
||
sizedBoxHeight(30.h),
|
||
Row(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
Container(
|
||
width: 160,
|
||
decoration: const ShapeDecoration(
|
||
shape: RoundedRectangleBorder(
|
||
side: BorderSide(
|
||
width: 0.50,
|
||
strokeAlign:
|
||
BorderSide.strokeAlignCenter,
|
||
color: Color(0xFF434A53),
|
||
),
|
||
),
|
||
),
|
||
),
|
||
sizedBoxWidth(6.w),
|
||
text14400white('Or'),
|
||
sizedBoxWidth(6.w),
|
||
Container(
|
||
width: 160,
|
||
decoration: const ShapeDecoration(
|
||
shape: RoundedRectangleBorder(
|
||
side: BorderSide(
|
||
width: 0.50,
|
||
strokeAlign:
|
||
BorderSide.strokeAlignCenter,
|
||
color: Color(0xFF434A53),
|
||
),
|
||
),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
sizedBoxHeight(20.h),
|
||
Center(
|
||
child: SizedBox(
|
||
width: 220.w,
|
||
child: Row(
|
||
mainAxisAlignment:
|
||
MainAxisAlignment.spaceAround,
|
||
children: [
|
||
if (Platform.isIOS)
|
||
GestureDetector(
|
||
onTap: () {
|
||
_loginWithApple();
|
||
},
|
||
child: Container(
|
||
width: 55,
|
||
height: 55,
|
||
decoration: ShapeDecoration(
|
||
gradient: LinearGradient(
|
||
begin: const Alignment(
|
||
0.71, -0.70),
|
||
end:
|
||
const Alignment(-0.71, 0.7),
|
||
colors: [
|
||
Colors.white.withOpacity(
|
||
0.07999999821186066),
|
||
Colors.white.withOpacity(
|
||
0.12999999523162842)
|
||
],
|
||
),
|
||
shape: const OvalBorder(
|
||
side: BorderSide(
|
||
width: 0.50,
|
||
color: Color(0xFF434A53)),
|
||
),
|
||
image: const DecorationImage(
|
||
image: AssetImage(
|
||
'assets/images/png/login2.png'))),
|
||
),
|
||
),
|
||
// const Spacer(),
|
||
if (Platform.isAndroid)
|
||
GestureDetector(
|
||
onTap: () {
|
||
_loginWithGoogle();
|
||
},
|
||
child: Container(
|
||
width: 55,
|
||
height: 55,
|
||
decoration: ShapeDecoration(
|
||
gradient: LinearGradient(
|
||
begin: const Alignment(
|
||
0.71, -0.70),
|
||
end:
|
||
const Alignment(-0.71, 0.7),
|
||
colors: [
|
||
Colors.white.withOpacity(
|
||
0.07999999821186066),
|
||
Colors.white.withOpacity(
|
||
0.12999999523162842)
|
||
],
|
||
),
|
||
shape: const OvalBorder(
|
||
side: BorderSide(
|
||
width: 0.50,
|
||
color: Color(0xFF434A53)),
|
||
),
|
||
image: const DecorationImage(
|
||
image: AssetImage(
|
||
'assets/images/png/login3.png'))),
|
||
),
|
||
),
|
||
// const Spacer(),
|
||
GestureDetector(
|
||
onTap: () async {
|
||
_checkIfisLoggedIn();
|
||
},
|
||
child: Container(
|
||
width: 55,
|
||
height: 55,
|
||
decoration: ShapeDecoration(
|
||
gradient: LinearGradient(
|
||
begin:
|
||
const Alignment(0.71, -0.70),
|
||
end: const Alignment(-0.71, 0.7),
|
||
colors: [
|
||
Colors.white.withOpacity(
|
||
0.07999999821186066),
|
||
Colors.white.withOpacity(
|
||
0.12999999523162842)
|
||
],
|
||
),
|
||
shape: const OvalBorder(
|
||
side: BorderSide(
|
||
width: 0.50,
|
||
color: Color(0xFF434A53)),
|
||
),
|
||
image: const DecorationImage(
|
||
image: AssetImage(
|
||
'assets/images/png/login4.png'))),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
// sizedBoxHeight(100)
|
||
],
|
||
),
|
||
),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|