native_system_kit 1.0.1
native_system_kit: ^1.0.1 copied to clipboard
A production-grade Flutter plugin for advanced native system features (Background Services, Alarms, Hardware Control, System Info).
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:native_system_kit/native_system_kit.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _status = 'Idle';
String _eventLog = '';
bool _isFlashlightOn = false;
double _brightness = 0.5;
double _volume = 0.5;
@override
void initState() {
super.initState();
_initSystemEvents();
_fetchInitialHardwareState();
}
Future<void> _fetchInitialHardwareState() async {
double b = await NativeSystemKit.instance.hardware.getBrightness();
double v = await NativeSystemKit.instance.hardware.getVolume();
setState(() {
_brightness = b;
_volume = v;
});
}
void _initSystemEvents() {
NativeSystemKit.instance.events.eventStream.listen((event) {
if (event == "android.media.VOLUME_CHANGED_ACTION") {
_updateVolume();
} else if (event == "BRIGHTNESS_CHANGED") {
_updateBrightness();
} else {
setState(() {
_eventLog = 'Event: $event\n$_eventLog';
});
}
});
}
Future<void> _updateVolume() async {
double v = await NativeSystemKit.instance.hardware.getVolume();
if (mounted) {
setState(() {
_volume = v;
});
}
}
Future<void> _updateBrightness() async {
double b = await NativeSystemKit.instance.hardware.getBrightness();
if (mounted) {
setState(() {
_brightness = b;
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Native System Kit'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildSectionTitle('Status'),
Text(_status, style: const TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 10),
_buildSectionTitle('Background Service'),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () async {
await NativeSystemKit.instance.service.startPersistentService(
title: "System Kit Service",
content: "Active and monitoring...",
);
setState(() => _status = "Service Started");
},
child: const Text('Start'),
),
ElevatedButton(
onPressed: () async {
await NativeSystemKit.instance.service.stopPersistentService();
setState(() => _status = "Service Stopped");
},
child: const Text('Stop'),
),
],
),
const SizedBox(height: 20),
_buildSectionTitle('Power Manager'),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () async {
await NativeSystemKit.instance.power.acquireWakeLock();
setState(() => _status = "WakeLock Acquired");
},
child: const Text('Acquire WakeLock'),
),
ElevatedButton(
onPressed: () async {
await NativeSystemKit.instance.power.releaseWakeLock();
setState(() => _status = "WakeLock Released");
},
child: const Text('Release WakeLock'),
),
],
),
ElevatedButton(
onPressed: () async {
bool isSaver = await NativeSystemKit.instance.power.isBatterySaverEnabled();
setState(() => _status = "Battery Saver: $isSaver");
},
child: const Text('Check Battery Saver'),
),
const SizedBox(height: 20),
_buildSectionTitle('Alarm Scheduler'),
ElevatedButton(
onPressed: () async {
try {
final trigger = DateTime.now().add(const Duration(seconds: 10));
await NativeSystemKit.instance.alarm.scheduleExact(
alarmId: "test_alarm_1",
triggerTime: trigger,
);
setState(() => _status = "Alarm Scheduled for ${trigger.toLocal()}");
} on PlatformException catch (e) {
if (e.code == "PERMISSION_DENIED") {
bool granted = await NativeSystemKit.instance.alarm.requestExactAlarmPermission();
setState(() => _status = "Requesting permission: $granted");
} else {
setState(() => _status = "Error: ${e.message}");
}
}
},
child: const Text('Schedule Alarm (+10s)'),
),
ElevatedButton(
onPressed: () async {
bool granted = await NativeSystemKit.instance.alarm.requestExactAlarmPermission();
setState(() => _status = granted ? "Permission Granted" : "Opening Settings...");
},
child: const Text('Request Alarm Permission'),
),
const SizedBox(height: 20),
_buildSectionTitle('Device Hardware'),
// Flashlight Toggle
ListTile(
title: const Text("Flashlight"),
trailing: Switch(
value: _isFlashlightOn,
onChanged: (val) async {
await NativeSystemKit.instance.hardware.setFlashlight(val);
setState(() => _isFlashlightOn = val);
},
),
),
// Brightness Slider
const Text("System Brightness"),
Slider(
value: _brightness,
onChanged: (val) async {
setState(() => _brightness = val);
try {
await NativeSystemKit.instance.hardware.setBrightness(val);
} on PlatformException catch (e) {
setState(() => _status = "Permission needed: ${e.message}");
}
},
divisions: 10,
label: _brightness.toStringAsFixed(1),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextButton(
child: const Text("Max Brightness"),
onPressed: () async {
try {
await NativeSystemKit.instance.hardware.setMaxBrightness();
setState(() => _brightness = 1.0);
} catch (e) {
// Handle permission error
}
},
),
],
),
// Volume Slider
const Text("Media Volume"),
Slider(
value: _volume,
onChanged: (val) {
setState(() => _volume = val);
NativeSystemKit.instance.hardware.setVolume(val);
},
divisions: 10,
label: _volume.toStringAsFixed(1),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextButton(
child: const Text("Mute"),
onPressed: () async {
await NativeSystemKit.instance.hardware.mute();
setState(() => _volume = 0.0);
},
),
TextButton(
child: const Text("Max Volume"),
onPressed: () async {
await NativeSystemKit.instance.hardware.setMaxVolume();
setState(() => _volume = 1.0);
},
),
],
),
const SizedBox(height: 20),
_buildSectionTitle('System Info'),
ElevatedButton(
onPressed: () async {
int totalSpace = await NativeSystemKit.instance.info.getTotalDiskSpace();
int freeSpace = await NativeSystemKit.instance.info.getFreeDiskSpace();
int totalRam = await NativeSystemKit.instance.info.getTotalRam();
bool rooted = await NativeSystemKit.instance.info.isDeviceRooted();
setState(() {
_eventLog = """
Disk: ${_formatBytes(freeSpace)} / ${_formatBytes(totalSpace)}
RAM Total: ${_formatBytes(totalRam)}
Rooted: $rooted
$_eventLog""";
});
},
child: const Text('Get System Info'),
),
const SizedBox(height: 20),
_buildSectionTitle('Event Log'),
Container(
height: 150,
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(border: Border.all(color: Colors.grey)),
child: SingleChildScrollView(child: Text(_eventLog)),
),
],
),
),
),
);
}
Widget _buildSectionTitle(String title) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Text(title, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
);
}
String _formatBytes(int bytes) {
if (bytes <= 0) return "0 B";
const suffixes = ["B", "KB", "MB", "GB", "TB"];
var i = 0;
double d = bytes.toDouble();
while (d > 1024 && i < suffixes.length - 1) {
d /= 1024;
i++;
}
return "${d.toStringAsFixed(2)} ${suffixes[i]}";
}
}