91 lines
2.4 KiB
Dart
91 lines
2.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
import 'package:inou_app/design/inou_theme.dart';
|
|
import 'package:inou_app/core/router.dart';
|
|
import 'package:inou_app/core/locale_provider.dart';
|
|
|
|
void main() {
|
|
runApp(const InouApp());
|
|
}
|
|
|
|
/// Global locale notifier - accessible throughout the app
|
|
final localeNotifier = ValueNotifier<Locale>(const Locale('en'));
|
|
|
|
class InouApp extends StatefulWidget {
|
|
const InouApp({super.key});
|
|
|
|
@override
|
|
State<InouApp> createState() => _InouAppState();
|
|
|
|
/// Static method to change locale from anywhere
|
|
static void setLocale(BuildContext context, Locale locale) {
|
|
localeNotifier.value = locale;
|
|
// Also persist to SharedPreferences
|
|
LocaleProvider().setLocale(locale);
|
|
}
|
|
|
|
/// Get current locale
|
|
static Locale getLocale(BuildContext context) {
|
|
return localeNotifier.value;
|
|
}
|
|
}
|
|
|
|
class _InouAppState extends State<InouApp> {
|
|
late LocaleProvider _localeProvider;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_localeProvider = LocaleProvider();
|
|
_localeProvider.addListener(_onLocaleChanged);
|
|
// Initialize with saved locale
|
|
_loadSavedLocale();
|
|
}
|
|
|
|
Future<void> _loadSavedLocale() async {
|
|
// Wait for provider to load saved locale
|
|
await Future.delayed(const Duration(milliseconds: 100));
|
|
if (mounted) {
|
|
localeNotifier.value = _localeProvider.locale;
|
|
}
|
|
}
|
|
|
|
void _onLocaleChanged() {
|
|
if (mounted) {
|
|
localeNotifier.value = _localeProvider.locale;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_localeProvider.removeListener(_onLocaleChanged);
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ValueListenableBuilder<Locale>(
|
|
valueListenable: localeNotifier,
|
|
builder: (context, locale, _) {
|
|
return MaterialApp.router(
|
|
title: 'inou',
|
|
debugShowCheckedModeBanner: false,
|
|
theme: InouTheme.light,
|
|
routerConfig: appRouter,
|
|
|
|
// Localization setup
|
|
locale: locale,
|
|
localizationsDelegates: const [
|
|
AppLocalizations.delegate,
|
|
GlobalMaterialLocalizations.delegate,
|
|
GlobalWidgetsLocalizations.delegate,
|
|
GlobalCupertinoLocalizations.delegate,
|
|
],
|
|
supportedLocales: LocaleProvider.supportedLocales,
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|