added status to sharedprefs, refactored buttons and added button functionality
All checks were successful
Build Flutter Web and Docker Image for Local Registry / Build Flutter Web App (push) Successful in 3m6s
All checks were successful
Build Flutter Web and Docker Image for Local Registry / Build Flutter Web App (push) Successful in 3m6s
This commit is contained in:
parent
89c1134a85
commit
ed345b1fa9
@ -50,4 +50,14 @@ class SharedPreferencesProvider extends ChangeNotifier {
|
|||||||
await prefs.setString('userLogo', image!);
|
await prefs.setString('userLogo', image!);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New methods to get and set the current status
|
||||||
|
String getCurrentStatus() {
|
||||||
|
return prefs.getString('currentStatus') ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> setCurrentStatus(String status) async {
|
||||||
|
await prefs.setString('currentStatus', status);
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,18 +38,53 @@ class StatusPageState extends State<StatusPage> {
|
|||||||
return messages.where((message) => message['Status'] == status).toList();
|
return messages.where((message) => message['Status'] == status).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _sendStatus(String id, String name, String? image, String status) {
|
void _sendStatus(String id, String name, String? image, String status) async {
|
||||||
|
final prefsProvider =
|
||||||
|
Provider.of<SharedPreferencesProvider>(context, listen: false);
|
||||||
|
|
||||||
|
if (!mounted) return;
|
||||||
|
|
||||||
|
// Check if the status is already active; if so, clear it, otherwise set it
|
||||||
|
final isStatusActive = prefsProvider.getCurrentStatus() == status;
|
||||||
|
final newStatus = isStatusActive ? '' : status;
|
||||||
|
|
||||||
|
// Update the status in SharedPreferences
|
||||||
|
await prefsProvider.setCurrentStatus(newStatus);
|
||||||
|
|
||||||
|
// Send the message to the WebSocket
|
||||||
final message = jsonEncode({
|
final message = jsonEncode({
|
||||||
'Id': id,
|
'Id': id,
|
||||||
'Name': name,
|
'Name': name,
|
||||||
'Image': image,
|
'Image': image,
|
||||||
'Status': status,
|
'Status': newStatus.isEmpty ? 'expired' : newStatus,
|
||||||
'Timestamp': DateTime.now().toIso8601String(),
|
'Timestamp': DateTime.now().toIso8601String(),
|
||||||
});
|
});
|
||||||
channel.sink.add(message);
|
channel.sink.add(message);
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
SnackBar(content: Text('Status "$status" sent!')),
|
if (mounted) {
|
||||||
);
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
'Status "${newStatus.isEmpty ? 'cleared' : newStatus}" sent!')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _handleIncomingMessage(Map<String, dynamic> message) async {
|
||||||
|
final prefsProvider =
|
||||||
|
Provider.of<SharedPreferencesProvider>(context, listen: false);
|
||||||
|
|
||||||
|
final status = message['Status'];
|
||||||
|
final incomingId = message['Id'];
|
||||||
|
|
||||||
|
messages.removeWhere((msg) => msg['Id'] == incomingId);
|
||||||
|
|
||||||
|
// Check if the message status is "expired" and update SharedPreferences
|
||||||
|
if (status == 'expired') {
|
||||||
|
await prefsProvider.setCurrentStatus('');
|
||||||
|
} else {
|
||||||
|
messages.add(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildMessageItem(Map<String, dynamic> message) {
|
Widget _buildMessageItem(Map<String, dynamic> message) {
|
||||||
@ -94,6 +129,20 @@ class StatusPageState extends State<StatusPage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to get the background color based on the current status
|
||||||
|
Color getButtonColor(String buttonStatus) {
|
||||||
|
final prefsProvider = Provider.of<SharedPreferencesProvider>(context);
|
||||||
|
final currentStatus = prefsProvider.getCurrentStatus();
|
||||||
|
return currentStatus == buttonStatus ? Colors.blueAccent : Colors.grey;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to get the text color based on the current status
|
||||||
|
Color getButtonTextColor(String buttonStatus) {
|
||||||
|
final prefsProvider = Provider.of<SharedPreferencesProvider>(context);
|
||||||
|
final currentStatus = prefsProvider.getCurrentStatus();
|
||||||
|
return currentStatus == buttonStatus ? Colors.white : Colors.black;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final prefsProvider = Provider.of<SharedPreferencesProvider>(context);
|
final prefsProvider = Provider.of<SharedPreferencesProvider>(context);
|
||||||
@ -115,13 +164,7 @@ class StatusPageState extends State<StatusPage> {
|
|||||||
if (snapshot.hasData) {
|
if (snapshot.hasData) {
|
||||||
final newMessage =
|
final newMessage =
|
||||||
jsonDecode(snapshot.data as String) as Map<String, dynamic>;
|
jsonDecode(snapshot.data as String) as Map<String, dynamic>;
|
||||||
|
_handleIncomingMessage(newMessage);
|
||||||
final incomingId = newMessage['Id'];
|
|
||||||
messages.removeWhere((msg) => msg['Id'] == incomingId);
|
|
||||||
if (newMessage['Status'] != 'expired' &&
|
|
||||||
newMessage['Status'] != "is leaving") {
|
|
||||||
messages.add(newMessage);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final onTheWayMessages = _getMessagesByStatus('is on the way');
|
final onTheWayMessages = _getMessagesByStatus('is on the way');
|
||||||
@ -181,19 +224,28 @@ class StatusPageState extends State<StatusPage> {
|
|||||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||||
children: [
|
children: [
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: getButtonColor('is on the way'),
|
||||||
|
),
|
||||||
onPressed: () => _sendStatus(
|
onPressed: () => _sendStatus(
|
||||||
userId, userName, userLogo, 'is on the way'),
|
userId, userName, userLogo, 'is on the way'),
|
||||||
child: const Text('On the Way'),
|
child: Text(
|
||||||
|
'On the Way',
|
||||||
|
style: TextStyle(
|
||||||
|
color: getButtonTextColor('is on the way')),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: getButtonColor('has arrived'),
|
||||||
|
),
|
||||||
onPressed: () => _sendStatus(
|
onPressed: () => _sendStatus(
|
||||||
userId, userName, userLogo, 'has arrived'),
|
userId, userName, userLogo, 'has arrived'),
|
||||||
child: const Text('Arrived'),
|
child: Text(
|
||||||
),
|
'Arrived',
|
||||||
ElevatedButton(
|
style:
|
||||||
onPressed: () =>
|
TextStyle(color: getButtonTextColor('has arrived')),
|
||||||
_sendStatus(userId, userName, userLogo, 'is leaving'),
|
),
|
||||||
child: const Text('Leaving'),
|
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.edit, color: Colors.blueAccent),
|
icon: const Icon(Icons.edit, color: Colors.blueAccent),
|
||||||
|
Loading…
Reference in New Issue
Block a user