date_change_checker 2.1.0
date_change_checker: ^2.1.0 copied to clipboard
A cross-platform Flutter package that detects whether the device's date/time has been changed (automatic date/time is disabled).
Date Change Checker #
A cross-platform Flutter package that detects whether the device's automatic date/time setting is disabled, providing unified functionality for both Android and iOS platforms.
Demo #
iOS Demo #
Android Demo #
Features #
- ✅ Cross-platform support (Android & iOS)
- ✅ Simple, unified API
- ✅ Real-time monitoring with streams
- ✅ Configurable check intervals
- ✅ No special permissions required
- ✅ Lightweight implementation
- ✅ Comprehensive error handling
- ✅ Stream lifecycle management
- ✅ Complete example application
Platform Support #
Platform | Minimum Version | Implementation |
---|---|---|
Android | API Level 21 (Android 5.0) | Settings.Global.AUTO_TIME |
iOS | iOS 11.0 | NSTimeZone.autoupdatingCurrent |
Installation #
Add this to your package's pubspec.yaml
file:
dependencies:
date_change_checker: ^2.0.0
Then run:
flutter pub get
Usage #
Basic Usage #
import 'package:date_change_checker/date_change_checker.dart';
// Check if date/time has been changed (automatic date/time is disabled)
try {
final isChanged = await DateChangeChecker.isDateTimeChanged();
if (isChanged) {
print('Date/time has been changed (automatic date/time is disabled)');
} else {
print('Date/time has not been changed (automatic date/time is enabled)');
}
} on PlatformException catch (e) {
print('Error: ${e.message}');
}
Stream Usage (Real-time Monitoring) #
import 'dart:async';
import 'package:date_change_checker/date_change_checker.dart';
class DateTimeMonitor {
StreamSubscription<AutoDateTimeStatus>? _subscription;
// Start monitoring date/time changes
void startMonitoring() {
_subscription = DateChangeChecker.dateTimeStatusStream(
interval: Duration(seconds: 5), // Check every 5 seconds
).listen(
(status) {
print('Date/time status changed: $status');
// Update your UI here
},
onError: (error) {
print('Monitoring error: $error');
},
);
}
// Stop monitoring
void stopMonitoring() {
_subscription?.cancel();
DateChangeChecker.disposeStream();
}
// Check if monitoring is active
bool get isMonitoring => DateChangeChecker.isStreamActive;
}
Complete Example #
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:date_change_checker/date_change_checker.dart';
//region Date Time Status Widget
class DateTimeStatusWidget extends StatefulWidget {
@override
_DateTimeStatusWidgetState createState() => _DateTimeStatusWidgetState();
}
class _DateTimeStatusWidgetState extends State<DateTimeStatusWidget> {
//region Properties
AutoDateTimeStatus? _status;
bool _isLoading = false;
String? _errorMessage;
//endregion
@override
void initState() {
super.initState();
_checkStatus();
}
//region Status Check Methods
Future<void> _checkStatus() async {
setState(() {
_isLoading = true;
_errorMessage = null;
});
try {
final isChanged = await DateChangeChecker.isDateTimeChanged();
// Convert to status for backward compatibility
final status = isChanged
? AutoDateTimeStatus.AUTO_DATE_TIME_OFF
: AutoDateTimeStatus.AUTO_DATE_TIME_ON;
setState(() {
_status = status;
_isLoading = false;
});
} on PlatformException catch (e) {
setState(() {
_errorMessage = e.message;
_isLoading = false;
});
}
}
//endregion
//region Build Method
@override
Widget build(BuildContext context) {
return Column(
children: [
if (_isLoading)
CircularProgressIndicator()
else if (_errorMessage != null)
Text('Error: $_errorMessage', style: TextStyle(color: Colors.red))
else if (_status != null)
Text(
_status == AutoDateTimeStatus.AUTO_DATE_TIME_ON
? 'Auto Date/Time: Enabled'
: 'Auto Date/Time: Disabled',
style: TextStyle(
color: _status == AutoDateTimeStatus.AUTO_DATE_TIME_ON
? Colors.green
: Colors.red,
),
),
ElevatedButton(
onPressed: _checkStatus,
child: Text('Refresh Status'),
),
],
);
}
//endregion
}
//endregion
API Reference #
DateChangeChecker #
Main class for checking automatic date/time settings.
Methods
isDateTimeChanged()
static Future<bool> isDateTimeChanged()
Checks if the device's date/time has been changed (automatic date/time is disabled).
Returns:
true
if date/time has been changed (automatic date/time is disabled)false
if date/time has not been changed (automatic date/time is enabled)
checkAutoDateTimeStatus()
(Deprecated)
static Future<AutoDateTimeStatus> checkAutoDateTimeStatus()
Deprecated: Use isDateTimeChanged()
instead.
Checks if automatic date/time is enabled on the device.
Returns:
AutoDateTimeStatus.AUTO_DATE_TIME_ON
if automatic date/time is enabledAutoDateTimeStatus.AUTO_DATE_TIME_OFF
if automatic date/time is disabled
Throws:
PlatformException
if the platform is not supported or an error occurs
AutoDateTimeStatus #
Enum representing the status of automatic date/time setting.
enum AutoDateTimeStatus {
/// Automatic date/time is enabled
AUTO_DATE_TIME_ON,
/// Automatic date/time is disabled
AUTO_DATE_TIME_OFF
}
Platform-Specific Implementation Details #
Android #
The Android implementation uses Settings.Global.AUTO_TIME
to check if automatic date/time is enabled. This setting is publicly readable and doesn't require any special permissions.
Settings.Global.getInt(context.contentResolver, Settings.Global.AUTO_TIME) == 1
iOS #
The iOS implementation uses NSTimeZone.autoupdatingCurrent
to determine if the device is set to automatically update its time zone and date/time settings.
let autoUpdatingTimeZone = NSTimeZone.autoupdatingCurrent
let systemTimeZone = NSTimeZone.system
return autoUpdatingTimeZone.isEqual(to: systemTimeZone)
Error Handling #
The package provides comprehensive error handling:
- Platform not supported: Throws
PlatformException
with appropriate error code - System API failure: Returns safe default values with proper error reporting
- Permission issues: Handled gracefully with fallback behavior
Example Application #
Run the example application to see the package in action:
cd example
flutter run
The example app demonstrates:
- Basic status checking
- Error handling
- UI integration
- Real-time status updates
- User guidance for enabling automatic date/time
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request on GitHub.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog #
See CHANGELOG.md for a detailed list of changes.