225 lines
6.7 KiB
Dart
225 lines
6.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
@override
|
|
State<LoginPage> createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
TextEditingController emailController = TextEditingController();
|
|
TextEditingController passwordController = TextEditingController();
|
|
|
|
final FirebaseFirestore firestore = FirebaseFirestore.instance;
|
|
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initializeNotifications();
|
|
_listenToFirebase();
|
|
}
|
|
|
|
void _initializeNotifications() {
|
|
final initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
|
|
final initializationSettings = InitializationSettings(
|
|
android: initializationSettingsAndroid,
|
|
);
|
|
flutterLocalNotificationsPlugin.initialize(initializationSettings);
|
|
}
|
|
|
|
void _listenToFirebase() {
|
|
firestore.collection('alerts').snapshots().listen((snapshot) {
|
|
for (var document in snapshot.docs) {
|
|
int flameStatus = document.get('flameStatus') ?? 0;
|
|
int smokeStatus = document.get('smokeDetected') ?? 0;
|
|
|
|
if (flameStatus == 2) {
|
|
_showNotification('Peringatan Kebakaran', 'Ada indikasi kebakaran');
|
|
}
|
|
|
|
if (smokeStatus == 2) {
|
|
_showNotification('Peringatan Asap', 'Ada indikasi terdeteksi asap');
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _showNotification(String title, String body) async {
|
|
const AndroidNotificationDetails androidPlatformChannelSpecifics =
|
|
AndroidNotificationDetails('your_channel_id', 'your_channel_name',
|
|
channelDescription: 'your_channel_description',
|
|
importance: Importance.max,
|
|
priority: Priority.high,
|
|
ticker: 'ticker');
|
|
const NotificationDetails platformChannelSpecifics =
|
|
NotificationDetails(android: androidPlatformChannelSpecifics);
|
|
await flutterLocalNotificationsPlugin.show(
|
|
0,
|
|
title,
|
|
body,
|
|
platformChannelSpecifics,
|
|
payload: 'item x',
|
|
);
|
|
}
|
|
|
|
Future<void> signInWithEmailAndPassword(BuildContext context) async {
|
|
try {
|
|
QuerySnapshot querySnapshot = await firestore.collection('satpam')
|
|
.where('email', isEqualTo: emailController.text.trim())
|
|
.where('password', isEqualTo: passwordController.text.trim())
|
|
.get();
|
|
|
|
if (querySnapshot.docs.isNotEmpty) {
|
|
Navigator.pushReplacementNamed(context, '/home_satpam');
|
|
} else {
|
|
showErrorDialog(context, "Email atau password salah.");
|
|
}
|
|
} catch (e) {
|
|
showErrorDialog(context, "Error signing in: $e");
|
|
}
|
|
}
|
|
|
|
void showErrorDialog(BuildContext context, String message) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: Text("Error"),
|
|
content: Text(message),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
child: Text("OK"),
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(32.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(
|
|
"SELAMAT",
|
|
style: TextStyle(fontSize: 40, fontWeight: FontWeight.bold, color: Colors.black),
|
|
),
|
|
Text(
|
|
"DATANG",
|
|
style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold, color: Colors.black),
|
|
),
|
|
SizedBox(height: 40),
|
|
_inputField("Email", emailController),
|
|
SizedBox(height: 20),
|
|
_inputField("Password", passwordController, isPassword: true),
|
|
SizedBox(height: 40),
|
|
_loginBtn(context),
|
|
SizedBox(height: 20),
|
|
_extraText(context),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _inputField(String hintText, TextEditingController controller, {bool isPassword = false}) {
|
|
return TextField(
|
|
controller: controller,
|
|
decoration: InputDecoration(
|
|
hintText: hintText,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(10.0),
|
|
),
|
|
filled: true,
|
|
fillColor: Colors.white,
|
|
),
|
|
obscureText: isPassword,
|
|
);
|
|
}
|
|
|
|
Widget _loginBtn(BuildContext context) {
|
|
return ElevatedButton(
|
|
onPressed: () {
|
|
signInWithEmailAndPassword(context);
|
|
},
|
|
child: SizedBox(
|
|
width: double.infinity,
|
|
child: Text(
|
|
"Login",
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(fontSize: 20, color: Colors.black),
|
|
),
|
|
),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.white,
|
|
padding: EdgeInsets.symmetric(vertical: 15),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10.0),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _extraText(BuildContext context) {
|
|
return GestureDetector(
|
|
onTap: () {
|
|
_showAdminVerificationForm(context);
|
|
},
|
|
child: Text(
|
|
"Apakah Anda admin?",
|
|
style: TextStyle(fontSize: 16, color: Colors.blue),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showAdminVerificationForm(BuildContext context) {
|
|
String userInput = '';
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: Text("Verifikasi Admin"),
|
|
content: TextField(
|
|
onChanged: (value) {
|
|
userInput = value;
|
|
},
|
|
decoration: InputDecoration(hintText: "Masukkan teks verifikasi"),
|
|
),
|
|
actions: <Widget>[
|
|
TextButton(
|
|
child: Text("Batal"),
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
),
|
|
TextButton(
|
|
child: Text("Verifikasi"),
|
|
onPressed: () {
|
|
if (userInput == "admin123") {
|
|
Navigator.pushReplacementNamed(context, '/home_admin');
|
|
} else {
|
|
Navigator.of(context).pop();
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text('Teks verifikasi salah.'),
|
|
));
|
|
}
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|