system_date_time_change 1.0.1 copy "system_date_time_change: ^1.0.1" to clipboard
system_date_time_change: ^1.0.1 copied to clipboard

A Flutter plugin to detect manual changes to device date/time and timezone mismatches across all platforms.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:system_date_time_change/system_date_time_change.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  DateTimeChangeStatus? _status;
  final List<DateTimeChangeEvent> _events = [];
  StreamSubscription<DateTimeChangeEvent>? _subscription;
  final _systemDateTimeChangePlugin = SystemDateTimeChange();

  @override
  void initState() {
    super.initState();
    startListening();
  }

  @override
  void dispose() {
    _subscription?.cancel();
    _systemDateTimeChangePlugin.stopListening();
    super.dispose();
  }

  Future<void> startListening() async {
    try {
      _subscription = _systemDateTimeChangePlugin.onDateTimeChanged.listen((
        event,
      ) {
        setState(() {
          _events.insert(0, event);
          if (_events.length > 10) {
            _events.removeLast();
          }
        });
      });
    } catch (e) {
      debugPrint('Error starting listener: $e');
    }
  }

  Future<void> checkStatus() async {
    try {
      final status = await _systemDateTimeChangePlugin.checkDateTimeStatus();
      setState(() {
        _status = status;
      });
    } on PlatformException catch (e) {
      debugPrint('Error checking status: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('System Date Time Change'),
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        ),
        body: SingleChildScrollView(
          padding: const EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              ElevatedButton.icon(
                onPressed: checkStatus,
                icon: const Icon(Icons.refresh),
                label: const Text('Check Status'),
              ),
              const SizedBox(height: 16),
              if (_status != null)
                Card(
                  child: Padding(
                    padding: const EdgeInsets.all(16),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'Current Status',
                          style: Theme.of(context).textTheme.titleMedium,
                        ),
                        const SizedBox(height: 8),
                        _buildStatusRow(
                          'Auto Time',
                          _status!.isAutoTimeEnabled,
                        ),
                        _buildStatusRow(
                          'Auto Time Zone',
                          _status!.isAutoTimeZoneEnabled,
                        ),
                        _buildStatusRow(
                          'Manual Change',
                          _status!.hasManualChange,
                        ),
                        _buildStatusRow(
                          'Timezone Mismatch',
                          _status!.hasTimezoneMismatch,
                        ),
                        const Divider(),
                        Text('Current: ${_status!.currentTime}'),
                        Text('Expected: ${_status!.expectedTime}'),
                        Text('Timezone: ${_status!.timezone}'),
                        const Divider(),
                        Text(
                          'Time Difference: ${_formatDuration(_status!.timeDifference)}',
                        ),
                        Text(
                          'Device Uptime: ${_formatDuration(_status!.deviceUptime)}',
                        ),
                      ],
                    ),
                  ),
                ),
              const SizedBox(height: 16),
              Text(
                'Events (${_events.length})',
                style: Theme.of(context).textTheme.titleMedium,
              ),
              const SizedBox(height: 8),
              if (_events.isEmpty)
                const Card(
                  child: Padding(
                    padding: EdgeInsets.all(16),
                    child: Center(
                      child: Text(
                        'No events yet. Change your system time to see events.',
                      ),
                    ),
                  ),
                )
              else
                ..._events.map(
                  (event) => Card(
                    margin: const EdgeInsets.only(bottom: 8),
                    child: Padding(
                      padding: const EdgeInsets.all(12),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Icon(
                                event.isManualChange
                                    ? Icons.warning
                                    : Icons.info,
                                color: event.isManualChange
                                    ? Colors.orange
                                    : Colors.blue,
                                size: 16,
                              ),
                              const SizedBox(width: 8),
                              Text(
                                event.isManualChange
                                    ? 'Manual Change'
                                    : 'Time Change',
                                style: const TextStyle(
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ],
                          ),
                          const SizedBox(height: 4),
                          Text(
                            'Current: ${event.currentTime}',
                            style: const TextStyle(fontSize: 12),
                          ),
                          Text(
                            'Expected: ${event.expectedTime}',
                            style: const TextStyle(fontSize: 12),
                          ),
                          Text(
                            'Timezone: ${event.timezone}',
                            style: const TextStyle(fontSize: 12),
                          ),
                          Text(
                            'Difference: ${_formatDuration(event.timeDifference)}',
                            style: const TextStyle(fontSize: 12),
                          ),
                          Text(
                            'Uptime: ${_formatDuration(event.deviceUptime)}',
                            style: const TextStyle(fontSize: 12),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildStatusRow(String label, bool value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: Row(
        children: [
          Icon(
            value ? Icons.check_circle : Icons.cancel,
            color: value ? Colors.green : Colors.red,
            size: 16,
          ),
          const SizedBox(width: 8),
          Text(label),
        ],
      ),
    );
  }

  String _formatDuration(Duration duration) {
    final days = duration.inDays;
    final hours = duration.inHours.remainder(24);
    final minutes = duration.inMinutes.remainder(60);
    final seconds = duration.inSeconds.remainder(60);

    if (days > 0) {
      return '${days}d ${hours}h ${minutes}m ${seconds}s';
    } else if (hours > 0) {
      return '${hours}h ${minutes}m ${seconds}s';
    } else if (minutes > 0) {
      return '${minutes}m ${seconds}s';
    } else {
      return '${seconds}s';
    }
  }
}
3
likes
160
points
183
downloads

Publisher

verified publishertusaway.com

Weekly Downloads

A Flutter plugin to detect manual changes to device date/time and timezone mismatches across all platforms.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_web_plugins, plugin_platform_interface, web

More

Packages that depend on system_date_time_change

Packages that implement system_date_time_change