import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'appearance_prefs.dart'; import 'l10n.dart'; import 'pets/pet_library.dart'; import 'screens/home_screen.dart'; import 'screens/servers_screen.dart'; import 'state.dart'; import 'version.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); // The version goes into every connection's handshake; read it once here so // nothing waits for it later. The language is read before the first frame // for the same reason: a saved choice should never flash the other one. await loadAppVersion(); await L10n.instance.ensureLoaded(); runApp(const OpenChamberApp()); } class OpenChamberApp extends StatefulWidget { const OpenChamberApp({super.key}); @override State createState() => _OpenChamberAppState(); } class _OpenChamberAppState extends State with WidgetsBindingObserver { final AppState app = AppState(); @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); app.init(); AppearancePrefs.instance.ensureLoaded(); PetLibrary.instance.ensureLoaded(); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); } @override void didChangeLocales(List? locales) { // "Follow the system" has to redraw when the system language changes. L10n.instance.systemLocaleChanged(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { // The socket often dies while the app sits in the background; nothing // reports it, so reopen the stream and refresh the open chat on return. if (state == AppLifecycleState.resumed) app.onResumed(); } @override Widget build(BuildContext context) { final look = AppearancePrefs.instance; return ListenableBuilder( listenable: Listenable.merge([app, look, L10n.instance]), builder: (context, _) { final home = app.client == null ? ServersScreen(app: app) : HomeScreen(app: app); return MaterialApp( title: 'OpenChamber', // Chinese and English, picked in 设置 → 常规 → 语言 or followed // from the system. The built-in pieces (date and time pickers, // text selection menu) follow the same choice. locale: L10n.instance.locale, supportedLocales: const [Locale('en'), Locale('zh', 'CN')], localizationsDelegates: const [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], themeMode: look.themeMode, theme: ThemeData( colorScheme: ColorScheme.fromSeed( seedColor: look.seedColor, brightness: Brightness.light, ), useMaterial3: true, ), darkTheme: ThemeData( colorScheme: ColorScheme.fromSeed( seedColor: look.seedColor, brightness: Brightness.dark, ), useMaterial3: true, ), home: home, ); }, ); } }