managed_settings_ios 0.1.0
managed_settings_ios: ^0.1.0 copied to clipboard
A Flutter plugin for managing iOS Screen Time settings to block apps during sleep mode using Apple's ManagedSettings framework.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:managed_settings_ios/managed_settings_ios.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Managed Settings iOS Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const SleepModeExample(),
);
}
}
class SleepModeExample extends StatefulWidget {
const SleepModeExample({super.key});
@override
State<SleepModeExample> createState() => _SleepModeExampleState();
}
class _SleepModeExampleState extends State<SleepModeExample> {
bool _hasPermission = false;
bool _isSleepModeActive = false;
bool _isLoading = false;
@override
void initState() {
super.initState();
_checkPermission();
_checkSleepModeStatus();
}
Future<void> _checkPermission() async {
final hasPermission = await ManagedSettingsIos.hasScreenTimePermission();
setState(() {
_hasPermission = hasPermission;
});
}
Future<void> _checkSleepModeStatus() async {
final isActive = await ManagedSettingsIos.isSleepModeActive();
setState(() {
_isSleepModeActive = isActive;
});
}
Future<void> _requestPermission() async {
setState(() {
_isLoading = true;
});
try {
final granted = await ManagedSettingsIos.requestScreenTimePermission();
setState(() {
_hasPermission = granted;
});
if (granted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Screen Time permission granted!'),
backgroundColor: Colors.green,
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Screen Time permission denied. Please enable it in Settings.'),
backgroundColor: Colors.red,
),
);
}
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error requesting permission: $e'),
backgroundColor: Colors.red,
),
);
} finally {
setState(() {
_isLoading = false;
});
}
}
Future<void> _startSleepMode() async {
if (!_hasPermission) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Screen Time permission is required. Please grant permission first.'),
backgroundColor: Colors.orange,
),
);
return;
}
setState(() {
_isLoading = true;
});
try {
final success = await ManagedSettingsIos.startSleepMode();
setState(() {
_isSleepModeActive = success;
});
if (success) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Sleep mode started successfully!'),
backgroundColor: Colors.green,
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Failed to start sleep mode.'),
backgroundColor: Colors.red,
),
);
}
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error starting sleep mode: $e'),
backgroundColor: Colors.red,
),
);
} finally {
setState(() {
_isLoading = false;
});
}
}
Future<void> _stopSleepMode() async {
setState(() {
_isLoading = true;
});
try {
final success = await ManagedSettingsIos.stopSleepMode();
setState(() {
_isSleepModeActive = !success;
});
if (success) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Sleep mode stopped successfully!'),
backgroundColor: Colors.green,
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Failed to stop sleep mode.'),
backgroundColor: Colors.red,
),
);
}
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error stopping sleep mode: $e'),
backgroundColor: Colors.red,
),
);
} finally {
setState(() {
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Managed Settings iOS Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Permission Status
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Screen Time Permission',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Row(
children: [
Icon(
_hasPermission ? Icons.check_circle : Icons.cancel,
color: _hasPermission ? Colors.green : Colors.red,
),
const SizedBox(width: 8),
Text(
_hasPermission ? 'Granted' : 'Not Granted',
style: TextStyle(
color: _hasPermission ? Colors.green : Colors.red,
),
),
],
),
],
),
),
),
const SizedBox(height: 16),
// Sleep Mode Status
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Sleep Mode Status',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Row(
children: [
Icon(
_isSleepModeActive ? Icons.bedtime : Icons.bedtime_outlined,
color: _isSleepModeActive ? Colors.orange : Colors.grey,
),
const SizedBox(width: 8),
Text(
_isSleepModeActive ? 'Active' : 'Inactive',
style: TextStyle(
color: _isSleepModeActive ? Colors.orange : Colors.grey,
),
),
],
),
],
),
),
),
const SizedBox(height: 24),
// Request Permission Button
ElevatedButton(
onPressed: _isLoading ? null : _requestPermission,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
child: const Text('Request Screen Time Permission'),
),
const SizedBox(height: 12),
// Start Sleep Mode Button
ElevatedButton(
onPressed: _isLoading || !_hasPermission || _isSleepModeActive
? null
: _startSleepMode,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
backgroundColor: Colors.orange,
),
child: const Text('Start Sleep Mode'),
),
const SizedBox(height: 12),
// Stop Sleep Mode Button
ElevatedButton(
onPressed: _isLoading || !_isSleepModeActive
? null
: _stopSleepMode,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
backgroundColor: Colors.red,
),
child: const Text('Stop Sleep Mode'),
),
if (_isLoading)
const Padding(
padding: EdgeInsets.all(16.0),
child: Center(
child: CircularProgressIndicator(),
),
),
],
),
),
);
}
}