TKK_E32222626/login_page.dart

235 lines
8.3 KiB
Dart

import 'package:app_sensor/forgotpassword_page.dart';
import 'package:app_sensor/monitoring_page.dart';
import 'package:app_sensor/signup_page.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:lottie/lottie.dart';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final FirebaseAuth _auth = FirebaseAuth.instance;
bool _obscurePassword = true;
bool _isLoading = false; // loading state
void _login() async {
String email = emailController.text.trim();
String password = passwordController.text.trim();
if (email.isEmpty || password.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Email dan Password tidak boleh kosong!'),
backgroundColor: Colors.red,
),
);
return;
}
setState(() {
_isLoading = true;
});
try {
await _auth.signInWithEmailAndPassword(
email: email,
password: password,
);
setState(() {
_isLoading = false;
});
// Menggunakan push agar halaman login tetap bisa dikunjungi kembali
Navigator.push(
context,
MaterialPageRoute(builder: (_) => MonitoringPage()),
);
} on FirebaseAuthException catch (e) {
setState(() {
_isLoading = false;
});
String message = 'Terjadi kesalahan';
if (e.code == 'user-not-found') {
message = 'Pengguna tidak ditemukan';
} else if (e.code == 'wrong-password') {
message = 'Password salah';
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
backgroundColor: Colors.red,
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [
Color(0xFFB3E5FC),
Color(0xFFE1F5FE),
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: Center(
child: SingleChildScrollView(
padding: EdgeInsets.all(24),
child: Column(
children: [
Lottie.asset(
'assets/animations/home.json',
width: 140,
height: 140,
),
SizedBox(height: 10),
Text(
'RumahOto',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.blueGrey[800],
),
),
SizedBox(height: 30),
Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.95),
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: Colors.blueGrey.withOpacity(0.1),
blurRadius: 20,
offset: Offset(0, 10),
),
],
),
child: Column(
children: [
TextField(
controller: emailController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
prefixIcon: Icon(Icons.email, color: Colors.blueGrey),
labelText: 'Email',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
SizedBox(height: 15),
TextField(
controller: passwordController,
obscureText: _obscurePassword,
decoration: InputDecoration(
prefixIcon: Icon(Icons.lock, color: Colors.blueGrey),
labelText: 'Password',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
suffixIcon: IconButton(
icon: Icon(
_obscurePassword ? Icons.visibility_off : Icons.visibility,
color: Colors.blueGrey,
),
onPressed: () {
setState(() {
_obscurePassword = !_obscurePassword;
});
},
),
),
),
SizedBox(height: 10),
Align(
alignment: Alignment.centerRight,
child: TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => ForgotPasswordPage()),
);
},
child: Text(
"Lupa Password?",
style: TextStyle(color: Colors.blueGrey),
),
),
),
SizedBox(height: 10),
_isLoading
? CircularProgressIndicator(
color: Colors.blueGrey,
)
: ElevatedButton(
onPressed: _login,
style: ElevatedButton.styleFrom(
backgroundColor: Color(0xFF81D4FA),
minimumSize: Size(double.infinity, 48),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Text(
'LOGIN',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.white,
letterSpacing: 1.1,
),
),
),
],
),
),
SizedBox(height: 20),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => SignUpPage()),
);
},
child: RichText(
text: TextSpan(
text: "Belum punya akun? ",
style: TextStyle(color: Colors.blueGrey[700]),
children: [
TextSpan(
text: 'Daftar Sekarang',
style: TextStyle(
decoration: TextDecoration.underline,
fontWeight: FontWeight.bold,
),
),
],
),
),
),
],
),
),
),
),
);
}
}