318 lines
12 KiB
Dart
318 lines
12 KiB
Dart
import 'package:e_commerce/const/AppColors.dart';
|
|
import 'package:e_commerce/ui/login_screen.dart';
|
|
import 'package:e_commerce/ui/user_form.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 RegistrationScreen extends StatefulWidget {
|
|
@override
|
|
_RegistrationScreenState createState() => _RegistrationScreenState();
|
|
}
|
|
|
|
class _RegistrationScreenState extends State<RegistrationScreen> {
|
|
TextEditingController _emailController = TextEditingController();
|
|
TextEditingController _passwordController = TextEditingController();
|
|
bool _obscureText = true;
|
|
|
|
signUp() async {
|
|
try {
|
|
// Buat akun baru dengan Firebase Authentication
|
|
UserCredential userCredential = await FirebaseAuth.instance
|
|
.createUserWithEmailAndPassword(
|
|
email: _emailController.text, password: _passwordController.text);
|
|
|
|
var authCredential = userCredential.user;
|
|
if (authCredential != null && authCredential.uid.isNotEmpty) {
|
|
// Jika berhasil, arahkan ke formulir pengguna
|
|
Navigator.push(context, CupertinoPageRoute(builder: (_) => UserForm()));
|
|
} else {
|
|
Fluttertoast.showToast(
|
|
msg: "Something is wrong",
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.TOP,
|
|
backgroundColor: AppColors.deep_orange,
|
|
textColor: Colors.white,
|
|
fontSize: 16.0,
|
|
);
|
|
}
|
|
} on FirebaseAuthException catch (e) {
|
|
// Tangani kesalahan yang umum terjadi saat mendaftar
|
|
if (e.code == 'weak-password') {
|
|
Fluttertoast.showToast(
|
|
msg: "The password provided is too weak.",
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.TOP,
|
|
backgroundColor: AppColors.deep_orange,
|
|
textColor: Colors.white,
|
|
fontSize: 16.0,
|
|
);
|
|
} else if (e.code == 'email-already-in-use') {
|
|
Fluttertoast.showToast(
|
|
msg: "The account already exists for that email.",
|
|
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) {
|
|
// Tangani kesalahan lain
|
|
Fluttertoast.showToast(
|
|
msg: "Unexpected error occurred: $e",
|
|
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(
|
|
"Register",
|
|
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
|
|
SizedBox(
|
|
width: 1.sw,
|
|
height: 56.h,
|
|
child: ElevatedButton(
|
|
onPressed: () {
|
|
signUp();
|
|
},
|
|
child: Text(
|
|
"Continue",
|
|
style: TextStyle(
|
|
color: Colors.white, fontSize: 18.sp),
|
|
),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: AppColors.deep_orange,
|
|
elevation: 3,
|
|
),
|
|
),
|
|
),
|
|
SizedBox(
|
|
height: 20.h,
|
|
),
|
|
Wrap(
|
|
children: [
|
|
Text(
|
|
"Have an account?",
|
|
style: TextStyle(
|
|
fontSize: 13.sp,
|
|
fontWeight: FontWeight.w600,
|
|
color: Color(0xFFBBBBBB),
|
|
),
|
|
),
|
|
GestureDetector(
|
|
child: Text(
|
|
" Login",
|
|
style: TextStyle(
|
|
fontSize: 13.sp,
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.deep_orange,
|
|
),
|
|
),
|
|
onTap: () {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
CupertinoPageRoute(
|
|
builder: (context) => LoginScreen()));
|
|
},
|
|
)
|
|
],
|
|
)
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|