device_time_format 0.0.1
device_time_format: ^0.0.1 copied to clipboard
A Flutter plugin to get the device's current time and detect whether the system uses 12-hour or 24-hour format.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:device_time_format/device_time_format.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _deviceTimeFormatPlugin = DeviceTimeFormat();
String _deviceTime = 'Unknown';
bool _is24Hour = false;
@override
void initState() {
super.initState();
fetchTimeInfo();
}
Future<void> fetchTimeInfo() async {
String? time;
bool? is24Hour;
try {
time = await _deviceTimeFormatPlugin.getDeviceTime();
is24Hour = await _deviceTimeFormatPlugin.is24HourFormat();
} catch (e) {
time = 'Error: $e';
}
if (!mounted) return;
setState(() {
_deviceTime = time ?? 'Unknown';
_is24Hour = is24Hour ?? false;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Device Time Format Plugin')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Current Device Time:',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 10),
Text(
_deviceTime,
style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
),
const SizedBox(height: 20),
Text(
_is24Hour ? 'Format: 24-hour' : 'Format: 12-hour (AM/PM)',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 30),
ElevatedButton(
onPressed: fetchTimeInfo,
child: const Text('Refresh Time'),
),
],
),
),
),
);
}
}