feat: port the client to Flutter (0.3.0)

Replace the MyGo/Go app with a Flutter client that keeps the feature set:

- servers: saved servers, passwordless and password login, HTTP/SOCKS5
  proxy via dart:io, self-signed certificates and pinning, tokens in
  flutter_secure_storage (Keystore)
- sessions: list with live events, search, new/rename/archive/delete,
  instance sheet
- chat: streaming Markdown (flutter_markdown), reasoning and tool rows,
  image previews, context usage ring and sheet, model/agent switching,
  fork/revert, attachments (file_picker), permissions
- Android 7.0 (API 24)+, plain http:// works in release (Dart dials
  sockets directly)
- build: China mirrors for Gradle/NDK, Aliyun Maven repositories, and the
  Gradle distribution pre-seeded; release signed with the test keystore

Read-aloud is dropped as requested. Version 0.3.0.
This commit is contained in:
blumia committed 2026-11-18 19:08:06 +08:00
1 parent 24a311f234
commit aee560172c
2616 files changed
+5810 -672083

No files matched your search

File diff suppressed because it is too large. Load diff
+317
View File
@@ -0,0 +1,317 @@
import 'package:flutter/material.dart';
import '../state.dart';
import '../storage.dart';
/// The first screen: the saved servers, or the connect form.
class ServersScreen extends StatefulWidget {
final AppState app;
const ServersScreen({super.key, required this.app});
@override
State<ServersScreen> createState() => _ServersScreenState();
}
class _ServersScreenState extends State<ServersScreen> {
bool _editing = false;
bool _editNew = true;
Connection? _edit;
String _label = '';
String _url = '';
String _proxy = '';
bool _insecure = false;
String _fingerprint = '';
String _password = '';
bool _connecting = false;
String? _formError;
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: widget.app,
builder: (context, _) {
if (_editing) return _form();
return _list();
},
);
}
Widget _list() {
final theme = Theme.of(context);
final conns = widget.app.connections;
return Scaffold(
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(20, 20, 20, 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('OpenChamber',
style: theme.textTheme.headlineMedium
?.copyWith(fontWeight: FontWeight.bold)),
const SizedBox(height: 4),
Text('连接你的 OpenChamber 服务器',
style: theme.textTheme.bodyMedium
?.copyWith(color: theme.hintColor)),
],
),
),
if (widget.app.error != null)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
child: Text(widget.app.error!,
style: TextStyle(color: theme.colorScheme.error, fontSize: 13)),
),
Expanded(
child: ListView(
padding: const EdgeInsets.all(16),
children: [
for (final c in conns) _serverCard(c),
const SizedBox(height: 12),
OutlinedButton.icon(
onPressed: () => _startEdit(null),
icon: const Icon(Icons.add),
label: const Text('添加服务器'),
),
],
),
),
],
),
),
);
}
Widget _serverCard(Connection c) {
final theme = Theme.of(context);
final connected = widget.app.conn?.id == c.id;
return Card(
margin: const EdgeInsets.only(bottom: 10),
child: ListTile(
title: Text(c.label, style: const TextStyle(fontWeight: FontWeight.w600)),
subtitle: Text(c.url, maxLines: 1, overflow: TextOverflow.ellipsis),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (connected)
Text('已连接', style: TextStyle(color: theme.colorScheme.primary))
else
TextButton(onPressed: () => _connect(c), child: const Text('连接')),
PopupMenuButton<String>(
onSelected: (v) {
if (v == 'edit') {
_startEdit(c);
} else if (v == 'delete') {
_confirmDelete(c);
}
},
itemBuilder: (_) => const [
PopupMenuItem(value: 'edit', child: Text('编辑')),
PopupMenuItem(value: 'delete', child: Text('删除')),
],
),
],
),
),
);
}
void _connect(Connection c) async {
setState(() {
_connecting = true;
_editNew = false;
_edit = c;
});
await widget.app.connect(c);
if (mounted && widget.app.client == null && widget.app.lastError != null) {
setState(() {
_connecting = false;
_editing = true;
_edit = c;
_label = c.label;
_url = c.url;
_proxy = c.proxy;
_insecure = c.insecure;
_fingerprint = c.fingerprint;
_password = '';
_formError = widget.app.lastError;
});
widget.app.clearLastError();
} else if (mounted) {
setState(() => _connecting = false);
}
}
void _startEdit(Connection? c) {
setState(() {
_editing = true;
_editNew = c == null;
_edit = c;
_label = c?.label ?? '';
_url = c?.url ?? '';
_proxy = c?.proxy ?? '';
_insecure = c?.insecure ?? false;
_fingerprint = c?.fingerprint ?? '';
_password = '';
_formError = null;
});
}
Widget _form() {
final theme = Theme.of(context);
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => setState(() => _editing = false),
),
title: Text(_editNew ? '添加服务器' : '连接 ${_edit!.label}'),
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
_labelText('名称'),
TextField(
controller: TextEditingController(text: _label),
onChanged: (v) => _label = v,
decoration: const InputDecoration(hintText: '如 我的电脑', border: OutlineInputBorder()),
),
const SizedBox(height: 12),
_labelText('服务器地址'),
TextField(
controller: TextEditingController(text: _url),
onChanged: (v) => _url = v,
keyboardType: TextInputType.url,
decoration: const InputDecoration(
hintText: 'http://192.168.1.10:3000', border: OutlineInputBorder()),
),
const SizedBox(height: 12),
_labelText('代理(可选)'),
TextField(
controller: TextEditingController(text: _proxy),
onChanged: (v) => _proxy = v,
keyboardType: TextInputType.url,
decoration: const InputDecoration(
hintText: 'http://… 或 socks5://…', border: OutlineInputBorder()),
),
const SizedBox(height: 12),
_labelText('自签证书指纹(可选)'),
TextField(
controller: TextEditingController(text: _fingerprint),
onChanged: (v) => _fingerprint = v,
decoration: const InputDecoration(hintText: 'SHA-256 十六进制', border: OutlineInputBorder()),
),
SwitchListTile(
contentPadding: EdgeInsets.zero,
title: const Text('信任自签名证书'),
value: _insecure,
onChanged: (v) => setState(() => _insecure = v),
),
if (!_editNew) ...[
_labelText('密码'),
TextField(
controller: TextEditingController(text: _password),
onChanged: (v) => _password = v,
obscureText: true,
decoration: const InputDecoration(
hintText: '服务器密码', border: OutlineInputBorder()),
),
],
if (_formError != null)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(_formError!,
style: TextStyle(color: theme.colorScheme.error)),
),
const SizedBox(height: 16),
FilledButton(
onPressed: _connecting ? null : _submit,
child: _connecting
? const SizedBox(
height: 20, width: 20, child: CircularProgressIndicator(strokeWidth: 2))
: const Text('连接'),
),
],
),
);
}
Widget _labelText(String s) => Padding(
padding: const EdgeInsets.only(bottom: 4),
child: Text(s, style: Theme.of(context).textTheme.bodySmall),
);
Future<void> _submit() async {
setState(() {
_connecting = true;
_formError = null;
});
final c = Connection(
id: _edit?.id ?? _newId(),
label: _label.isEmpty ? _hostOf(_url) : _label,
url: _url,
proxy: _proxy,
insecure: _insecure,
fingerprint: _fingerprint,
);
if (_editNew) {
await widget.app.saveConnection(c);
} else {
final list = [...widget.app.connections];
for (var i = 0; i < list.length; i++) {
if (list[i].id == c.id) {
list[i] = c;
break;
}
}
widget.app.connections = list;
await widget.app.storage.saveConnections(list);
}
await widget.app.connect(c, password: _password);
if (mounted) {
if (widget.app.client != null) {
setState(() => _editing = false);
} else {
setState(() {
_connecting = false;
_formError = widget.app.lastError;
});
widget.app.clearLastError();
}
}
}
void _confirmDelete(Connection c) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('删除服务器?'),
content: Text('“${c.label}”将从列表移除,设备令牌也会删除。'),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')),
FilledButton(
onPressed: () {
Navigator.pop(context);
widget.app.removeConnection(c);
},
child: const Text('删除'),
),
],
),
);
}
static String _newId() =>
'srv_${DateTime.now().microsecondsSinceEpoch.toRadixString(36)}';
static String _hostOf(String raw) {
try {
return Uri.parse(raw).host;
} catch (_) {
return raw;
}
}
}
+337
View File
@@ -0,0 +1,337 @@
import 'dart:async';
import 'package:flutter/material.dart';
import '../api/models.dart';
import '../state.dart';
import 'chat_screen.dart';
/// The connected screen: the live session list.
class SessionsScreen extends StatefulWidget {
final AppState app;
const SessionsScreen({super.key, required this.app});
@override
State<SessionsScreen> createState() => _SessionsScreenState();
}
class _SessionsScreenState extends State<SessionsScreen> {
bool _searchOpen = false;
Timer? _timer;
late final TextEditingController _searchCtrl;
@override
void initState() {
super.initState();
_searchCtrl = TextEditingController();
}
@override
void dispose() {
_timer?.cancel();
_searchCtrl.dispose();
super.dispose();
}
void _onSearchChanged(String v) {
_timer?.cancel();
_timer = Timer(const Duration(milliseconds: 350), () {
if (v.isEmpty) {
widget.app.refreshSessions();
} else {
widget.app.searchSessions(v);
}
});
}
@override
Widget build(BuildContext context) {
final app = widget.app;
return ListenableBuilder(
listenable: app,
builder: (context, _) {
return Scaffold(
appBar: AppBar(
title: const Text('会话'),
leading: IconButton(
icon: const Icon(Icons.dns_outlined),
tooltip: '服务器',
onPressed: _instancesSheet,
),
actions: [
IconButton(
icon: Icon(_searchOpen ? Icons.close : Icons.search),
tooltip: '搜索',
onPressed: () {
setState(() {
_searchOpen = !_searchOpen;
if (!_searchOpen) {
_searchCtrl.clear();
app.refreshSessions();
}
});
},
),
IconButton(
icon: const Icon(Icons.add),
tooltip: '新建会话',
onPressed: _newSessionSheet,
),
],
),
body: Column(
children: [
if (_searchOpen)
Padding(
padding: const EdgeInsets.fromLTRB(16, 4, 16, 4),
child: TextField(
controller: _searchCtrl,
autofocus: true,
onChanged: _onSearchChanged,
decoration: const InputDecoration(
hintText: '搜索会话',
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(),
isDense: true,
),
),
),
if (app.error != null)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
children: [
Expanded(
child: Text(app.error!,
style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 13)),
),
TextButton(onPressed: app.refreshSessions, child: const Text('重试')),
],
),
),
Expanded(child: _body()),
],
),
);
},
);
}
Widget _body() {
final app = widget.app;
if (app.sessionsLoading && app.sessions.isEmpty) {
return const Center(child: CircularProgressIndicator());
}
if (app.sessions.isEmpty) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('暂无会话'),
const SizedBox(height: 8),
FilledButton(onPressed: _newSessionSheet, child: const Text('新建会话')),
],
),
);
}
return RefreshIndicator(
onRefresh: app.refreshSessions,
child: ListView.builder(
itemCount: app.sessions.length,
itemBuilder: (context, i) {
final s = app.sessions[i];
final running = app.active.contains(s.id);
return ListTile(
title: Text(s.title.isEmpty ? '(无标题会话)' : s.title,
maxLines: 1, overflow: TextOverflow.ellipsis),
subtitle: Text(_subtitle(s),
maxLines: 1, overflow: TextOverflow.ellipsis),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (running)
Padding(
padding: const EdgeInsets.only(right: 8),
child: Icon(Icons.circle, size: 10, color: Theme.of(context).colorScheme.primary),
),
PopupMenuButton<String>(
onSelected: (v) => _sessionAction(s, v),
itemBuilder: (_) => const [
PopupMenuItem(value: 'rename', child: Text('重命名')),
PopupMenuItem(value: 'archive', child: Text('归档')),
PopupMenuItem(value: 'delete', child: Text('删除')),
],
),
],
),
onTap: () {
app.openChat(s);
Navigator.push(
context,
MaterialPageRoute(builder: (_) => ChatScreen(app: app, session: s)),
);
},
);
},
),
);
}
String _subtitle(Session s) {
final parts = <String>[];
if (s.agent.isNotEmpty) parts.add(s.agent);
if (s.model != null) parts.add(widget.app.modelName(s.model));
if (s.updated > 0) {
final t = DateTime.fromMillisecondsSinceEpoch(s.updated);
parts.add('${t.month.toString().padLeft(2, '0')}-${t.day.toString().padLeft(2, '0')} '
'${t.hour.toString().padLeft(2, '0')}:${t.minute.toString().padLeft(2, '0')}');
}
return parts.join(' · ');
}
void _sessionAction(Session s, String v) {
switch (v) {
case 'rename':
_rename(s);
break;
case 'archive':
widget.app.client?.updateSession(s.id, archived: true);
break;
case 'delete':
_confirmDelete(s);
break;
}
}
void _rename(Session s) {
final ctrl = TextEditingController(text: s.title);
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('重命名'),
content: TextField(controller: ctrl, autofocus: true),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')),
FilledButton(
onPressed: () {
Navigator.pop(context);
widget.app.client?.updateSession(s.id, title: ctrl.text);
},
child: const Text('保存'),
),
],
),
);
}
void _confirmDelete(Session s) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('删除会话?'),
content: Text('“${s.title}”及其子会话将被删除,且无法恢复。'),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')),
FilledButton(
onPressed: () {
Navigator.pop(context);
widget.app.client?.deleteSession(s.id);
widget.app.sessions.removeWhere((x) => x.id == s.id);
widget.app.touch();
},
child: const Text('删除'),
),
],
),
);
}
void _newSessionSheet() {
final ctrl = TextEditingController();
showModalBottomSheet(
context: context,
builder: (context) => Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 24),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text('新建会话', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
const SizedBox(height: 12),
TextField(
controller: ctrl,
autofocus: true,
decoration: const InputDecoration(hintText: '标题(可选)', border: OutlineInputBorder()),
),
const SizedBox(height: 12),
FilledButton(
onPressed: () async {
Navigator.pop(context);
final s = await widget.app.client?.createSession(
title: ctrl.text, directory: widget.app.directory);
if (s != null) {
widget.app.sessions.insert(0, s);
widget.app.touch();
widget.app.openChat(s);
if (mounted) {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => ChatScreen(app: widget.app, session: s)),
);
}
}
},
child: const Text('创建'),
),
],
),
),
);
}
void _instancesSheet() {
final app = widget.app;
showModalBottomSheet(
context: context,
builder: (context) => SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Padding(
padding: EdgeInsets.all(12),
child: Text('服务器', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
),
for (final c in app.connections) ...[
ListTile(
title: Text(c.label),
subtitle: Text(c.url),
trailing: app.conn?.id == c.id
? Text('已连接', style: TextStyle(color: Theme.of(context).colorScheme.primary))
: null,
onTap: () {
Navigator.pop(context);
if (app.conn?.id != c.id) app.switchTo(c);
},
),
],
ListTile(
leading: const Icon(Icons.add),
title: const Text('添加服务器'),
onTap: () => Navigator.pop(context),
),
if (app.client != null)
ListTile(
leading: const Icon(Icons.link_off),
title: const Text('断开当前连接'),
onTap: () {
Navigator.pop(context);
app.disconnect();
},
),
const SizedBox(height: 8),
],
),
),
);
}
}