- 新增 PlatformCapabilities 平台能力判断类,统一检测移动端原生插件可用性 - 地图、定位、扫码、相册、拨号、通话录制等入口在桌面端降级为提示 - 系统 UI 配置与 moveTaskToBack 在桌面端跳过,避免插件异常 - 设备信息工具新增 Windows 平台支持,返回 computerName - 百度地图 SDK 初始化增加平台判断,桌面端不再调用 - 新增个人资料编辑页面及路由,从「我的」页头部入口跳转 - 国际化添加 featureUnsupported 提示文案
512 lines
18 KiB
Dart
512 lines
18 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:cupertino_ui/cupertino_ui.dart';
|
||
import 'package:flutter_webrtc/flutter_webrtc.dart';
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
import 'package:material_ui/material_ui.dart';
|
||
import 'package:ttstd_family_care/core/widgets/app_page_scaffold.dart';
|
||
|
||
import '../../../core/platform/platform_capabilities.dart';
|
||
import '../../device/presentation/device_switcher.dart';
|
||
import '../data/connection_providers.dart';
|
||
import '../domain/connection_models.dart' as connection;
|
||
import 'connection_controller.dart';
|
||
|
||
/// 远程控制页面:实时观看老人设备屏幕,并通过手势向设备下发控制指令。
|
||
///
|
||
/// 使用说明:
|
||
/// - 单指点击 = 点击
|
||
/// - 长按 = 长按
|
||
/// - 单指拖动 = 滑动
|
||
/// 坐标归一化为 0.0~1.0(相对屏幕),由被控端还原像素。
|
||
class RemoteControlPage extends ConsumerStatefulWidget {
|
||
final String sn;
|
||
|
||
const RemoteControlPage({super.key, required this.sn});
|
||
|
||
@override
|
||
ConsumerState<RemoteControlPage> createState() => _RemoteControlPageState();
|
||
}
|
||
|
||
class _RemoteControlPageState extends ConsumerState<RemoteControlPage> {
|
||
/// 录制状态条是否可见。
|
||
bool _showRecordStatus = false;
|
||
Timer? _recordStatusTimer;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
final controller = ref.read(connectionControllerProvider.notifier);
|
||
controller.targetSn = widget.sn;
|
||
// 等一帧,确保 provider 已就绪
|
||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
controller.connect();
|
||
});
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_recordStatusTimer?.cancel();
|
||
super.dispose();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final session = ref.watch(connectionControllerProvider);
|
||
final renderer = ref.read(connectionControllerProvider.notifier).remoteRenderer;
|
||
final connected = session.state == connection.ConnectionState.connected;
|
||
|
||
// 监听录制状态文字:录制中一直显示;非录制时更新后显示,3 秒后自动隐藏。
|
||
ref.listen(
|
||
connectionControllerProvider.select((s) => s.recordStatus),
|
||
(previous, next) {
|
||
if (next.isEmpty) {
|
||
_recordStatusTimer?.cancel();
|
||
if (_showRecordStatus) {
|
||
setState(() => _showRecordStatus = false);
|
||
}
|
||
} else {
|
||
_recordStatusTimer?.cancel();
|
||
setState(() => _showRecordStatus = true);
|
||
// 录制中保持常显;非录制(如保存提示)3 秒后自动隐藏。
|
||
final recording = ref.read(connectionControllerProvider).recording;
|
||
if (!recording) {
|
||
_recordStatusTimer = Timer(const Duration(seconds: 3), () {
|
||
if (mounted) setState(() => _showRecordStatus = false);
|
||
});
|
||
}
|
||
}
|
||
},
|
||
);
|
||
|
||
// 用 PopScope 统一处理所有退出入口(自带返回箭头 / 系统返回键):
|
||
// 无论从哪个入口离开页面,都在 pop 后断开远程连接。
|
||
return PopScope(
|
||
canPop: true,
|
||
onPopInvokedWithResult: (didPop, _) {
|
||
if (didPop) {
|
||
ref.read(connectionControllerProvider.notifier).disconnect();
|
||
}
|
||
},
|
||
child: AppPageScaffold(
|
||
navigationBar: CupertinoNavigationBar(
|
||
middle: const DeviceContextTitle('远程控制'),
|
||
trailing: CupertinoButton(
|
||
padding: EdgeInsets.zero,
|
||
onPressed: () => _showMoreMenu(session),
|
||
child: const Icon(CupertinoIcons.ellipsis, size: 22),
|
||
),
|
||
),
|
||
child: SafeArea(
|
||
child: Stack(
|
||
children: [
|
||
Column(
|
||
children: [
|
||
Expanded(
|
||
child: _buildVideo(renderer, connected, session),
|
||
),
|
||
_buildControlsBar(session),
|
||
],
|
||
),
|
||
// 悬浮录制状态提示:叠加在视频区顶部,自适应宽度居中,不占用布局空间。
|
||
if (_showRecordStatus && session.recordStatus.isNotEmpty)
|
||
Positioned(
|
||
top: 8,
|
||
left: 0,
|
||
right: 0,
|
||
child: IgnorePointer(
|
||
child: Align(
|
||
alignment: Alignment.topCenter,
|
||
child: _buildRecordStatusBar(session),
|
||
),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _buildVideo(
|
||
RTCVideoRenderer? renderer,
|
||
bool connected,
|
||
ConnectionSessionState session,
|
||
) {
|
||
if (session.state == connection.ConnectionState.connecting) {
|
||
return const Center(child: CupertinoActivityIndicator());
|
||
}
|
||
if (session.state == connection.ConnectionState.error) {
|
||
return Center(
|
||
child: Column(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
const Icon(CupertinoIcons.exclamationmark_circle,
|
||
color: CupertinoColors.systemRed, size: 48),
|
||
const SizedBox(height: 12),
|
||
Text(session.errorMessage ?? '连接失败', textAlign: TextAlign.center),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
if (!connected || renderer == null) {
|
||
return const Center(child: Text('正在等待画面...'));
|
||
}
|
||
// 远程画面:用 LayoutBuilder 获取视频区域真实尺寸(坐标基准),
|
||
// 用 Listener 捕获原始指针事件(避免 GestureDetector 手势竞争导致坐标错乱)。
|
||
return LayoutBuilder(
|
||
builder: (context, constraints) {
|
||
final vw = constraints.maxWidth;
|
||
final vh = constraints.maxHeight;
|
||
return Listener(
|
||
behavior: HitTestBehavior.opaque,
|
||
onPointerDown: (e) => _onPointerDown(e.localPosition, vw, vh),
|
||
onPointerMove: (e) => _onPointerMove(e.localPosition, vw, vh),
|
||
onPointerUp: (e) => _onPointerUp(e.localPosition, vw, vh),
|
||
onPointerCancel: (e) => _onPointerCancel(),
|
||
child: Container(
|
||
color: Colors.black,
|
||
alignment: Alignment.center,
|
||
child: RTCVideoView(
|
||
renderer,
|
||
objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain,
|
||
),
|
||
),
|
||
);
|
||
},
|
||
);
|
||
}
|
||
|
||
// ---- 基于 Listener 的手势处理(参考源实现 remote_touch_view.dart)----
|
||
|
||
/// 触摸起点(本地坐标)与按下时间。
|
||
Offset? _downPos;
|
||
DateTime? _downTime;
|
||
/// 长按超时(400ms 后视为长按)。
|
||
static const _longPressTimeout = Duration(milliseconds: 400);
|
||
/// 点击判定阈值:相对坐标 2%。
|
||
static const _tapSlop = 0.02;
|
||
/// 实时 MOTION_EVENT 采样节流(16ms)。
|
||
static const _sampleIntervalMs = 16;
|
||
DateTime? _lastMoveSample;
|
||
|
||
/// 归一化:本地坐标 / 视频区域尺寸,clamp 0~1。
|
||
double _rx(double x, double w) => w <= 0 ? 0 : (x / w).clamp(0.0, 1.0);
|
||
double _ry(double y, double h) => h <= 0 ? 0 : (y / h).clamp(0.0, 1.0);
|
||
|
||
void _onPointerDown(Offset local, double vw, double vh) {
|
||
_downPos = local;
|
||
_downTime = DateTime.now();
|
||
_lastMoveSample = null;
|
||
ref.read(connectionControllerProvider.notifier)
|
||
.sendMotionEvent(0, _rx(local.dx, vw), _ry(local.dy, vh)); // ACTION_DOWN
|
||
}
|
||
|
||
void _onPointerMove(Offset local, double vw, double vh) {
|
||
final down = _downPos;
|
||
if (down == null) return;
|
||
// 节流采样,减少指令洪泛
|
||
final now = DateTime.now();
|
||
final last = _lastMoveSample;
|
||
if (last != null && now.difference(last).inMilliseconds < _sampleIntervalMs) return;
|
||
_lastMoveSample = now;
|
||
ref.read(connectionControllerProvider.notifier)
|
||
.sendMotionEvent(2, _rx(local.dx, vw), _ry(local.dy, vh)); // ACTION_MOVE
|
||
}
|
||
|
||
void _onPointerUp(Offset local, double vw, double vh) {
|
||
final down = _downPos;
|
||
final downTime = _downTime;
|
||
_downPos = null;
|
||
_downTime = null;
|
||
_lastMoveSample = null;
|
||
if (down == null || vw <= 0 || vh <= 0) return;
|
||
|
||
final controller = ref.read(connectionControllerProvider.notifier);
|
||
final duration = DateTime.now().difference(downTime ?? DateTime.now());
|
||
final dx = (_rx(local.dx, vw) - _rx(down.dx, vw)).abs();
|
||
final dy = (_ry(local.dy, vh) - _ry(down.dy, vh)).abs();
|
||
|
||
// 先补发 ACTION_UP,结束触摸
|
||
controller.sendMotionEvent(1, _rx(local.dx, vw), _ry(local.dy, vh));
|
||
|
||
// 位移很小且时间短 → 点击;否则 → 滑动;长按由按下时长判定
|
||
if (duration >= _longPressTimeout && dx < _tapSlop && dy < _tapSlop) {
|
||
controller.sendLongPress(_rx(down.dx, vw), _ry(down.dy, vh));
|
||
} else if (dx < _tapSlop && dy < _tapSlop) {
|
||
controller.sendTap(_rx(down.dx, vw), _ry(down.dy, vh));
|
||
} else {
|
||
controller.sendSwipe(
|
||
_rx(down.dx, vw),
|
||
_ry(down.dy, vh),
|
||
_rx(local.dx, vw),
|
||
_ry(local.dy, vh),
|
||
);
|
||
}
|
||
}
|
||
|
||
void _onPointerCancel() {
|
||
_downPos = null;
|
||
_downTime = null;
|
||
_lastMoveSample = null;
|
||
}
|
||
|
||
/// 悬浮录制状态提示条:自适应宽度居中,录制中红色、非录制灰色。
|
||
Widget _buildRecordStatusBar(ConnectionSessionState session) {
|
||
return Container(
|
||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 5),
|
||
decoration: BoxDecoration(
|
||
color: session.recording
|
||
? CupertinoColors.systemRed.withValues(alpha: 0.9)
|
||
: CupertinoColors.systemGrey.withValues(alpha: 0.85),
|
||
borderRadius: BorderRadius.circular(14),
|
||
),
|
||
child: Text(
|
||
session.recordStatus,
|
||
style: const TextStyle(fontSize: 12, color: CupertinoColors.white),
|
||
overflow: TextOverflow.ellipsis,
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _buildControlsBar(ConnectionSessionState session) {
|
||
final connected = session.state == connection.ConnectionState.connected;
|
||
return Container(
|
||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
|
||
decoration: BoxDecoration(
|
||
color: CupertinoColors.systemGroupedBackground,
|
||
border: Border(top: BorderSide(color: CupertinoColors.separator, width: 0.5)),
|
||
),
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
// 单行:返回 / 主页 / 最近
|
||
Row(
|
||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||
children: [
|
||
_CtrlButton(
|
||
icon: CupertinoIcons.arrow_left,
|
||
label: '返回',
|
||
enabled: connected,
|
||
onTap: () => ref.read(connectionControllerProvider.notifier).sendKey(4), // KEYCODE_BACK
|
||
),
|
||
_CtrlButton(
|
||
icon: CupertinoIcons.home,
|
||
label: '主页',
|
||
enabled: connected,
|
||
onTap: () => ref.read(connectionControllerProvider.notifier).sendKey(3), // KEYCODE_HOME
|
||
),
|
||
_CtrlButton(
|
||
icon: CupertinoIcons.rectangle_grid_2x2,
|
||
label: '最近',
|
||
enabled: connected,
|
||
onTap: () => ref.read(connectionControllerProvider.notifier).sendKey(187), // KEYCODE_APP_SWITCH
|
||
),
|
||
],
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
/// 弹出「更多」菜单(iOS 风格 ActionSheet):静音 / 分辨率 / 帧率 / 录制。
|
||
void _showMoreMenu(ConnectionSessionState session) {
|
||
final controller = ref.read(connectionControllerProvider.notifier);
|
||
final recording = session.recording;
|
||
showCupertinoModalPopup<void>(
|
||
context: context,
|
||
builder: (ctx) => CupertinoActionSheet(
|
||
title: const Text('更多操作'),
|
||
actions: [
|
||
CupertinoActionSheetAction(
|
||
onPressed: () {
|
||
Navigator.of(ctx).pop();
|
||
// TODO: 静音切换
|
||
},
|
||
child: const Row(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
Icon(CupertinoIcons.speaker_slash_fill,
|
||
size: 18, color: CupertinoColors.systemGrey),
|
||
SizedBox(width: 8),
|
||
Text('静音'),
|
||
],
|
||
),
|
||
),
|
||
CupertinoActionSheetAction(
|
||
onPressed: () {
|
||
Navigator.of(ctx).pop();
|
||
_showResolutionMenu(session);
|
||
},
|
||
child: Row(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
const Icon(CupertinoIcons.slider_horizontal_3,
|
||
size: 18, color: CupertinoColors.systemGrey),
|
||
const SizedBox(width: 8),
|
||
Text(
|
||
session.resolutionOptions[session.selectedResolution]['label']
|
||
as String,
|
||
),
|
||
],
|
||
),
|
||
),
|
||
CupertinoActionSheetAction(
|
||
onPressed: () {
|
||
Navigator.of(ctx).pop();
|
||
_showFpsMenu(session);
|
||
},
|
||
child: Row(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
const Icon(CupertinoIcons.speedometer,
|
||
size: 18, color: CupertinoColors.systemGrey),
|
||
const SizedBox(width: 8),
|
||
Text(
|
||
session.currentFps > 0 ? '${session.currentFps}fps' : '帧率',
|
||
),
|
||
],
|
||
),
|
||
),
|
||
CupertinoActionSheetAction(
|
||
isDestructiveAction: recording,
|
||
onPressed: () {
|
||
Navigator.of(ctx).pop();
|
||
// 桌面端 WebRTC 未实现本地录制,入口降级为提示。
|
||
if (!PlatformCapabilities.callRecording) {
|
||
PlatformCapabilities.showUnsupported(context);
|
||
return;
|
||
}
|
||
controller.toggleRecord();
|
||
},
|
||
child: Row(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
Icon(
|
||
recording ? CupertinoIcons.stop_circle : CupertinoIcons.video_camera,
|
||
size: 18,
|
||
color: recording
|
||
? CupertinoColors.destructiveRed
|
||
: CupertinoColors.systemGrey,
|
||
),
|
||
const SizedBox(width: 8),
|
||
Text(recording ? '停止录制' : '录制'),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
cancelButton: CupertinoActionSheetAction(
|
||
isDefaultAction: true,
|
||
onPressed: () => Navigator.of(ctx).pop(),
|
||
child: const Text('取消'),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
/// 弹出分辨率选择菜单(iOS 风格 ActionSheet)。
|
||
void _showResolutionMenu(ConnectionSessionState session) {
|
||
final controller = ref.read(connectionControllerProvider.notifier);
|
||
showCupertinoModalPopup<void>(
|
||
context: context,
|
||
builder: (ctx) => CupertinoActionSheet(
|
||
title: const Text('切换分辨率'),
|
||
actions: [
|
||
for (int i = 0; i < session.resolutionOptions.length; i++)
|
||
CupertinoActionSheetAction(
|
||
onPressed: () {
|
||
Navigator.of(ctx).pop();
|
||
controller.selectResolution(i);
|
||
},
|
||
child: Row(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
if (i == session.selectedResolution) ...[
|
||
const Icon(CupertinoIcons.check_mark,
|
||
size: 18, color: CupertinoColors.activeBlue),
|
||
const SizedBox(width: 8),
|
||
],
|
||
Text(session.resolutionOptions[i]['label'] as String),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
cancelButton: CupertinoActionSheetAction(
|
||
isDefaultAction: true,
|
||
onPressed: () => Navigator.of(ctx).pop(),
|
||
child: const Text('取消'),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
/// 弹出帧率选择菜单(iOS 风格 ActionSheet);仅切帧率,分辨率保持不变。
|
||
void _showFpsMenu(ConnectionSessionState session) {
|
||
final controller = ref.read(connectionControllerProvider.notifier);
|
||
showCupertinoModalPopup<void>(
|
||
context: context,
|
||
builder: (ctx) => CupertinoActionSheet(
|
||
title: const Text('切换帧率'),
|
||
actions: [
|
||
for (final fps in session.fpsOptions)
|
||
CupertinoActionSheetAction(
|
||
onPressed: () {
|
||
Navigator.of(ctx).pop();
|
||
controller.selectFps(fps);
|
||
},
|
||
child: Row(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
if (fps == session.currentFps) ...[
|
||
const Icon(CupertinoIcons.check_mark,
|
||
size: 18, color: CupertinoColors.activeBlue),
|
||
const SizedBox(width: 8),
|
||
],
|
||
Text('${fps}fps'),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
cancelButton: CupertinoActionSheetAction(
|
||
isDefaultAction: true,
|
||
onPressed: () => Navigator.of(ctx).pop(),
|
||
child: const Text('取消'),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
class _CtrlButton extends StatelessWidget {
|
||
final IconData icon;
|
||
final String label;
|
||
final bool enabled;
|
||
final VoidCallback onTap;
|
||
|
||
const _CtrlButton({
|
||
required this.icon,
|
||
required this.label,
|
||
required this.enabled,
|
||
required this.onTap,
|
||
});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return GestureDetector(
|
||
onTap: enabled ? onTap : null,
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
Icon(
|
||
icon,
|
||
size: 20,
|
||
color: enabled ? CupertinoColors.activeBlue : CupertinoColors.systemGrey3,
|
||
),
|
||
const SizedBox(height: 4),
|
||
Text(label, style: const TextStyle(fontSize: 12)),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
}
|