294 lines
13 KiB
Dart
294 lines
13 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:monitoringlistrik/adminhome.dart';
|
|
import 'package:monitoringlistrik/token.dart';
|
|
|
|
class MonitoringScreen extends StatefulWidget {
|
|
const MonitoringScreen({
|
|
Key? key,
|
|
required this.token,
|
|
required this.name,
|
|
required this.deadline,
|
|
}) : super(key: key);
|
|
|
|
final String token;
|
|
final String name;
|
|
final Timestamp deadline;
|
|
|
|
@override
|
|
_MonitoringScreenState createState() => _MonitoringScreenState();
|
|
}
|
|
|
|
class _MonitoringScreenState extends State<MonitoringScreen> {
|
|
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
|
bool isLoading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
}
|
|
|
|
Future<void> updateIsValue(String value) async {
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
|
|
try {
|
|
QuerySnapshot querySnapshot = await _firestore
|
|
.collection('isvalue')
|
|
.where('token', isEqualTo: widget.token)
|
|
.get();
|
|
|
|
if (querySnapshot.docs.isNotEmpty) {
|
|
String docId = querySnapshot.docs.first.id;
|
|
await _firestore.collection('isvalue').doc(docId).update({
|
|
'isvalue': value,
|
|
});
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Data berhasil diperbarui'),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
);
|
|
} else {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Data tidak ditemukan'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Gagal memperbarui data: $e'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
} finally {
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
Widget _buildMonitoringTile(String value, String unit, String label) {
|
|
return Column(
|
|
children: <Widget>[
|
|
Container(
|
|
color: Colors.grey,
|
|
width: 150,
|
|
height: 100,
|
|
padding: EdgeInsets.all(16.0),
|
|
child: Center(
|
|
child: Column(
|
|
children: [
|
|
Text(
|
|
value,
|
|
style: TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
Text(
|
|
unit,
|
|
style: TextStyle(
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
SizedBox(height: 7),
|
|
Text(label),
|
|
SizedBox(height: 24),
|
|
],
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Center(
|
|
child: StreamBuilder<DocumentSnapshot>(
|
|
stream: _firestore.collection('monitoring').doc('power').snapshots(),
|
|
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot1) {
|
|
return StreamBuilder<DocumentSnapshot>(
|
|
stream: _firestore.collection('monitoring').doc('current').snapshots(),
|
|
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot2) {
|
|
return StreamBuilder<DocumentSnapshot>(
|
|
stream: _firestore.collection('monitoring').doc('energy').snapshots(),
|
|
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot3) {
|
|
return StreamBuilder<DocumentSnapshot>(
|
|
stream: _firestore.collection('monitoring').doc('voltage').snapshots(),
|
|
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot4) {
|
|
return StreamBuilder<DocumentSnapshot>(
|
|
stream: _firestore.collection('monitoring').doc('saldo').snapshots(),
|
|
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot5) {
|
|
return StreamBuilder<DocumentSnapshot>(
|
|
stream: _firestore.collection('monitoring').doc('biaya').snapshots(),
|
|
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot6) {
|
|
if (snapshot1.connectionState == ConnectionState.waiting ||
|
|
snapshot2.connectionState == ConnectionState.waiting ||
|
|
snapshot3.connectionState == ConnectionState.waiting ||
|
|
snapshot4.connectionState == ConnectionState.waiting ||
|
|
snapshot5.connectionState == ConnectionState.waiting ||
|
|
snapshot6.connectionState == ConnectionState.waiting) {
|
|
return Center(
|
|
child: CircularProgressIndicator(),
|
|
);
|
|
}
|
|
|
|
if (!snapshot1.hasData || !snapshot1.data!.exists ||
|
|
!snapshot2.hasData || !snapshot2.data!.exists ||
|
|
!snapshot3.hasData || !snapshot3.data!.exists ||
|
|
!snapshot4.hasData || !snapshot4.data!.exists ||
|
|
!snapshot5.hasData || !snapshot5.data!.exists ||
|
|
!snapshot6.hasData || !snapshot6.data!.exists) {
|
|
return Center(
|
|
child: Text('Data tidak ditemukan'),
|
|
);
|
|
}
|
|
|
|
var documentData1 = snapshot1.data!.data() as Map<String, dynamic>?;
|
|
var documentData2 = snapshot2.data!.data() as Map<String, dynamic>?;
|
|
var documentData3 = snapshot3.data!.data() as Map<String, dynamic>?;
|
|
var documentData4 = snapshot4.data!.data() as Map<String, dynamic>?;
|
|
var documentData5 = snapshot5.data!.data() as Map<String, dynamic>?;
|
|
var documentData6 = snapshot6.data!.data() as Map<String, dynamic>?;
|
|
|
|
if (documentData1 == null || documentData2 == null || documentData3 == null || documentData4 == null || documentData5 == null || documentData6 == null) {
|
|
return Center(
|
|
child: Text('Data tidak ditemukan'),
|
|
);
|
|
}
|
|
|
|
return Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Container(
|
|
padding: EdgeInsets.all(16.0),
|
|
decoration: BoxDecoration(
|
|
color: const Color.fromARGB(255, 227, 227, 227),
|
|
borderRadius: BorderRadius.all(Radius.circular(30)),
|
|
),
|
|
child: Text(
|
|
'MONITORING',
|
|
style: TextStyle(fontSize: 24),
|
|
),
|
|
),
|
|
Spacer(),
|
|
Container(
|
|
padding: EdgeInsets.all(16.0),
|
|
decoration: BoxDecoration(
|
|
color: widget.deadline.toDate().isAfter(DateTime.now())
|
|
? Colors.green
|
|
: Colors.red,
|
|
borderRadius: BorderRadius.all(Radius.circular(10)),
|
|
),
|
|
child: Text(
|
|
widget.name,
|
|
style: TextStyle(fontSize: 24),
|
|
),
|
|
),
|
|
Spacer(),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => TokenScreen(
|
|
token: widget.token,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
child: Text(
|
|
'Perpanjang Token',
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
style: ElevatedButton.styleFrom(
|
|
primary: Colors.blue,
|
|
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
|
|
),
|
|
),
|
|
Spacer(),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
children: <Widget>[
|
|
_buildMonitoringTile(
|
|
documentData4['voltage'].toString(),
|
|
'V',
|
|
'Voltage'),
|
|
_buildMonitoringTile(
|
|
documentData2['current'].toString(),
|
|
'A',
|
|
'Current'),
|
|
],
|
|
),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
children: <Widget>[
|
|
_buildMonitoringTile(
|
|
documentData1['power'].toString(),
|
|
'W',
|
|
'Power'),
|
|
_buildMonitoringTile(
|
|
documentData3['energy'].toString(),
|
|
'kWh',
|
|
'Energy'),
|
|
],
|
|
),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
children: <Widget>[
|
|
_buildMonitoringTile(
|
|
documentData5['saldo'].toString(),
|
|
'Rp',
|
|
'Sisa Saldo'),
|
|
_buildMonitoringTile(
|
|
documentData6['biaya'].toString(),
|
|
'Rp',
|
|
'Biaya'),
|
|
],
|
|
),
|
|
SizedBox(height: 10),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => AdminHomeScreen()),
|
|
);
|
|
},
|
|
child: Text(
|
|
'Kembali',
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
style: ElevatedButton.styleFrom(
|
|
primary: Colors.blue,
|
|
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
|
|
),
|
|
),
|
|
SizedBox(height: 20),
|
|
Spacer(),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|