flutter_timezone_observer 1.0.0
flutter_timezone_observer: ^1.0.0 copied to clipboard
Get and observe the device's IANA timezone. Includes mixins to automatically sync the timezone when the app resumes.
import 'package:flutter/material.dart';
import 'package:flutter_timezone_observer/app_lifecycle_aware_timezone_observer_mixin.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp>
with AppLifecycleAwareTimezoneObserverMixin {
@override
void onTimezoneChanged(String newZone) {
if (!mounted) return;
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const Text('Timezone Observer Example'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Current device timezone (via Mixin):',
style: TextStyle(fontSize: 16),
),
const SizedBox(height: 8),
Text(
currentZone ?? 'Loading...',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 32),
const Text(
'To test: Background the app, '
'change your device timezone in settings, '
'and return to the app.',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.grey),
),
],
),
),
),
),
);
}
}