233 lines
7.6 KiB
Dart
233 lines
7.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:google_sign_in/google_sign_in.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../widgets/input_field.dart';
|
|
import 'home_screen.dart';
|
|
import 'register_screen.dart';
|
|
|
|
class LoginScreen extends StatefulWidget {
|
|
const LoginScreen({super.key});
|
|
|
|
@override
|
|
State<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
|
final usernameController = TextEditingController();
|
|
final passwordController = TextEditingController();
|
|
bool rememberMe = false;
|
|
|
|
final GoogleSignIn _googleSignIn = GoogleSignIn(
|
|
clientId: 'YOUR_WEB_CLIENT_ID_HERE',
|
|
scopes: ['email'],
|
|
);
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_checkAutoLogin();
|
|
}
|
|
|
|
Future<void> _checkAutoLogin() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final isRemembered = prefs.getBool('rememberMe') ?? false;
|
|
final googleUser = prefs.getString('googleUser');
|
|
|
|
if (isRemembered) {
|
|
final savedUsername = prefs.getString('loginUsername') ?? prefs.getString('username');
|
|
final savedPassword = prefs.getString('password');
|
|
if (savedUsername != null && savedPassword != null) {
|
|
usernameController.text = savedUsername;
|
|
passwordController.text = savedPassword;
|
|
setState(() => rememberMe = true);
|
|
}
|
|
} else if (googleUser != null) {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const HomeScreen()),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> _saveLoginDetails(String username, String password) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString('loginUsername', username); // Ensures login uses latest username
|
|
await prefs.setString('password', password);
|
|
|
|
if (rememberMe) {
|
|
await prefs.setBool('rememberMe', true);
|
|
} else {
|
|
await prefs.remove('rememberMe');
|
|
}
|
|
}
|
|
|
|
|
|
void login() async {
|
|
String username = usernameController.text.trim();
|
|
String password = passwordController.text.trim();
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
String? savedUsername = prefs.getString('loginUsername') ?? prefs.getString('username'); // Get the latest username
|
|
String? savedPassword = prefs.getString('password'); // Get the saved password
|
|
|
|
if (savedUsername == username && savedPassword == password) {
|
|
await _saveLoginDetails(username, password);
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const HomeScreen()),
|
|
);
|
|
} else {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Invalid username or password')),
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
Future<void> googleLogin() async {
|
|
try {
|
|
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
|
|
if (googleUser != null) {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString('googleUser', googleUser.displayName ?? 'Google User');
|
|
await prefs.setString('googleEmail', googleUser.email);
|
|
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const HomeScreen()),
|
|
);
|
|
}
|
|
} catch (error) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Google Sign-In Failed: $error')),
|
|
);
|
|
}
|
|
}
|
|
|
|
@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: [
|
|
Image.asset(
|
|
'assets/logo.png',
|
|
width: 1000,
|
|
height: 150,
|
|
),
|
|
const SizedBox(height: 40),
|
|
InputField(
|
|
hintText: 'Username',
|
|
icon: Icons.person,
|
|
controller: usernameController,
|
|
),
|
|
const SizedBox(height: 16),
|
|
InputField(
|
|
hintText: 'Password',
|
|
icon: Icons.lock,
|
|
obscureText: true,
|
|
controller: passwordController,
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
/// Remember Me Checkbox - Centered
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Checkbox(
|
|
value: rememberMe,
|
|
onChanged: (value) {
|
|
setState(() {
|
|
rememberMe = value ?? false;
|
|
});
|
|
},
|
|
activeColor: Colors.grey[800],
|
|
),
|
|
const Text(
|
|
'Remember Me',
|
|
style: TextStyle(
|
|
color: Colors.black87,
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 24),
|
|
|
|
/// Login Button
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.grey[800],
|
|
foregroundColor: Colors.white,
|
|
minimumSize: const Size.fromHeight(50),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
),
|
|
onPressed: login,
|
|
child: const Text('Login', style: TextStyle(fontSize: 18)),
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
/// Horizontal Line with "or"
|
|
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),
|
|
|
|
/// Google Sign-In Button (Styled same as Login)
|
|
ElevatedButton.icon(
|
|
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: googleLogin,
|
|
icon: const Icon(Icons.g_mobiledata, size: 30), // Google Icon
|
|
label: const Text('Sign in with Google', style: TextStyle(fontSize: 18)),
|
|
),
|
|
const SizedBox(height: 20),
|
|
|
|
/// Register Text (Styled in Blue)
|
|
GestureDetector(
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const RegisterScreen()),
|
|
);
|
|
},
|
|
child: const Text(
|
|
"Don't have an account? Register",
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w500,
|
|
color: Colors.grey, // Better visibility
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|