发布网络质量探测 1.1.2:更新客户端界面与版本文件
署名:言灸
This commit is contained in:
1 parent
3a6356f4d9
commit
c1a60eef43
4 files changed
+415
-67
No files matched your search
@@ -4,6 +4,7 @@ import 'dart:io';
|
||||
|
||||
import '../metrics/sample_book.dart';
|
||||
import '../models/session_config.dart';
|
||||
import '../ui/address_text.dart';
|
||||
|
||||
class RemoteProbe {
|
||||
HttpClient? _client;
|
||||
@@ -16,6 +17,7 @@ class RemoteProbe {
|
||||
required ProbeBody body,
|
||||
required void Function(ProbeSample sample) onSample,
|
||||
required bool Function() cancelled,
|
||||
void Function(List<ResolvedAddress> resolved)? onResolved,
|
||||
}) async {
|
||||
final normalized = normalizeNodeUrl(baseUrl);
|
||||
if (normalized == null) {
|
||||
@@ -52,6 +54,7 @@ class RemoteProbe {
|
||||
throw RemoteException('节点返回的任务无效');
|
||||
}
|
||||
_jobId = parsed['jobId'] as String;
|
||||
onResolved?.call(readResolvedList(parsed));
|
||||
if (cancelled()) {
|
||||
return;
|
||||
}
|
||||
@@ -86,7 +89,13 @@ class RemoteProbe {
|
||||
_jobId = null;
|
||||
if (client != null && id != null && base != null) {
|
||||
try {
|
||||
await _send(client, 'DELETE', base.resolve('/v1/jobs/$id'), nodeToken, const []);
|
||||
await _send(
|
||||
client,
|
||||
'DELETE',
|
||||
base.resolve('/v1/jobs/$id'),
|
||||
nodeToken,
|
||||
const [],
|
||||
);
|
||||
} on Object {
|
||||
// The stream ending already asks the node to drop the job.
|
||||
}
|
||||
@@ -101,7 +110,9 @@ class RemoteProbe {
|
||||
void Function(ProbeSample sample) onSample,
|
||||
bool Function() cancelled,
|
||||
) async {
|
||||
final request = await client.getUrl(_base!.resolve('/v1/jobs/$_jobId/stream'));
|
||||
final request = await client.getUrl(
|
||||
_base!.resolve('/v1/jobs/$_jobId/stream'),
|
||||
);
|
||||
request.followRedirects = false;
|
||||
_auth(request, nodeToken);
|
||||
final response = await request.close().timeout(const Duration(seconds: 20));
|
||||
@@ -194,7 +205,9 @@ Future<NodeInfo> fetchNodeInfo(String baseUrl) async {
|
||||
}
|
||||
final client = HttpClient();
|
||||
try {
|
||||
final request = await client.getUrl(Uri.parse(normalized).resolve('/v1/info'));
|
||||
final request = await client.getUrl(
|
||||
Uri.parse(normalized).resolve('/v1/info'),
|
||||
);
|
||||
request.followRedirects = false;
|
||||
final response = await request.close().timeout(const Duration(seconds: 8));
|
||||
final body = await response.transform(utf8.decoder).join();
|
||||
@@ -206,7 +219,9 @@ Future<NodeInfo> fetchNodeInfo(String baseUrl) async {
|
||||
throw RemoteException('节点信息无效');
|
||||
}
|
||||
return NodeInfo(
|
||||
visibility: json['visibility'] is String ? json['visibility'] as String : '',
|
||||
visibility: json['visibility'] is String
|
||||
? json['visibility'] as String
|
||||
: '',
|
||||
authRequired: json['authRequired'] == true,
|
||||
);
|
||||
} on RemoteException {
|
||||
|
||||
+269
-46
@@ -2,6 +2,8 @@ import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../models/node_edit.dart';
|
||||
import '../models/port_field.dart';
|
||||
import '../models/probe_profile.dart';
|
||||
import '../models/session_config.dart';
|
||||
import '../probe/endpoint_picker.dart';
|
||||
@@ -9,7 +11,11 @@ import '../probe/probe_controller.dart';
|
||||
import '../probe/remote_client.dart';
|
||||
import '../storage/app_store.dart';
|
||||
import '../storage/token_vault.dart';
|
||||
import 'address_text.dart';
|
||||
import 'format.dart';
|
||||
import 'history_page.dart';
|
||||
import 'metric_guide_page.dart';
|
||||
import 'metrics_panel.dart';
|
||||
import 'quality_charts.dart';
|
||||
|
||||
class HomePage extends StatefulWidget {
|
||||
@@ -134,7 +140,11 @@ class _HomePageState extends State<HomePage> {
|
||||
final nodeToken = config.node == null
|
||||
? ''
|
||||
: (await _vault.read(nodeTokenKey(config.node!.id)) ?? '');
|
||||
await _controller.start(config, nodeToken: nodeToken, echoToken: _echo.text);
|
||||
await _controller.start(
|
||||
config,
|
||||
nodeToken: nodeToken,
|
||||
echoToken: _echo.text,
|
||||
);
|
||||
}
|
||||
|
||||
void _changeProfile(String? id) {
|
||||
@@ -144,9 +154,11 @@ class _HomePageState extends State<HomePage> {
|
||||
final previous = ProbeProfile.byId(_profileId);
|
||||
final next = ProbeProfile.byId(id);
|
||||
setState(() {
|
||||
if (_port.text.trim() == '${previous.port}') {
|
||||
_port.text = '${next.port}';
|
||||
}
|
||||
_port.text = portAfterProfileChange(
|
||||
current: _port.text,
|
||||
previousDefault: previous.port,
|
||||
nextDefault: next.port,
|
||||
);
|
||||
_profileId = id;
|
||||
});
|
||||
}
|
||||
@@ -160,6 +172,22 @@ class _HomePageState extends State<HomePage> {
|
||||
appBar: AppBar(
|
||||
title: const Text('网络质量探测'),
|
||||
backgroundColor: const Color(0xFFF3F1EC),
|
||||
actions: [
|
||||
PopupMenuButton<String>(
|
||||
tooltip: '说明',
|
||||
onSelected: (value) {
|
||||
final page = value == 'history'
|
||||
? const HistoryPage()
|
||||
: const MetricGuidePage();
|
||||
Navigator.of(context)
|
||||
.push(MaterialPageRoute<void>(builder: (_) => page));
|
||||
},
|
||||
itemBuilder: (context) => const [
|
||||
PopupMenuItem(value: 'metrics', child: Text('指标说明')),
|
||||
PopupMenuItem(value: 'history', child: Text('更新历史')),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
child: Align(
|
||||
@@ -167,12 +195,14 @@ class _HomePageState extends State<HomePage> {
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 880),
|
||||
child: ListView(
|
||||
padding: EdgeInsets.fromLTRB(wide ? 24 : 12, 8, wide ? 24 : 12, 28),
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
wide ? 24 : 12,
|
||||
8,
|
||||
wide ? 24 : 12,
|
||||
28,
|
||||
),
|
||||
children: [
|
||||
_card(
|
||||
title: '探测来源',
|
||||
child: _source(view, wide),
|
||||
),
|
||||
_card(title: '探测来源', child: _source(view, wide)),
|
||||
_card(
|
||||
title: '目标',
|
||||
child: Column(
|
||||
@@ -181,7 +211,13 @@ class _HomePageState extends State<HomePage> {
|
||||
_pair(
|
||||
wide,
|
||||
_field(_host, '目标地址', '域名或 IP'),
|
||||
_field(_port, '端口', '1–65535'),
|
||||
_field(
|
||||
_port,
|
||||
'端口',
|
||||
portHint(profile),
|
||||
fieldKey: const Key('port-field'),
|
||||
helper: portHelperText,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
DropdownButtonFormField<String>(
|
||||
@@ -190,12 +226,18 @@ class _HomePageState extends State<HomePage> {
|
||||
decoration: const InputDecoration(labelText: '探测类型'),
|
||||
items: [
|
||||
for (final item in ProbeProfile.all)
|
||||
DropdownMenuItem(value: item.id, child: Text(item.label)),
|
||||
DropdownMenuItem(
|
||||
value: item.id,
|
||||
child: Text(item.label),
|
||||
),
|
||||
],
|
||||
onChanged: view.running ? null : _changeProfile,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(profile.blurb, style: const TextStyle(color: muted, fontSize: 13)),
|
||||
Text(
|
||||
profile.blurb,
|
||||
style: const TextStyle(color: muted, fontSize: 13),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -206,7 +248,11 @@ class _HomePageState extends State<HomePage> {
|
||||
children: [
|
||||
_pair(
|
||||
wide,
|
||||
_field(_interval, '间隔(毫秒)', _remote ? '至少 200' : '至少 100'),
|
||||
_field(
|
||||
_interval,
|
||||
'间隔(毫秒)',
|
||||
_remote ? '至少 200' : '至少 100',
|
||||
),
|
||||
_field(_timeout, '超时(毫秒)', '100–15000'),
|
||||
),
|
||||
if (profile.needsPacketSize) ...[
|
||||
@@ -252,11 +298,44 @@ class _HomePageState extends State<HomePage> {
|
||||
_banner(view.banner!),
|
||||
],
|
||||
const SizedBox(height: 12),
|
||||
_card(title: '结果', child: _result(view)),
|
||||
_card(
|
||||
title: '结果',
|
||||
child: _result(
|
||||
view,
|
||||
view.profileId.isEmpty ? profile.id : view.profileId,
|
||||
),
|
||||
),
|
||||
if (view.report != null)
|
||||
SessionSummaryCard(
|
||||
report: view.report!,
|
||||
profileId: view.report!.profileId,
|
||||
),
|
||||
QualityCharts(
|
||||
samples: view.samples,
|
||||
rateLabel: view.rateLabel,
|
||||
running: view.running,
|
||||
sessionId: view.sessionId,
|
||||
),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (_) => const MetricGuidePage(),
|
||||
),
|
||||
),
|
||||
child: const Text('指标说明'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (_) => const HistoryPage(),
|
||||
),
|
||||
),
|
||||
child: const Text('更新历史'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const Text(
|
||||
'延迟用发包一侧的单调时钟计算。远程探测时,这个时间在节点上量好再传回来,不包含手机到节点的控制通道耗时。TCP 超时不是物理丢包。没有协议应答的 UDP 记为无应答/未知,不会在探测完成前显示 100%。',
|
||||
@@ -271,17 +350,25 @@ class _HomePageState extends State<HomePage> {
|
||||
}
|
||||
|
||||
Widget _source(ProbeView view, bool wide) {
|
||||
final selected = _nodes.cast<RemoteNode?>().followedBy(const [null]).firstWhere(
|
||||
(node) => node?.id == _nodeId,
|
||||
orElse: () => null,
|
||||
);
|
||||
final selected = _nodes
|
||||
.cast<RemoteNode?>()
|
||||
.followedBy(const [null])
|
||||
.firstWhere((node) => node?.id == _nodeId, orElse: () => null);
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
SegmentedButton<bool>(
|
||||
segments: const [
|
||||
ButtonSegment(value: false, label: Text('本机'), icon: Icon(Icons.phone_android)),
|
||||
ButtonSegment(value: true, label: Text('远程节点'), icon: Icon(Icons.dns_outlined)),
|
||||
ButtonSegment(
|
||||
value: false,
|
||||
label: Text('本机'),
|
||||
icon: Icon(Icons.phone_android),
|
||||
),
|
||||
ButtonSegment(
|
||||
value: true,
|
||||
label: Text('远程节点'),
|
||||
icon: Icon(Icons.dns_outlined),
|
||||
),
|
||||
],
|
||||
selected: {_remote},
|
||||
onSelectionChanged: view.running
|
||||
@@ -290,9 +377,7 @@ class _HomePageState extends State<HomePage> {
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
_remote
|
||||
? '由选中的节点向目标发包,这台设备只负责控制和显示。'
|
||||
: '由这台设备直接向目标发包。可以探测本机上的反射器。',
|
||||
_remote ? '由选中的节点向目标发包,这台设备只负责控制和显示。' : '由这台设备直接向目标发包。可以探测本机上的反射器。',
|
||||
style: const TextStyle(color: muted, fontSize: 13),
|
||||
),
|
||||
if (_remote) ...[
|
||||
@@ -338,13 +423,18 @@ class _HomePageState extends State<HomePage> {
|
||||
return '${node.name} · $vis';
|
||||
}
|
||||
|
||||
Widget _result(ProbeView view) {
|
||||
Widget _result(ProbeView view, String profileId) {
|
||||
final summary = view.summary;
|
||||
final rate = view.rateLabel == '探测无应答'
|
||||
? summary.probeLossPct
|
||||
: view.rateLabel == '失败率'
|
||||
? summary.failurePct
|
||||
: summary.unknownPct;
|
||||
final address = describeAddress(
|
||||
resolved: view.resolved,
|
||||
usedAddress: view.usedAddress,
|
||||
usedFamily: view.usedFamily,
|
||||
tryingAddress: view.tryingAddress,
|
||||
tryingFamily: view.tryingFamily,
|
||||
fallbackText: view.fallbackText,
|
||||
addressUnreported: view.addressUnreported,
|
||||
usedIsFake: view.usedIsFake,
|
||||
);
|
||||
final motd = view.detail['motd'];
|
||||
final version = view.detail['version'];
|
||||
final players = view.detail['players'];
|
||||
@@ -357,13 +447,42 @@ class _HomePageState extends State<HomePage> {
|
||||
children: [
|
||||
_chip(view.phase),
|
||||
_chip('来源 ${view.sourceLabel}'),
|
||||
if (view.targetAddress != null) _chip('发往 ${view.targetAddress}'),
|
||||
_chip('延迟 ${formatMs(summary.rttMs)}'),
|
||||
_chip('抖动 ${formatMs(summary.jitterMs)}'),
|
||||
_chip('${view.rateLabel} ${formatPct(rate)}'),
|
||||
if (view.targetHost.isNotEmpty) _chip(view.targetHost),
|
||||
],
|
||||
),
|
||||
if (view.targetIsFake) ...[
|
||||
const SizedBox(height: 8),
|
||||
MetricsPanel(
|
||||
summary: summary,
|
||||
profileId: profileId,
|
||||
elapsed: view.elapsed,
|
||||
),
|
||||
if (address.trying != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(address.trying!, style: const TextStyle(color: ink)),
|
||||
],
|
||||
if (address.used != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(address.used!, style: const TextStyle(color: ink)),
|
||||
],
|
||||
if (address.resolved != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
address.resolved!,
|
||||
style: const TextStyle(color: muted, fontSize: 13),
|
||||
),
|
||||
],
|
||||
if (address.unreported != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
address.unreported!,
|
||||
style: const TextStyle(color: Color(0xFF9A3412)),
|
||||
),
|
||||
],
|
||||
if (address.fallback != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(address.fallback!, style: const TextStyle(color: ink)),
|
||||
],
|
||||
if (address.fake) ...[
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
clashFakeIpNote,
|
||||
@@ -386,7 +505,10 @@ class _HomePageState extends State<HomePage> {
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 8),
|
||||
Text(summaryLine(summary), style: const TextStyle(color: muted, fontSize: 13)),
|
||||
Text(
|
||||
summaryLine(summary),
|
||||
style: const TextStyle(color: muted, fontSize: 13),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -434,6 +556,7 @@ class _NodeDialogState extends State<_NodeDialog> {
|
||||
final _name = TextEditingController();
|
||||
final _url = TextEditingController();
|
||||
final _token = TextEditingController();
|
||||
String? _editingId;
|
||||
String? _hint;
|
||||
|
||||
@override
|
||||
@@ -450,6 +573,41 @@ class _NodeDialogState extends State<_NodeDialog> {
|
||||
setState(() => _hint = '请填写名称,以及不带口令的 http 或 https 地址');
|
||||
return;
|
||||
}
|
||||
final editing = _editingId;
|
||||
if (editing != null) {
|
||||
RemoteNode? previous;
|
||||
for (final node in _nodes) {
|
||||
if (node.id == editing) {
|
||||
previous = node;
|
||||
}
|
||||
}
|
||||
if (previous == null) {
|
||||
return;
|
||||
}
|
||||
final next = applyNodeEdit(
|
||||
previous,
|
||||
name: _name.text.trim(),
|
||||
baseUrl: url,
|
||||
);
|
||||
final replaced = await saveEditedNodeToken(
|
||||
widget.vault,
|
||||
editing,
|
||||
_token.text,
|
||||
);
|
||||
setState(() {
|
||||
_nodes = [
|
||||
for (final node in _nodes)
|
||||
if (node.id == editing) next else node,
|
||||
];
|
||||
_editingId = null;
|
||||
_hint = replaced ? '已更新,口令已替换,仍只留在本机。' : '已更新。口令留空,原来的口令未改。';
|
||||
_name.clear();
|
||||
_url.clear();
|
||||
_token.clear();
|
||||
});
|
||||
widget.onChanged(_nodes, editing);
|
||||
return;
|
||||
}
|
||||
final id = DateTime.now().microsecondsSinceEpoch.toString();
|
||||
final node = RemoteNode(id: id, name: _name.text.trim(), baseUrl: url);
|
||||
await widget.vault.write(nodeTokenKey(id), _token.text.trim());
|
||||
@@ -463,6 +621,26 @@ class _NodeDialogState extends State<_NodeDialog> {
|
||||
widget.onChanged(_nodes, id);
|
||||
}
|
||||
|
||||
void _beginEdit(RemoteNode node) {
|
||||
setState(() {
|
||||
_editingId = node.id;
|
||||
_name.text = node.name;
|
||||
_url.text = node.baseUrl;
|
||||
_token.clear();
|
||||
_hint = '正在修改「${node.name}」。口令留空则保留已保存的口令,不会显示原口令。';
|
||||
});
|
||||
}
|
||||
|
||||
void _cancelEdit() {
|
||||
setState(() {
|
||||
_editingId = null;
|
||||
_name.clear();
|
||||
_url.clear();
|
||||
_token.clear();
|
||||
_hint = null;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _check(RemoteNode node) async {
|
||||
try {
|
||||
final info = await fetchNodeInfo(node.baseUrl);
|
||||
@@ -487,7 +665,10 @@ class _NodeDialogState extends State<_NodeDialog> {
|
||||
|
||||
Future<void> _delete(RemoteNode node) async {
|
||||
await widget.vault.delete(nodeTokenKey(node.id));
|
||||
final next = [for (final item in _nodes) if (item.id != node.id) item];
|
||||
final next = [
|
||||
for (final item in _nodes)
|
||||
if (item.id != node.id) item,
|
||||
];
|
||||
setState(() => _nodes = next);
|
||||
widget.onChanged(next, null);
|
||||
}
|
||||
@@ -510,22 +691,41 @@ class _NodeDialogState extends State<_NodeDialog> {
|
||||
for (final node in _nodes)
|
||||
ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text(_nodes.isEmpty ? '' : '${node.name} · ${visibilityLabel(node.visibility)}'),
|
||||
subtitle: Text(node.baseUrl, maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||
title: Text(
|
||||
'${node.name} · ${visibilityLabel(node.visibility)}',
|
||||
),
|
||||
subtitle: Text(
|
||||
node.baseUrl,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: Wrap(
|
||||
spacing: 4,
|
||||
children: [
|
||||
TextButton(onPressed: () => _check(node), child: const Text('检查')),
|
||||
TextButton(onPressed: () => _delete(node), child: const Text('删除')),
|
||||
TextButton(
|
||||
key: Key('edit-node-${node.id}'),
|
||||
onPressed: () => _beginEdit(node),
|
||||
child: const Text('编辑'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => _check(node),
|
||||
child: const Text('检查'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => _delete(node),
|
||||
child: const Text('删除'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
TextField(
|
||||
key: const Key('node-name'),
|
||||
controller: _name,
|
||||
decoration: const InputDecoration(labelText: '名称'),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextField(
|
||||
key: const Key('node-url'),
|
||||
controller: _url,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '节点地址',
|
||||
@@ -534,10 +734,14 @@ class _NodeDialogState extends State<_NodeDialog> {
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextField(
|
||||
key: const Key('node-token'),
|
||||
controller: _token,
|
||||
obscureText: true,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '节点口令(公开节点留空)',
|
||||
decoration: InputDecoration(
|
||||
labelText: _editingId == null ? '节点口令(公开节点留空)' : '新口令(留空则保留)',
|
||||
helperText: _editingId == null
|
||||
? null
|
||||
: '留空则保留已保存的口令,不会显示原口令。',
|
||||
),
|
||||
),
|
||||
if (_hint != null) ...[
|
||||
@@ -549,8 +753,17 @@ class _NodeDialogState extends State<_NodeDialog> {
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(onPressed: _save, child: const Text('添加节点')),
|
||||
TextButton(onPressed: () => Navigator.pop(context), child: const Text('完成')),
|
||||
if (_editingId != null)
|
||||
TextButton(onPressed: _cancelEdit, child: const Text('取消编辑')),
|
||||
TextButton(
|
||||
key: const Key('node-save'),
|
||||
onPressed: _save,
|
||||
child: Text(_editingId == null ? '添加节点' : '保存修改'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('完成'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -568,7 +781,10 @@ Widget _card({required String title, required Widget child}) {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(title, style: const TextStyle(fontWeight: FontWeight.w700, color: ink)),
|
||||
Text(
|
||||
title,
|
||||
style: const TextStyle(fontWeight: FontWeight.w700, color: ink),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
child,
|
||||
],
|
||||
@@ -582,11 +798,18 @@ Widget _field(
|
||||
String label,
|
||||
String hint, {
|
||||
bool secret = false,
|
||||
Key? fieldKey,
|
||||
String? helper,
|
||||
}) {
|
||||
return TextField(
|
||||
key: fieldKey,
|
||||
controller: controller,
|
||||
obscureText: secret,
|
||||
decoration: InputDecoration(labelText: label, hintText: hint),
|
||||
decoration: InputDecoration(
|
||||
labelText: label,
|
||||
hintText: hint,
|
||||
helperText: helper,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../probe/probe_controller.dart';
|
||||
import 'chart_selection.dart';
|
||||
import 'format.dart';
|
||||
|
||||
const rttColor = Color(0xFF1F4E79);
|
||||
const rateColor = Color(0xFF9A3412);
|
||||
@@ -9,20 +11,67 @@ const ink = Color(0xFF1C2430);
|
||||
const muted = Color(0xFF5E6872);
|
||||
const line = Color(0xFFE4DFD4);
|
||||
|
||||
class QualityCharts extends StatelessWidget {
|
||||
class QualityCharts extends StatefulWidget {
|
||||
const QualityCharts({
|
||||
super.key,
|
||||
required this.samples,
|
||||
required this.rateLabel,
|
||||
required this.running,
|
||||
required this.sessionId,
|
||||
});
|
||||
|
||||
final List<ChartSample> samples;
|
||||
final String rateLabel;
|
||||
final bool running;
|
||||
final int sessionId;
|
||||
|
||||
@override
|
||||
State<QualityCharts> createState() => _QualityChartsState();
|
||||
}
|
||||
|
||||
class _QualityChartsState extends State<QualityCharts> {
|
||||
ChartSelection? _pin;
|
||||
|
||||
@override
|
||||
void didUpdateWidget(QualityCharts oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
final kept = nextChartSelection(
|
||||
current: _pin,
|
||||
sessionId: widget.sessionId,
|
||||
sampleSecs: [for (final sample in widget.samples) sample.sec],
|
||||
);
|
||||
if (kept?.sec != _pin?.sec || kept?.sessionId != _pin?.sessionId) {
|
||||
_pin = kept;
|
||||
}
|
||||
}
|
||||
|
||||
void _pinAt(double sec) {
|
||||
setState(() {
|
||||
_pin = nextChartSelection(
|
||||
current: _pin,
|
||||
sessionId: widget.sessionId,
|
||||
sampleSecs: [for (final sample in widget.samples) sample.sec],
|
||||
tappedSec: sec,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
ChartSample? get _pinnedSample {
|
||||
final pin = _pin;
|
||||
if (pin == null || pin.sessionId != widget.sessionId) {
|
||||
return null;
|
||||
}
|
||||
for (final sample in widget.samples) {
|
||||
if ((sample.sec - pin.sec).abs() <= chartPinEpsilon) {
|
||||
return sample;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final pinned = _pinnedSample;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
@@ -34,29 +83,51 @@ class QualityCharts extends StatelessWidget {
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
pinned == null
|
||||
? '点曲线上的一点可以钉住该时刻,松手后仍会保留。再点另一点会更换。'
|
||||
: pinnedReadout(pinned, widget.rateLabel),
|
||||
key: const Key('chart-pin'),
|
||||
style: const TextStyle(color: ink, fontSize: 13, height: 1.4),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
_ChartCard(
|
||||
title: '延迟',
|
||||
unit: 'ms',
|
||||
samples: samples,
|
||||
samples: widget.samples,
|
||||
read: (sample) => sample.rttMs,
|
||||
color: rttColor,
|
||||
floor: 1,
|
||||
running: running,
|
||||
running: widget.running,
|
||||
pinnedSec: pinned?.sec,
|
||||
onPin: _pinAt,
|
||||
),
|
||||
_ChartCard(
|
||||
title: rateLabel,
|
||||
title: widget.rateLabel,
|
||||
unit: '%',
|
||||
samples: samples,
|
||||
samples: widget.samples,
|
||||
read: (sample) => sample.ratePct,
|
||||
color: rateColor,
|
||||
floor: 10,
|
||||
running: running,
|
||||
running: widget.running,
|
||||
pinnedSec: pinned?.sec,
|
||||
onPin: _pinAt,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String pinnedReadout(ChartSample sample, String rateLabel) {
|
||||
final latency = sample.rttMs == null
|
||||
? '这一刻没有延迟'
|
||||
: '延迟 ${formatMs(sample.rttMs)}';
|
||||
final rate = sample.ratePct == null
|
||||
? '$rateLabel —'
|
||||
: '$rateLabel ${formatPct(sample.ratePct)}';
|
||||
return '已选 ${sample.sec.toStringAsFixed(1)} 秒 · $latency · $rate';
|
||||
}
|
||||
|
||||
class _Swatch extends StatelessWidget {
|
||||
const _Swatch({required this.color, required this.label});
|
||||
|
||||
@@ -92,6 +163,8 @@ class _ChartCard extends StatelessWidget {
|
||||
required this.color,
|
||||
required this.floor,
|
||||
required this.running,
|
||||
required this.pinnedSec,
|
||||
required this.onPin,
|
||||
});
|
||||
|
||||
final String title;
|
||||
@@ -101,6 +174,8 @@ class _ChartCard extends StatelessWidget {
|
||||
final Color color;
|
||||
final double floor;
|
||||
final bool running;
|
||||
final double? pinnedSec;
|
||||
final void Function(double sec) onPin;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -132,9 +207,7 @@ class _ChartCard extends StatelessWidget {
|
||||
? _lineChart(spots)
|
||||
: Center(
|
||||
child: Text(
|
||||
running
|
||||
? '正在等待第一次完成的探测。未完成的不会画成 100%。'
|
||||
: '暂无已完成的探测',
|
||||
running ? '正在等待第一次完成的探测。未完成的不会画成 100%。' : '暂无已完成的探测',
|
||||
style: const TextStyle(color: muted),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
@@ -190,7 +263,8 @@ class _ChartCard extends StatelessWidget {
|
||||
show: true,
|
||||
drawVerticalLine: false,
|
||||
horizontalInterval: yInterval,
|
||||
getDrawingHorizontalLine: (_) => const FlLine(color: line, strokeWidth: 1),
|
||||
getDrawingHorizontalLine: (_) =>
|
||||
const FlLine(color: line, strokeWidth: 1),
|
||||
),
|
||||
borderData: FlBorderData(
|
||||
show: true,
|
||||
@@ -200,14 +274,19 @@ class _ChartCard extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
titlesData: FlTitlesData(
|
||||
topTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)),
|
||||
rightTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)),
|
||||
topTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: const AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(
|
||||
showTitles: true,
|
||||
reservedSize: 40,
|
||||
interval: yInterval,
|
||||
getTitlesWidget: (value, meta) => _axisLabel(_formatTick(value, maxY)),
|
||||
getTitlesWidget: (value, meta) =>
|
||||
_axisLabel(_formatTick(value, maxY)),
|
||||
),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
@@ -220,15 +299,46 @@ class _ChartCard extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
),
|
||||
lineTouchData: const LineTouchData(handleBuiltInTouches: true),
|
||||
extraLinesData: ExtraLinesData(
|
||||
verticalLines: [
|
||||
if (pinnedSec != null)
|
||||
VerticalLine(
|
||||
x: pinnedSec!,
|
||||
color: color.withValues(alpha: 0.85),
|
||||
strokeWidth: 1.5,
|
||||
),
|
||||
],
|
||||
),
|
||||
lineTouchData: LineTouchData(
|
||||
handleBuiltInTouches: false,
|
||||
touchSpotThreshold: 18,
|
||||
touchCallback: (event, response) {
|
||||
if (event is! FlTapUpEvent) {
|
||||
return;
|
||||
}
|
||||
final spots = response?.lineBarSpots;
|
||||
if (spots == null || spots.isEmpty) {
|
||||
return;
|
||||
}
|
||||
onPin(spots.first.x);
|
||||
},
|
||||
),
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
spots: spots,
|
||||
isCurved: false,
|
||||
color: color,
|
||||
barWidth: 2,
|
||||
dotData: const FlDotData(show: false),
|
||||
belowBarData: BarAreaData(show: true, color: color.withValues(alpha: 0.10)),
|
||||
dotData: FlDotData(
|
||||
show: pinnedSec != null,
|
||||
checkToShowDot: (spot, _) =>
|
||||
pinnedSec != null &&
|
||||
(spot.x - pinnedSec!).abs() <= chartPinEpsilon,
|
||||
),
|
||||
belowBarData: BarAreaData(
|
||||
show: true,
|
||||
color: color.withValues(alpha: 0.10),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user