Files

101 lines
3.2 KiB
Dart
Raw Permalink Normal View History

2026-11-18 19:08:06 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
2026-11-18 19:08:06 +08:00
import 'appearance_prefs.dart';
import 'l10n.dart';
import 'pets/pet_library.dart';
import 'screens/home_screen.dart';
2026-11-18 19:08:06 +08:00
import 'screens/servers_screen.dart';
import 'state.dart';
import 'version.dart';
2026-11-18 19:08:06 +08:00
Future<void> 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();
2026-11-18 19:08:06 +08:00
runApp(const OpenChamberApp());
}
class OpenChamberApp extends StatefulWidget {
const OpenChamberApp({super.key});
@override
State<OpenChamberApp> createState() => _OpenChamberAppState();
}
class _OpenChamberAppState extends State<OpenChamberApp>
with WidgetsBindingObserver {
2026-11-18 19:08:06 +08:00
final AppState app = AppState();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
2026-11-18 19:08:06 +08:00
app.init();
AppearancePrefs.instance.ensureLoaded();
PetLibrary.instance.ensureLoaded();
2026-11-18 19:08:06 +08:00
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeLocales(List<Locale>? 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();
}
2026-11-18 19:08:06 +08:00
@override
Widget build(BuildContext context) {
final look = AppearancePrefs.instance;
2026-11-18 19:08:06 +08:00
return ListenableBuilder(
listenable: Listenable.merge([app, look, L10n.instance]),
2026-11-18 19:08:06 +08:00
builder: (context, _) {
final home = app.client == null
? ServersScreen(app: app)
: HomeScreen(app: app);
2026-11-18 19:08:06 +08:00
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,
2026-11-18 19:08:06 +08:00
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: look.seedColor,
2026-11-18 19:08:06 +08:00
brightness: Brightness.light,
),
useMaterial3: true,
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: look.seedColor,
2026-11-18 19:08:06 +08:00
brightness: Brightness.dark,
),
useMaterial3: true,
),
home: home,
);
},
);
}
}