simplest_power_utilities 0.0.1
simplest_power_utilities: ^0.0.1 copied to clipboard
Plugin which exposes various power-related utilities for Android and iOS, including low power mode detection
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:simplest_power_utilities/simplest_power_utilities.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Low Power Mode Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const LowPowerModePage(),
);
}
}
class LowPowerModePage extends StatefulWidget {
const LowPowerModePage({super.key});
@override
State<LowPowerModePage> createState() => _LowPowerModePageState();
}
class _LowPowerModePageState extends State<LowPowerModePage> {
final _plugin = SimplestPowerUtilities();
bool? _isLowPowerModeEnabled;
String? _errorMessage;
StreamSubscription<bool>? _subscription;
int _updateCount = 0;
@override
void initState() {
super.initState();
_initializeState();
_subscribeToChanges();
}
@override
void dispose() {
_subscription?.cancel();
super.dispose();
}
Future<void> _initializeState() async {
try {
final isEnabled = await _plugin.isLowPowerModeEnabled();
if (mounted) {
setState(() {
_isLowPowerModeEnabled = isEnabled;
_errorMessage = null;
});
}
} on PlatformException catch (e) {
if (mounted) {
setState(() {
_errorMessage = 'Error: ${e.message}';
_isLowPowerModeEnabled = null;
});
}
} catch (e) {
if (mounted) {
setState(() {
_errorMessage = 'Unexpected error: $e';
_isLowPowerModeEnabled = null;
});
}
}
}
void _subscribeToChanges() {
_subscription = _plugin.onLowPowerModeChanged.listen(
(isEnabled) {
if (mounted) {
setState(() {
_isLowPowerModeEnabled = isEnabled;
_updateCount++;
_errorMessage = null;
});
}
},
onError: (error) {
if (mounted) {
setState(() {
_errorMessage = 'Stream error: $error';
});
}
},
);
}
Future<void> _refreshStatus() async {
await _initializeState();
}
Future<void> _launchBatterySettings() async {
try {
final success = await _plugin.launchBatterySettings();
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
success
? 'Battery settings opened successfully'
: 'Failed to open battery settings',
),
backgroundColor: success
? Theme.of(context).colorScheme.primaryContainer
: Theme.of(context).colorScheme.errorContainer,
duration: const Duration(seconds: 2),
),
);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error launching battery settings: $e'),
backgroundColor: Theme.of(context).colorScheme.errorContainer,
duration: const Duration(seconds: 3),
),
);
}
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colorScheme = theme.colorScheme;
return Scaffold(
appBar: AppBar(
title: const Text('Low Power Mode Detection'),
actions: [
IconButton(
icon: const Icon(Icons.refresh),
onPressed: _refreshStatus,
tooltip: 'Refresh status',
),
],
),
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Status Card
Card(
elevation: 2,
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
children: [
Icon(
_isLowPowerModeEnabled == true
? Icons.battery_saver
: _isLowPowerModeEnabled == false
? Icons.battery_std
: Icons.battery_unknown,
size: 64,
color: _isLowPowerModeEnabled == true
? colorScheme.error
: _isLowPowerModeEnabled == false
? colorScheme.primary
: colorScheme.onSurfaceVariant,
),
const SizedBox(height: 16),
Text(
_isLowPowerModeEnabled == true
? 'Low Power Mode Enabled'
: _isLowPowerModeEnabled == false
? 'Low Power Mode Disabled'
: 'Status Unknown',
style: theme.textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
if (_errorMessage != null)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
_errorMessage!,
style: theme.textTheme.bodyMedium?.copyWith(
color: colorScheme.error,
),
textAlign: TextAlign.center,
),
),
],
),
),
),
const SizedBox(height: 24),
// Details Card
Card(
elevation: 1,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Details',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
_buildDetailRow(
context,
'Current Status',
_isLowPowerModeEnabled == null
? 'Unknown'
: (_isLowPowerModeEnabled!
? 'Enabled'
: 'Disabled'),
),
const Divider(),
_buildDetailRow(
context,
'Updates Received',
'$_updateCount',
),
const Divider(),
_buildDetailRow(
context,
'Stream Active',
_subscription != null ? 'Yes' : 'No',
),
],
),
),
),
const SizedBox(height: 24),
// Launch Battery Settings Button
Card(
elevation: 1,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Battery Settings',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: _launchBatterySettings,
icon: const Icon(Icons.settings),
label: const Text('Open Battery Settings'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(
horizontal: 24.0,
vertical: 16.0,
),
),
),
const SizedBox(height: 8),
Text(
'Tap to open your device\'s battery settings screen',
style: theme.textTheme.bodySmall?.copyWith(
color: colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
],
),
),
),
const SizedBox(height: 24),
// Information Card
Card(
elevation: 1,
color: colorScheme.surfaceContainerHighest,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
Icons.info_outline,
size: 20,
color: colorScheme.primary,
),
const SizedBox(width: 8),
Text(
'Information',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
),
),
],
),
const SizedBox(height: 12),
Text(
'This app demonstrates real-time low power mode detection. '
'The status updates automatically when low power mode is enabled or disabled on your device.',
style: theme.textTheme.bodyMedium,
),
const SizedBox(height: 8),
Text(
'To test: Enable or disable low power mode in your device settings and watch the status update in real-time.',
style: theme.textTheme.bodySmall?.copyWith(
color: colorScheme.onSurfaceVariant,
),
),
],
),
),
),
],
),
),
),
);
}
Widget _buildDetailRow(BuildContext context, String label, String value) {
final theme = Theme.of(context);
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
label,
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
Text(
value,
style: theme.textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.w500,
),
),
],
);
}
}