TKK_E32210585_aplikasi/lib/ui/login_screen.dart

312 lines
12 KiB
Dart

import 'package:e_commerce/const/AppColors.dart';
import 'package:e_commerce/ui/bottom_nav_controller.dart';
import 'package:e_commerce/ui/registration_screen.dart';
import 'package:e_commerce/widgets/customButton.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
TextEditingController _emailController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
bool _obscureText = true;
signIn() async {
try {
UserCredential userCredential = await FirebaseAuth.instance
.signInWithEmailAndPassword(
email: _emailController.text, password: _passwordController.text);
var authCredential = userCredential.user;
if (authCredential != null && authCredential.uid.isNotEmpty) {
Navigator.pushReplacement(
context, CupertinoPageRoute(builder: (_) => BottomNavController()));
} else {
Fluttertoast.showToast(
msg: "Something went wrong",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP,
backgroundColor: AppColors.deep_orange,
textColor: Colors.white,
fontSize: 16.0,
);
}
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
Fluttertoast.showToast(
msg: "No user found for that email.",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP,
backgroundColor: AppColors.deep_orange,
textColor: Colors.white,
fontSize: 16.0,
);
} else if (e.code == 'wrong-password') {
Fluttertoast.showToast(
msg: "Wrong password provided for that user.",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP,
backgroundColor: AppColors.deep_orange,
textColor: Colors.white,
fontSize: 16.0,
);
} else if (e.code == 'invalid-email') {
Fluttertoast.showToast(
msg: "Invalid email format.",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP,
backgroundColor: AppColors.deep_orange,
textColor: Colors.white,
fontSize: 16.0,
);
} else {
Fluttertoast.showToast(
msg: "An error occurred: ${e.message}",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP,
backgroundColor: AppColors.deep_orange,
textColor: Colors.white,
fontSize: 16.0,
);
}
} catch (e) {
Fluttertoast.showToast(
msg: "Unexpected error occurred",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0,
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColors.deep_orange,
body: SafeArea(
child: Column(
children: [
SizedBox(
height: 150.h,
width: ScreenUtil().screenWidth,
child: Padding(
padding: EdgeInsets.only(left: 20.w),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
IconButton(
onPressed: null,
icon: Icon(
Icons.light,
color: Colors.transparent,
),
),
Text(
"Login",
style: TextStyle(fontSize: 22.sp, color: Colors.white),
),
],
),
),
),
Expanded(
child: Container(
width: ScreenUtil().screenWidth,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(28.r),
topRight: Radius.circular(28.r),
),
),
child: Padding(
padding: EdgeInsets.all(20.w),
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 20.h,
),
Text(
"Welcome to PojokTI",
style: TextStyle(
fontSize: 22.sp, color: AppColors.deep_orange),
),
Text(
"Everyday shopping made easy.",
style: TextStyle(
fontSize: 14.sp,
color: Color(0xFFBBBBBB),
),
),
SizedBox(
height: 15.h,
),
Row(
children: [
Container(
height: 48.h,
width: 41.w,
decoration: BoxDecoration(
color: AppColors.deep_orange,
borderRadius: BorderRadius.circular(12.r)),
child: Center(
child: Icon(
Icons.email_outlined,
color: Colors.white,
size: 20.w,
),
),
),
SizedBox(
width: 10.w,
),
Expanded(
child: TextField(
controller: _emailController,
decoration: InputDecoration(
hintText: "example@gmail.com",
hintStyle: TextStyle(
fontSize: 14.sp,
color: Colors.grey,
),
labelText: 'EMAIL',
labelStyle: TextStyle(
fontSize: 15.sp,
color: AppColors.deep_orange,
),
),
),
),
],
),
SizedBox(
height: 10.h,
),
Row(
children: [
Container(
height: 48.h,
width: 41.w,
decoration: BoxDecoration(
color: AppColors.deep_orange,
borderRadius: BorderRadius.circular(12.r)),
child: Center(
child: Icon(
Icons.lock_outline,
color: Colors.white,
size: 20.w,
),
),
),
SizedBox(
width: 10.w,
),
Expanded(
child: TextField(
controller: _passwordController,
obscureText: _obscureText,
decoration: InputDecoration(
hintText: "password must be 6 character",
hintStyle: TextStyle(
fontSize: 14.sp,
color: Colors.grey,
),
labelText: 'PASSWORD',
labelStyle: TextStyle(
fontSize: 15.sp,
color: AppColors.deep_orange,
),
suffixIcon: _obscureText == true
? IconButton(
onPressed: () {
setState(() {
_obscureText = false;
});
},
icon: Icon(
Icons.remove_red_eye,
size: 20.w,
))
: IconButton(
onPressed: () {
setState(() {
_obscureText = true;
});
},
icon: Icon(
Icons.visibility_off,
size: 20.w,
)),
),
),
),
],
),
SizedBox(
height: 50.h,
),
// elevated button
customButton(
"Login",
() {
signIn();
},
),
SizedBox(
height: 20.h,
),
Wrap(
children: [
Text(
"Don't have an account?",
style: TextStyle(
fontSize: 13.sp,
fontWeight: FontWeight.w600,
color: Color(0xFFBBBBBB),
),
),
GestureDetector(
child: Text(
" Register",
style: TextStyle(
fontSize: 13.sp,
fontWeight: FontWeight.w600,
color: AppColors.deep_orange,
),
),
onTap: () {
Navigator.pushReplacement(
context,
CupertinoPageRoute(
builder: (context) =>
RegistrationScreen()));
},
)
],
)
],
),
),
),
),
),
],
),
),
);
}
}