Developed by Totalx Software
A Flutter package for checking and synchronizing the device time with NTP (Network Time Protocol) server time. This package ensures that the device's time is accurate within a configurable tolerance and provides user-friendly dialogs to prompt corrections when discrepancies are detected.
- Fetch accurate time from an NTP server.
- Validate device time against NTP time with configurable tolerance.
- Display dialogs for time issues and guide users to date/time settings.
- Supports both Android and iOS platforms for settings navigation.
Add the following dependencies to your project's pubspec.yaml
file:
dependencies:
sync_time_ntp_totalxsoftware: ^0.0.2
Run the following command:
flutter pub get
Check if the device time is synchronized with the NTP server:
import 'package:sync_time_ntp_totalxsoftware/sync_time_ntp_totalxsoftware.dart';
void validateTime() async {
bool isTimeSynced = await NtpTimeSyncChecker.validateDeviceTime(
toleranceInSeconds: 5,
);
if (isTimeSynced) {
print("Device time is synchronized.");
} else {
print("Device time is NOT synchronized.");
}
}
Fetch the current accurate time from an NTP server:
import 'package:sync_time_ntp_totalxsoftware/sync_time_ntp_totalxsoftware.dart';
void fetchNTPTime() async {
DateTime? networkTime = await NtpTimeSyncChecker.getNetworkTime();
if (networkTime != null) {
print("Network Time: \$networkTime");
} else {
print("Failed to fetch NTP time.");
}
}
Display a dialog when the device's time is not synced, prompting the user to correct it:
import 'package:flutter/material.dart';
import 'package:sync_time_ntp_totalxsoftware/sync_time_ntp_totalxsoftware.dart';
// Show a dialog if time is not synchronized
NtpTimeSyncChecker.showTimeSyncIssueDialog(
context: context,
onRetry: () {
Navigator.pop(context);
_validateTime();
},
);
Here is a full example showcasing the package usage:
import 'package:flutter/material.dart';
import 'package:sync_time_ntp_totalxsoftware/sync_time_ntp_totalxsoftware.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'NTP Time Sync Example',
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _status = "Not Checked";
String _networkTime = "-";
Future<void> _validateTime() async {
bool isSynced = await NtpTimeSyncChecker.validateDeviceTime(
toleranceInSeconds: 5,
);
setState(() {
_status = isSynced ? "Time is synchronized" : "Time is NOT synchronized!";
});
if (!isSynced) {
SyncTimeNTP.showTimeIssueDialog(
context: context,
onRetry: _validateTime,
);
}
}
Future<void> _fetchNetworkTime() async {
DateTime? time = await NtpTimeSyncChecker.getNetworkTime();
setState(() {
_networkTime = time?.toString() ?? "Failed to fetch time";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('NTP Time Sync Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Network Time: $_networkTime'),
ElevatedButton(
onPressed: _fetchNetworkTime,
child: const Text('Fetch NTP Time'),
),
const SizedBox(height: 20),
Text('Status: $_status'),
ElevatedButton(
onPressed: _validateTime,
child: const Text('Validate Device Time'),
),
],
),
),
);
}
}
- Description: Validates if the device's time is within a specified tolerance compared to the NTP server.
- Parameters:
toleranceInSeconds
: The acceptable difference in seconds (default:5
seconds).
- Returns:
true
if synchronized, otherwisefalse
.
- Description: Fetches the current accurate time from an NTP server.
- Returns: A
DateTime
object with the network time ornull
on failure.
- Description: Displays a dialog prompting the user to fix time synchronization issues.
- Parameters:
context
: The widget context for displaying the dialog.onRetry
: A callback for retrying the time validation.
- Android: Opens the date/time settings.
- iOS: Opens the date/time settings.
Explore more about TotalX at www.totalx.in - Your trusted software development company!
Join the vibrant Flutter Firebase Kerala community for updates, discussions, and support:
Flutter Firebase Kerala Totax