Compare commits
2 Commits
42699a406e
...
212f5726db
| Author | SHA1 | Date | |
|---|---|---|---|
| 212f5726db | |||
| 650982b8bb |
@ -1,11 +1,145 @@
|
|||||||
|
import 'package:appwrite/models.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_template_getx_provider/services/appwrite_service.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
|
||||||
class LoginController extends GetxController {
|
class LoginController extends GetxController {
|
||||||
final szRemark = 'You are on Login Page....'.obs;
|
final szHeaderLogin = 'Login Page'.obs;
|
||||||
|
final szHeaderSignin = 'Signin Page'.obs;
|
||||||
|
final loginFormKey = GlobalKey<FormState>();
|
||||||
|
final isLoginScreen = true.obs;
|
||||||
|
late TextEditingController emailController;
|
||||||
|
late TextEditingController passwordController;
|
||||||
|
late TextEditingController nameController;
|
||||||
|
late AppWriteProvider appWriteProvider;
|
||||||
|
Session? _session;
|
||||||
|
User? _signedUser;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onInit() {
|
||||||
|
super.onInit();
|
||||||
|
appWriteProvider = AppWriteProvider();
|
||||||
|
emailController = TextEditingController();
|
||||||
|
passwordController = TextEditingController();
|
||||||
|
nameController = TextEditingController();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onReady() {}
|
void onReady() {}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onClose() {}
|
void onClose() {
|
||||||
|
emailController.dispose();
|
||||||
|
passwordController.dispose();
|
||||||
|
nameController.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
switchScreen() {
|
||||||
|
_clearFields();
|
||||||
|
isLoginScreen.value = !isLoginScreen.value;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _clearFields() {
|
||||||
|
emailController.clear();
|
||||||
|
passwordController.clear();
|
||||||
|
nameController.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void logOrSignIn() async {
|
||||||
|
_checkInputValueOfFormFields();
|
||||||
|
loginFormKey.currentState?.save();
|
||||||
|
if (isLoginScreen.value) {
|
||||||
|
// Perform login action
|
||||||
|
String email = emailController.text;
|
||||||
|
String password = passwordController.text;
|
||||||
|
// Add your login logic here
|
||||||
|
print('Logging in with Email: $email, Password: $password');
|
||||||
|
_session = await appWriteProvider.login({
|
||||||
|
'email': email,
|
||||||
|
'password': password,
|
||||||
|
});
|
||||||
|
if (_session!.current) {
|
||||||
|
Get.snackbar(
|
||||||
|
'Login Successful',
|
||||||
|
'You have been logged in successfully.',
|
||||||
|
snackPosition: SnackPosition.BOTTOM,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
Get.snackbar(
|
||||||
|
'Login Failed',
|
||||||
|
'Invalid email or password.',
|
||||||
|
snackPosition: SnackPosition.BOTTOM,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Perform signin action
|
||||||
|
String name = nameController.text;
|
||||||
|
String email = emailController.text;
|
||||||
|
String password = passwordController.text;
|
||||||
|
// Add your signin logic here
|
||||||
|
print('Signing in with Name: $name, Email: $email, Password: $password');
|
||||||
|
_signedUser = await appWriteProvider.signup({
|
||||||
|
'email': email,
|
||||||
|
'password': password,
|
||||||
|
'name': name,
|
||||||
|
});
|
||||||
|
if (_signedUser != null) {
|
||||||
|
Get.snackbar(
|
||||||
|
'Signin Successful',
|
||||||
|
'Your account has been created successfully.',
|
||||||
|
snackPosition: SnackPosition.BOTTOM,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
Get.snackbar(
|
||||||
|
'Signin Failed',
|
||||||
|
'An error occurred during account creation.',
|
||||||
|
snackPosition: SnackPosition.BOTTOM,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_clearFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _checkInputValueOfFormFields() {
|
||||||
|
bool isValid = true;
|
||||||
|
if (!isLoginScreen.value) {
|
||||||
|
if (nameController.text == '') {
|
||||||
|
isValid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (emailController.text == '') {
|
||||||
|
isValid = false;
|
||||||
|
}
|
||||||
|
if (passwordController.text.isEmpty) {
|
||||||
|
isValid = false;
|
||||||
|
}
|
||||||
|
if (isValid == false) {
|
||||||
|
Get.snackbar(
|
||||||
|
'Invalid Input',
|
||||||
|
'Please fill in all required(*) fields.',
|
||||||
|
snackPosition: SnackPosition.BOTTOM,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return isValid;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> logout() async {
|
||||||
|
bool isLoggedOut = false;
|
||||||
|
isLoggedOut = await appWriteProvider.logout();
|
||||||
|
if (isLoggedOut) {
|
||||||
|
Get.snackbar(
|
||||||
|
'Logout Successful',
|
||||||
|
'You have been logged out successfully.',
|
||||||
|
snackPosition: SnackPosition.BOTTOM,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
Get.snackbar(
|
||||||
|
'Logout Failed',
|
||||||
|
'An error occurred during logout.',
|
||||||
|
snackPosition: SnackPosition.BOTTOM,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return isLoggedOut;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,150 @@ class LoginPage extends GetView<LoginController> {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
var loginCtrl = controller;
|
var loginCtrl = controller;
|
||||||
return SafeArea(
|
return SafeArea(
|
||||||
child: Scaffold(body: Center(child: Text(loginCtrl.szRemark.value))),
|
child: Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: SingleChildScrollView(
|
||||||
|
child: Container(
|
||||||
|
width: Get.width * 0.6,
|
||||||
|
padding: const EdgeInsets.all(40.0),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
borderRadius: BorderRadius.circular(10),
|
||||||
|
color: const Color.fromARGB(64, 189, 189, 189),
|
||||||
|
),
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
Container(
|
||||||
|
padding: const EdgeInsets.all(20),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.white.withAlpha(150),
|
||||||
|
borderRadius: BorderRadius.circular(100),
|
||||||
|
),
|
||||||
|
child: Obx(
|
||||||
|
() => loginCtrl.isLoginScreen.value
|
||||||
|
? IconButton(
|
||||||
|
onPressed: () => loginCtrl.logout(),
|
||||||
|
icon: Icon(Icons.login, size: 80),
|
||||||
|
color: Colors.pink.shade700,
|
||||||
|
)
|
||||||
|
: IconButton(
|
||||||
|
onPressed: () => loginCtrl.logout(),
|
||||||
|
icon: Icon(Icons.app_registration, size: 80),
|
||||||
|
color: Colors.pink.shade700,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 80),
|
||||||
|
Obx(
|
||||||
|
() => loginCtrl.isLoginScreen.value
|
||||||
|
? Text(
|
||||||
|
loginCtrl.szHeaderLogin.value,
|
||||||
|
style: Theme.of(context).textTheme.headlineMedium,
|
||||||
|
)
|
||||||
|
: Text(
|
||||||
|
loginCtrl.szHeaderSignin.value,
|
||||||
|
style: Theme.of(context).textTheme.headlineMedium,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 40),
|
||||||
|
Form(
|
||||||
|
key: loginCtrl.loginFormKey,
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
Obx(
|
||||||
|
() => loginCtrl.isLoginScreen.value
|
||||||
|
? const SizedBox.shrink()
|
||||||
|
: _nameFormField(loginCtrl),
|
||||||
|
),
|
||||||
|
SizedBox(height: 20),
|
||||||
|
_emailFormField(loginCtrl),
|
||||||
|
SizedBox(height: 20),
|
||||||
|
_passwdFormField(loginCtrl),
|
||||||
|
SizedBox(height: 40),
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
// Handle login or signin action
|
||||||
|
loginCtrl.logOrSignIn();
|
||||||
|
},
|
||||||
|
child: Obx(
|
||||||
|
() => loginCtrl.isLoginScreen.value
|
||||||
|
? const Text('Login')
|
||||||
|
: const Text('Signin'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 10),
|
||||||
|
Obx(
|
||||||
|
() => loginCtrl.isLoginScreen.value
|
||||||
|
? TextButton(
|
||||||
|
child: Text(
|
||||||
|
'To Signin',
|
||||||
|
style: TextStyle(color: Colors.blue.shade500),
|
||||||
|
),
|
||||||
|
onPressed: () {
|
||||||
|
loginCtrl.switchScreen();
|
||||||
|
},
|
||||||
|
)
|
||||||
|
: TextButton(
|
||||||
|
child: Text(
|
||||||
|
'To Login',
|
||||||
|
style: TextStyle(color: Colors.blue.shade500),
|
||||||
|
),
|
||||||
|
onPressed: () {
|
||||||
|
loginCtrl.switchScreen();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Email Form Field
|
||||||
|
TextFormField _emailFormField(LoginController loginCtrl) {
|
||||||
|
return TextFormField(
|
||||||
|
keyboardType: TextInputType.emailAddress,
|
||||||
|
controller: loginCtrl.emailController,
|
||||||
|
decoration: _formFieldDecoration('Email*'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Password Form Field
|
||||||
|
TextFormField _passwdFormField(LoginController loginCtrl) {
|
||||||
|
return TextFormField(
|
||||||
|
keyboardType: TextInputType.visiblePassword,
|
||||||
|
controller: loginCtrl.passwordController,
|
||||||
|
decoration: _formFieldDecoration('Password*'),
|
||||||
|
obscureText: true,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name Form Field
|
||||||
|
TextFormField _nameFormField(LoginController loginCtrl) {
|
||||||
|
return TextFormField(
|
||||||
|
keyboardType: TextInputType.name,
|
||||||
|
controller: loginCtrl.nameController,
|
||||||
|
decoration: _formFieldDecoration('Name*'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Form Field Decoration
|
||||||
|
InputDecoration _formFieldDecoration(String labelText) {
|
||||||
|
return InputDecoration(
|
||||||
|
labelText: labelText,
|
||||||
|
enabledBorder: OutlineInputBorder(
|
||||||
|
borderSide: BorderSide(color: Colors.pink.shade700),
|
||||||
|
borderRadius: BorderRadius.circular(5.0),
|
||||||
|
),
|
||||||
|
focusedBorder: OutlineInputBorder(
|
||||||
|
borderSide: BorderSide(color: Colors.pink.shade400),
|
||||||
|
borderRadius: BorderRadius.circular(5.0),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user