181 lines
5.8 KiB
Dart
181 lines
5.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../widgets/input_field.dart';
|
|
import 'login_screen.dart';
|
|
|
|
class RegisterScreen extends StatefulWidget {
|
|
const RegisterScreen({super.key});
|
|
|
|
@override
|
|
State<RegisterScreen> createState() => _RegisterScreenState();
|
|
}
|
|
|
|
class _RegisterScreenState extends State<RegisterScreen> {
|
|
final usernameController = TextEditingController();
|
|
final emailController = TextEditingController();
|
|
final passwordController = TextEditingController();
|
|
final confirmPasswordController = TextEditingController();
|
|
|
|
Future<void> register() async {
|
|
String username = usernameController.text.trim();
|
|
String email = emailController.text.trim();
|
|
String password = passwordController.text.trim();
|
|
String confirmPassword = confirmPasswordController.text.trim();
|
|
|
|
if (username.isEmpty || email.isEmpty || password.isEmpty || confirmPassword.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('All fields are required')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(email)) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Invalid email format')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (password != confirmPassword) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Passwords do not match')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
// Check if the username or email is already taken
|
|
String? existingUser = prefs.getString(username);
|
|
String? existingEmail = prefs.getString(email);
|
|
if (existingUser != null || existingEmail != null) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Username or Email already exists. Choose another.')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Save user credentials in SharedPreferences
|
|
await prefs.setString(username, password);
|
|
await prefs.setString(email, username); // Store email linked to the username
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Registration Successful! Please Login')),
|
|
);
|
|
|
|
// Redirect to Login Screen
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const LoginScreen()),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.white,
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
/// Logo
|
|
Image.asset(
|
|
'assets/logo.png',
|
|
width: 1000,
|
|
height: 150,
|
|
),
|
|
const SizedBox(height: 40),
|
|
|
|
/// Username Field
|
|
InputField(
|
|
hintText: 'Username',
|
|
icon: Icons.person,
|
|
controller: usernameController,
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
/// Email Field
|
|
InputField(
|
|
hintText: 'Email',
|
|
icon: Icons.email,
|
|
controller: emailController,
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
/// Password Field
|
|
InputField(
|
|
hintText: 'Password',
|
|
icon: Icons.lock,
|
|
obscureText: true,
|
|
controller: passwordController,
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
/// Confirm Password Field
|
|
InputField(
|
|
hintText: 'Confirm Password',
|
|
icon: Icons.lock,
|
|
obscureText: true,
|
|
controller: confirmPasswordController,
|
|
),
|
|
const SizedBox(height: 24),
|
|
|
|
/// Register Button
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.grey[800], // Match Login Button
|
|
foregroundColor: Colors.white,
|
|
minimumSize: const Size.fromHeight(50),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
),
|
|
onPressed: register,
|
|
child: const Text('Register', style: TextStyle(fontSize: 18)),
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
/// Horizontal Line with "or" (optional, in case Google Sign-Up is added)
|
|
Row(
|
|
children: const [
|
|
Expanded(
|
|
child: Divider(color: Colors.grey, thickness: 1),
|
|
),
|
|
Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 8),
|
|
child: Text("or", style: TextStyle(color: Colors.grey, fontSize: 16)),
|
|
),
|
|
Expanded(
|
|
child: Divider(color: Colors.grey, thickness: 1),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
/// Login Navigation (Styled in Blue)
|
|
GestureDetector(
|
|
onTap: () {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const LoginScreen()),
|
|
);
|
|
},
|
|
child: const Text(
|
|
"Already have an account? Login",
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w500,
|
|
color: Colors.grey, // Styled for better visibility
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|