2026-11-18 19:08:06 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2026-11-26 00:36:27 +08:00
|
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
2026-11-18 19:08:06 +08:00
|
|
|
|
2026-11-21 19:17:22 +08:00
|
|
|
import 'appearance_prefs.dart';
|
2026-11-26 09:04:25 +08:00
|
|
|
import 'l10n.dart';
|
2026-11-22 13:39:49 +08:00
|
|
|
import 'pets/pet_library.dart';
|
2026-11-21 01:16:10 +08:00
|
|
|
import 'screens/home_screen.dart';
|
2026-11-18 19:08:06 +08:00
|
|
|
import 'screens/servers_screen.dart';
|
|
|
|
|
import 'state.dart';
|
2026-11-25 12:39:48 +08:00
|
|
|
import 'version.dart';
|
2026-11-18 19:08:06 +08:00
|
|
|
|
2026-11-25 12:39:48 +08:00
|
|
|
Future<void> main() async {
|
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
// The version goes into every connection's handshake; read it once here so
|
2026-11-26 09:04:25 +08:00
|
|
|
// 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.
|
2026-11-25 12:39:48 +08:00
|
|
|
await loadAppVersion();
|
2026-11-26 09:04:25 +08:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 20:18:04 +08:00
|
|
|
class _OpenChamberAppState extends State<OpenChamberApp>
|
|
|
|
|
with WidgetsBindingObserver {
|
2026-11-18 19:08:06 +08:00
|
|
|
final AppState app = AppState();
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2026-11-19 20:18:04 +08:00
|
|
|
WidgetsBinding.instance.addObserver(this);
|
2026-11-18 19:08:06 +08:00
|
|
|
app.init();
|
2026-11-21 19:17:22 +08:00
|
|
|
AppearancePrefs.instance.ensureLoaded();
|
2026-11-22 13:39:49 +08:00
|
|
|
PetLibrary.instance.ensureLoaded();
|
2026-11-18 19:08:06 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-19 20:18:04 +08:00
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-26 09:04:25 +08:00
|
|
|
@override
|
|
|
|
|
void didChangeLocales(List<Locale>? locales) {
|
|
|
|
|
// "Follow the system" has to redraw when the system language changes.
|
|
|
|
|
L10n.instance.systemLocaleChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 20:18:04 +08:00
|
|
|
@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) {
|
2026-11-21 19:17:22 +08:00
|
|
|
final look = AppearancePrefs.instance;
|
2026-11-18 19:08:06 +08:00
|
|
|
return ListenableBuilder(
|
2026-11-26 09:04:25 +08:00
|
|
|
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)
|
2026-11-21 01:16:10 +08:00
|
|
|
: HomeScreen(app: app);
|
2026-11-18 19:08:06 +08:00
|
|
|
return MaterialApp(
|
|
|
|
|
title: 'OpenChamber',
|
2026-11-26 09:04:25 +08:00
|
|
|
// 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')],
|
2026-11-26 00:36:27 +08:00
|
|
|
localizationsDelegates: const [
|
|
|
|
|
GlobalMaterialLocalizations.delegate,
|
|
|
|
|
GlobalWidgetsLocalizations.delegate,
|
|
|
|
|
GlobalCupertinoLocalizations.delegate,
|
|
|
|
|
],
|
2026-11-21 19:17:22 +08:00
|
|
|
themeMode: look.themeMode,
|
2026-11-18 19:08:06 +08:00
|
|
|
theme: ThemeData(
|
|
|
|
|
colorScheme: ColorScheme.fromSeed(
|
2026-11-21 19:17:22 +08:00
|
|
|
seedColor: look.seedColor,
|
2026-11-18 19:08:06 +08:00
|
|
|
brightness: Brightness.light,
|
|
|
|
|
),
|
|
|
|
|
useMaterial3: true,
|
|
|
|
|
),
|
|
|
|
|
darkTheme: ThemeData(
|
|
|
|
|
colorScheme: ColorScheme.fromSeed(
|
2026-11-21 19:17:22 +08:00
|
|
|
seedColor: look.seedColor,
|
2026-11-18 19:08:06 +08:00
|
|
|
brightness: Brightness.dark,
|
|
|
|
|
),
|
|
|
|
|
useMaterial3: true,
|
|
|
|
|
),
|
|
|
|
|
home: home,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|