25-8-9-1
This commit is contained in:
39
lib/controller/functions/battery_status.dart
Normal file
39
lib/controller/functions/battery_status.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'package:battery_plus/battery_plus.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class BatteryNotifier {
|
||||
static final Battery _battery = Battery();
|
||||
static int? _lastNotifiedLevel;
|
||||
|
||||
static Future<void> checkBatteryAndNotify() async {
|
||||
try {
|
||||
final int batteryLevel = await _battery.batteryLevel;
|
||||
|
||||
// ✅ لا تكرر الإشعار إذا الفرق قليل
|
||||
if (_lastNotifiedLevel != null &&
|
||||
(batteryLevel >= _lastNotifiedLevel! - 2)) return;
|
||||
|
||||
if (batteryLevel <= 30) {
|
||||
Color backgroundColor = Colors.yellow;
|
||||
if (batteryLevel <= 20) {
|
||||
backgroundColor = Colors.red;
|
||||
}
|
||||
|
||||
Get.snackbar(
|
||||
"⚠️ تنبيه البطارية", // العنوان
|
||||
"مستوى البطارية: $batteryLevel٪", // النص
|
||||
snackPosition: SnackPosition.TOP,
|
||||
backgroundColor: backgroundColor,
|
||||
colorText: Colors.white,
|
||||
duration: const Duration(seconds: 10), // مدة الظهور
|
||||
margin: const EdgeInsets.all(10),
|
||||
);
|
||||
|
||||
_lastNotifiedLevel = batteryLevel;
|
||||
}
|
||||
} catch (e) {
|
||||
print('Battery check error: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user