import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class LoginScreen extends StatefulWidget {
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
bool isLoading = false;
String? errorMessage;
void signIn() async {
setState(() {
isLoading = true;
errorMessage = null;
});
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
// On success, you might navigate to another screen
// For this example, just clear inputs
emailController.clear();
passwordController.clear();
} on FirebaseAuthException catch (e) {
setState(() {
errorMessage = e.message ?? 'Sign in failed';
});
} catch (_) {
setState(() {
errorMessage = 'An unexpected error occurred';
});
} finally {
setState(() {
isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Login Screen')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: emailController,
decoration: InputDecoration(labelText: 'Email'),
keyboardType: TextInputType.emailAddress,
),
SizedBox(height: 12),
TextField(
controller: passwordController,
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
),
SizedBox(height: 24),
if (errorMessage != null) ...[
Text(errorMessage!, style: TextStyle(color: Colors.red)),
SizedBox(height: 12),
],
isLoading
? CircularProgressIndicator()
: ElevatedButton(
onPressed: signIn,
child: Text('Sign In'),
),
SizedBox(height: 24),
TextButton(
onPressed: () {
// Placeholder for navigation to Sign Up screen
},
child: Text("Don't have an account? Sign Up"),
),
],
),
),
);
}
}
This solution uses FirebaseAuth's signInWithEmailAndPassword method to authenticate users with their email and password.
We show a loading spinner while waiting for the sign-in process to complete.
If an error occurs, we catch it and display the error message below the input fields in red.
The Sign Up button currently has a placeholder for navigation logic, which you can implement later.
This approach keeps the UI responsive and informs the user about the sign-in status clearly.