- settings → general → language picks system, Chinese or English; the choice is stored on the device and read before the first frame - our Chinese strings are translated through tr()/trArgs() and AppText, with the English in lib/l10n/en_*.dart; a missing entry falls back to Chinese and a test checks that every literal in lib has one - AppText rebuilds on a language change (it depends on the resolved locale), so const text and whole pages switch at once; the built-in date/time pickers and selection menu follow the same language - server and user data (titles, messages, prompts, paths) is never translated - the chat list sits in a SelectionArea: long-press selects message text and shows the copy/select-all menu - bump to 0.5.2
144 lines
4.5 KiB
Dart
144 lines
4.5 KiB
Dart
// The connected shell. A phone treats the session list as the left drawer and
|
|
// the chat as the screen; a wide layout shows both side by side, which is
|
|
// where a tablet or a desktop window lands.
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../state.dart';
|
|
import 'chat_screen.dart';
|
|
import 'new_session_screen.dart';
|
|
import 'sessions_screen.dart';
|
|
import '../l10n.dart';
|
|
|
|
class HomeScreen extends StatefulWidget {
|
|
final AppState app;
|
|
const HomeScreen({super.key, required this.app});
|
|
|
|
@override
|
|
State<HomeScreen> createState() => _HomeScreenState();
|
|
}
|
|
|
|
class _HomeScreenState extends State<HomeScreen> {
|
|
final GlobalKey<ScaffoldState> _scaffold = GlobalKey();
|
|
bool _opening = false;
|
|
|
|
static const _wideBreakpoint = 840.0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
widget.app.addListener(_maybeOpenFirst);
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => _maybeOpenFirst());
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
widget.app.removeListener(_maybeOpenFirst);
|
|
super.dispose();
|
|
}
|
|
|
|
/// Like the first-party client, a phone lands in a conversation rather than
|
|
/// on an empty pane: open the newest one when nothing is open yet.
|
|
void _maybeOpenFirst() {
|
|
final app = widget.app;
|
|
if (!mounted || _opening) return;
|
|
if (app.draftOpen) return;
|
|
if (app.chat != null || app.sessions.isEmpty) return;
|
|
_opening = true;
|
|
app.openChat(app.sessions.first).whenComplete(() => _opening = false);
|
|
}
|
|
|
|
void _closeDrawer() {
|
|
_scaffold.currentState?.closeDrawer();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// Read above the shell's Scaffold: its body has the bottom inset removed.
|
|
final keyboardInset = MediaQuery.viewInsetsOf(context).bottom;
|
|
return ListenableBuilder(
|
|
listenable: widget.app,
|
|
builder: (context, _) {
|
|
return LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final wide = constraints.maxWidth >= _wideBreakpoint;
|
|
final app = widget.app;
|
|
final chat = app.chat;
|
|
final draftOpen = app.draftOpen && app.draft != null;
|
|
final content = draftOpen
|
|
? NewSessionScreen(
|
|
key: const ValueKey('draft'),
|
|
app: app,
|
|
keyboardInset: keyboardInset,
|
|
onOpenSessions: wide
|
|
? null
|
|
: () => _scaffold.currentState?.openDrawer(),
|
|
)
|
|
: chat == null
|
|
? _empty(wide)
|
|
: ChatScreen(
|
|
key: ValueKey(chat.session.id),
|
|
app: app,
|
|
session: chat.session,
|
|
keyboardInset: keyboardInset,
|
|
onOpenSessions: wide
|
|
? null
|
|
: () => _scaffold.currentState?.openDrawer(),
|
|
);
|
|
if (wide) {
|
|
return Scaffold(
|
|
body: Row(
|
|
children: [
|
|
SizedBox(
|
|
width: 320,
|
|
child: SessionListPane(app: widget.app),
|
|
),
|
|
const VerticalDivider(width: 1),
|
|
Expanded(child: content),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
return Scaffold(
|
|
key: _scaffold,
|
|
drawer: Drawer(
|
|
width: constraints.maxWidth * 0.86 > 380
|
|
? 380
|
|
: constraints.maxWidth * 0.86,
|
|
child: SessionListPane(
|
|
app: widget.app,
|
|
onOpened: _closeDrawer,
|
|
),
|
|
),
|
|
body: content,
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _empty(bool wide) {
|
|
final scheme = Theme.of(context).colorScheme;
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(32),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(Icons.forum_outlined, size: 40, color: scheme.onSurfaceVariant),
|
|
const SizedBox(height: 12),
|
|
AppText('选择一个会话',
|
|
style: TextStyle(fontSize: 16, color: scheme.onSurfaceVariant)),
|
|
if (!wide) ...[
|
|
const SizedBox(height: 12),
|
|
FilledButton(
|
|
onPressed: () => _scaffold.currentState?.openDrawer(),
|
|
child: const AppText('打开会话列表'),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |