40 lines
1.2 KiB
Dart
40 lines
1.2 KiB
Dart
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');
|
|
}
|
|
}
|
|
}
|