更新pubspec。修改启动屏幕的启动后台XML,并通过删除过时的文件和增强会话管理来重构记录功能。
This commit is contained in:
704
lib/features/recording/pages/page_record.dart
Normal file
704
lib/features/recording/pages/page_record.dart
Normal file
@@ -0,0 +1,704 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:recording_tool/core/platform/app_platform_info.dart';
|
||||
import 'package:recording_tool/core/platform/device_health_checker.dart';
|
||||
import 'package:recording_tool/core/utils/date_time_formatter.dart';
|
||||
import 'package:recording_tool/features/recording/model/model_recording.dart';
|
||||
import 'package:recording_tool/features/recording/model/model_recording_session.dart';
|
||||
import 'package:recording_tool/features/recording/recording_display_name.dart';
|
||||
import 'package:recording_tool/features/recording/recording_platform.dart';
|
||||
import 'package:recording_tool/features/recording/view-model/view_model_recording.dart';
|
||||
import 'package:recording_tool/features/recording/widgets/camera_preview_widget.dart';
|
||||
import 'package:recording_tool/features/recording/widgets/recording_saved_dialog.dart';
|
||||
import 'package:recording_tool/features/recording/widgets/recording_touch_lock_overlay.dart';
|
||||
import 'package:recording_tool/shared/widgets/widgets.dart';
|
||||
|
||||
/// 录制页入口
|
||||
class RecordingPage extends ConsumerStatefulWidget {
|
||||
const RecordingPage({super.key});
|
||||
|
||||
@override
|
||||
/// 创建页面状态
|
||||
ConsumerState<RecordingPage> createState() => _RecordingPageState();
|
||||
}
|
||||
|
||||
class _RecordingPageState extends ConsumerState<RecordingPage> {
|
||||
var _immersiveApplied = false;
|
||||
|
||||
@override
|
||||
/// 首帧后初始化录制流程
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _bootstrap());
|
||||
}
|
||||
|
||||
/// 检查设备健康状态并弹窗提示
|
||||
Future<void> _checkAndShowDeviceHealthAlerts() async {
|
||||
final snapshot = await AppPlatformInfo.deviceHealth();
|
||||
if (!mounted) return;
|
||||
|
||||
final lines = DeviceHealthChecker.warningLines(snapshot);
|
||||
if (lines.isEmpty) return;
|
||||
|
||||
await AppDialog.deviceHealthAlert(context, lines: lines);
|
||||
}
|
||||
|
||||
/// 页面启动:健康检查、读剪贴板、进入录制模式、准备相机会话
|
||||
Future<void> _bootstrap() async {
|
||||
await _checkAndShowDeviceHealthAlerts();
|
||||
if (!mounted) return;
|
||||
|
||||
final clipboardResult = await ref
|
||||
.read(recordingViewModelProvider.notifier)
|
||||
.getClipboardContent();
|
||||
if (!mounted) return;
|
||||
if (clipboardResult == ClipboardReadResult.invalid) {
|
||||
AppToast.show('无选手信息');
|
||||
}
|
||||
await _enterRecordingMode();
|
||||
// Allow PlatformView to attach before binding CameraX preview.
|
||||
await Future<void>.delayed(const Duration(milliseconds: 400));
|
||||
if (!mounted) return;
|
||||
await ref.read(recordingViewModelProvider.notifier).prepareSession();
|
||||
}
|
||||
|
||||
/// Android 进入沉浸式全屏
|
||||
Future<void> _enterRecordingMode() async {
|
||||
if (!Platform.isAndroid) return;
|
||||
await SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
|
||||
await RecordingPlatform.setImmersiveMode(enabled: true);
|
||||
_immersiveApplied = true;
|
||||
}
|
||||
|
||||
/// 解析保存成功弹窗的标题文案
|
||||
String _savedDialogSessionTitle(
|
||||
RecordingModel recordingInfo,
|
||||
String? savedName,
|
||||
) {
|
||||
final clipboard = recordingInfo.clipboardRecordingModel;
|
||||
if (recordingInfo.hasValidClipboardInfo &&
|
||||
clipboard.title.trim().isNotEmpty) {
|
||||
return clipboard.title.trim();
|
||||
}
|
||||
if (savedName != null && savedName.isNotEmpty) {
|
||||
return resolveRecordingDisplayName(savedName);
|
||||
}
|
||||
return '录制完成';
|
||||
}
|
||||
|
||||
/// 无选手信息时弹窗提示
|
||||
Future<void> _showNoPlayerInfoDialog() {
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
content: const Text('无选手信息'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(),
|
||||
child: const Text('确定'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 点击开始录制:校验剪贴板与健康状态
|
||||
Future<void> _onStartRecording() async {
|
||||
final recordingInfo = ref.read(recordingViewModelProvider);
|
||||
if (!recordingInfo.hasClipboardFilename) {
|
||||
await _showNoPlayerInfoDialog();
|
||||
return;
|
||||
}
|
||||
await _checkAndShowDeviceHealthAlerts();
|
||||
if (!mounted) return;
|
||||
await ref.read(recordingViewModelProvider.notifier).startRecording();
|
||||
}
|
||||
|
||||
/// 清空剪贴板信息,准备新一轮录制
|
||||
void _clearClipboardForNewRound() {
|
||||
final notifier = ref.read(recordingViewModelProvider.notifier);
|
||||
notifier.resetClipboardInfo();
|
||||
notifier.clearSavedRecordingResult();
|
||||
}
|
||||
|
||||
/// 保存成功后按需弹出完成对话框
|
||||
Future<void> _showRecordingSavedDialogIfNeeded() async {
|
||||
final recordingInfo = ref.read(recordingViewModelProvider);
|
||||
final session = recordingInfo.session;
|
||||
if (session.lastSavedDisplayName == null || session.gallerySaveFailed) {
|
||||
return;
|
||||
}
|
||||
|
||||
final sessionTitle = _savedDialogSessionTitle(
|
||||
recordingInfo,
|
||||
session.lastSavedDisplayName,
|
||||
);
|
||||
|
||||
await showRecordingSavedDialog(
|
||||
context,
|
||||
sessionTitle: sessionTitle,
|
||||
onContinueRound: () {
|
||||
ref
|
||||
.read(recordingViewModelProvider.notifier)
|
||||
.clearSavedRecordingResult();
|
||||
},
|
||||
onRecordNewRound: _clearClipboardForNewRound,
|
||||
);
|
||||
}
|
||||
|
||||
/// 退出沉浸式并释放录制会话
|
||||
Future<void> _exitRecordingMode() async {
|
||||
if (!_immersiveApplied) return;
|
||||
await ref.read(recordingViewModelProvider.notifier).teardown();
|
||||
await SystemChrome.setEnabledSystemUIMode(
|
||||
SystemUiMode.manual,
|
||||
overlays: SystemUiOverlay.values,
|
||||
);
|
||||
await RecordingPlatform.setImmersiveMode(enabled: false);
|
||||
_immersiveApplied = false;
|
||||
}
|
||||
|
||||
@override
|
||||
/// 页面销毁时恢复系统 UI
|
||||
void dispose() {
|
||||
if (_immersiveApplied) {
|
||||
SystemChrome.setEnabledSystemUIMode(
|
||||
SystemUiMode.manual,
|
||||
overlays: SystemUiOverlay.values,
|
||||
);
|
||||
RecordingPlatform.setImmersiveMode(enabled: false);
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
/// 构建录制页 UI
|
||||
Widget build(BuildContext context) {
|
||||
final recordingInfo = ref.watch(recordingViewModelProvider);
|
||||
final state = recordingInfo.session;
|
||||
final viewModel = ref.read(recordingViewModelProvider.notifier);
|
||||
final clipboard = recordingInfo.clipboardRecordingModel;
|
||||
final showClipboardInfo = recordingInfo.hasValidClipboardInfo;
|
||||
|
||||
return PopScope(
|
||||
canPop: !state.isRecording,
|
||||
onPopInvokedWithResult: (didPop, result) async {
|
||||
if (didPop) {
|
||||
await _exitRecordingMode();
|
||||
return;
|
||||
}
|
||||
if (state.isRecording) {
|
||||
AppToast.show('录制中无法返回,请先停止录制');
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
const CameraPreviewWidget(),
|
||||
if (!state.isPreviewReady && state.errorMessage == null)
|
||||
const _RecordingLoadingOverlay(message: '正在启动相机…'),
|
||||
if (state.isTouchLocked && state.isRecording)
|
||||
RecordingTouchLockOverlay(
|
||||
enabled: true,
|
||||
onUnlocked: () => viewModel.setTouchLocked(false),
|
||||
),
|
||||
_RecordingHud(
|
||||
state: state,
|
||||
eventTitle: showClipboardInfo ? clipboard.title : null,
|
||||
eventAddress: showClipboardInfo ? clipboard.address : null,
|
||||
showClipboardHint: showClipboardInfo,
|
||||
clipboardAddress: clipboard.address.trim(),
|
||||
onClearEventInfo: _clearClipboardForNewRound,
|
||||
onPasteEventInfo: () async {
|
||||
final result = await ref
|
||||
.read(recordingViewModelProvider.notifier)
|
||||
.getClipboardContent();
|
||||
if (!context.mounted) return;
|
||||
if (result != ClipboardReadResult.success) {
|
||||
AppToast.show('无赛事信息');
|
||||
}
|
||||
},
|
||||
onStart: _onStartRecording,
|
||||
onStop: () async {
|
||||
await viewModel.stopRecording();
|
||||
if (!context.mounted) return;
|
||||
final latest = ref.read(recordingViewModelProvider).session;
|
||||
if (latest.gallerySaveFailed) {
|
||||
AppToast.show(latest.errorMessage ?? '保存到相册失败,请开启相册权限');
|
||||
return;
|
||||
}
|
||||
await _showRecordingSavedDialogIfNeeded();
|
||||
},
|
||||
onOpenDnd: () async {
|
||||
await viewModel.openDndSettings();
|
||||
await viewModel.refreshDndAccess();
|
||||
},
|
||||
onOpenBattery: () async {
|
||||
await viewModel.openBatterySettings();
|
||||
await viewModel.refreshBatteryOptimization();
|
||||
},
|
||||
onToggleTouchLock: () {
|
||||
viewModel.setTouchLocked(!state.isTouchLocked);
|
||||
},
|
||||
),
|
||||
if (state.isStartingRecording)
|
||||
const _RecordingLoadingOverlay(message: '正在开始录制…'),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 录制加载遮罩(相机启动/开始录制)
|
||||
class _RecordingLoadingOverlay extends StatelessWidget {
|
||||
const _RecordingLoadingOverlay({required this.message});
|
||||
|
||||
final String message;
|
||||
|
||||
@override
|
||||
/// 显示加载动画与提示文案
|
||||
Widget build(BuildContext context) {
|
||||
return ColoredBox(
|
||||
color: Colors.black,
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SizedBox.square(
|
||||
dimension: 32.r,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2.5.r,
|
||||
color: Colors.white70,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 14.h),
|
||||
Text(
|
||||
message,
|
||||
style: TextStyle(color: Colors.white70, fontSize: 14.sp),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 录制页 HUD 层(赛事信息、控制按钮、状态提示)
|
||||
class _RecordingHud extends StatelessWidget {
|
||||
const _RecordingHud({
|
||||
required this.state,
|
||||
this.eventTitle,
|
||||
this.eventAddress,
|
||||
this.showClipboardHint = false,
|
||||
this.clipboardAddress = '',
|
||||
required this.onClearEventInfo,
|
||||
required this.onPasteEventInfo,
|
||||
required this.onStart,
|
||||
required this.onStop,
|
||||
required this.onOpenDnd,
|
||||
required this.onOpenBattery,
|
||||
required this.onToggleTouchLock,
|
||||
});
|
||||
|
||||
final RecordingSessionState state;
|
||||
final String? eventTitle;
|
||||
final String? eventAddress;
|
||||
final bool showClipboardHint;
|
||||
final String clipboardAddress;
|
||||
final VoidCallback onClearEventInfo;
|
||||
final Future<void> Function() onPasteEventInfo;
|
||||
final Future<void> Function() onStart;
|
||||
final Future<void> Function() onStop;
|
||||
final VoidCallback onOpenDnd;
|
||||
final VoidCallback onOpenBattery;
|
||||
final VoidCallback onToggleTouchLock;
|
||||
|
||||
/// 叠加层文字样式
|
||||
static TextStyle get _overlayTextStyle => TextStyle(
|
||||
color: Colors.white,
|
||||
shadows: [Shadow(color: Colors.black54, blurRadius: 6.r)],
|
||||
);
|
||||
|
||||
/// 底部控制区左右占位宽度
|
||||
static double get _controlSlotWidth => 48.r;
|
||||
|
||||
@override
|
||||
/// 构建 HUD 布局
|
||||
Widget build(BuildContext context) {
|
||||
final showPasteEventInfo = eventTitle == null && !state.isRecording;
|
||||
|
||||
return SafeArea(
|
||||
child: Stack(
|
||||
children: [
|
||||
Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
height:
|
||||
eventTitle != null ||
|
||||
state.isRecording ||
|
||||
showPasteEventInfo
|
||||
? 56.h
|
||||
: 8.h,
|
||||
),
|
||||
const Spacer(),
|
||||
if (state.errorMessage != null)
|
||||
Padding(
|
||||
padding: EdgeInsets.all(12.r),
|
||||
child: Text(
|
||||
state.errorMessage!,
|
||||
style: const TextStyle(color: Colors.amber),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
if (state.permissionWarning != null)
|
||||
Padding(
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: 16.r,
|
||||
vertical: 8.r,
|
||||
),
|
||||
child: Text(
|
||||
state.permissionWarning!,
|
||||
style: TextStyle(
|
||||
color: Colors.orangeAccent,
|
||||
fontSize: 12.sp,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
_SetupHints(
|
||||
hasDndAccess: state.hasDndAccess,
|
||||
isBatteryIgnored: state.isBatteryOptimizedIgnored,
|
||||
notificationsGranted: state.notificationsGranted,
|
||||
showClipboardHint: showClipboardHint,
|
||||
clipboardAddress: clipboardAddress,
|
||||
onOpenDnd: onOpenDnd,
|
||||
onOpenBattery: onOpenBattery,
|
||||
onOpenNotificationSettings: openAppSettings,
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.fromLTRB(24.r, 8.r, 24.r, 24.r),
|
||||
child: Row(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: _controlSlotWidth,
|
||||
height: _controlSlotWidth,
|
||||
child: state.isRecording
|
||||
? IconButton(
|
||||
onPressed: onToggleTouchLock,
|
||||
icon: Icon(
|
||||
state.isTouchLocked
|
||||
? Icons.lock
|
||||
: Icons.lock_open,
|
||||
color: Colors.white,
|
||||
size: 28.r,
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
Expanded(
|
||||
child: Center(
|
||||
child: GestureDetector(
|
||||
onTap: state.isStartingRecording
|
||||
? null
|
||||
: () async {
|
||||
if (state.isRecording) {
|
||||
await onStop();
|
||||
} else {
|
||||
await onStart();
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
width: 76.w,
|
||||
height: 76.h,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(
|
||||
color: Colors.white,
|
||||
width: 4.r,
|
||||
),
|
||||
color: state.isRecording
|
||||
? Colors.white
|
||||
: Colors.red,
|
||||
),
|
||||
child: Icon(
|
||||
state.isRecording
|
||||
? Icons.stop
|
||||
: Icons.fiber_manual_record,
|
||||
color: state.isRecording
|
||||
? Colors.red
|
||||
: Colors.white,
|
||||
size: 36.r,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
width: _controlSlotWidth,
|
||||
height: _controlSlotWidth,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (showPasteEventInfo)
|
||||
Positioned(
|
||||
top: 8.r,
|
||||
left: 12.w,
|
||||
right: 12.w,
|
||||
child: Center(
|
||||
child: TextButton.icon(
|
||||
onPressed: onPasteEventInfo,
|
||||
icon: Icon(Icons.content_paste, size: 18.r),
|
||||
label: const Text('粘贴赛事信息'),
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: Colors.white,
|
||||
backgroundColor: Colors.black.withValues(alpha: 0.5),
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: 14.r,
|
||||
vertical: 8.r,
|
||||
),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20.r),
|
||||
side: const BorderSide(color: Colors.white30),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (eventTitle != null)
|
||||
Positioned(
|
||||
top: 8.r,
|
||||
left: 12.w,
|
||||
right: 12.w,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.only(right: state.isRecording ? 96.w : 0),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
eventTitle!,
|
||||
style: _overlayTextStyle.copyWith(
|
||||
fontSize: 16.sp,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
if (!state.isRecording)
|
||||
IconButton(
|
||||
onPressed: onClearEventInfo,
|
||||
icon: Icon(
|
||||
Icons.delete_outline,
|
||||
color: Colors.white,
|
||||
size: 22.r,
|
||||
),
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: BoxConstraints(
|
||||
minWidth: 40.r,
|
||||
minHeight: 40.r,
|
||||
),
|
||||
tooltip: '删除',
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
if (state.isRecording)
|
||||
Positioned(
|
||||
top: 8.r,
|
||||
right: 12.w,
|
||||
child: Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: 12.r, vertical: 6.r),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red,
|
||||
borderRadius: BorderRadius.circular(20.r),
|
||||
),
|
||||
child: Text(
|
||||
'REC ${state.elapsedLabel}',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
// if (eventAddress != null && eventAddress!.isNotEmpty)
|
||||
// Positioned(
|
||||
// left: 16.w,
|
||||
// bottom: 108.r,
|
||||
// right: 120.w,
|
||||
// child: Text(
|
||||
// eventAddress!,
|
||||
// style: _overlayTextStyle.copyWith(
|
||||
// fontSize: 13.sp,
|
||||
// color: Colors.white70,
|
||||
// ),
|
||||
// maxLines: 2,
|
||||
// overflow: TextOverflow.ellipsis,
|
||||
// ),
|
||||
// ),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 权限与剪贴板相关设置提示条
|
||||
class _SetupHints extends StatelessWidget {
|
||||
const _SetupHints({
|
||||
required this.hasDndAccess,
|
||||
required this.isBatteryIgnored,
|
||||
required this.notificationsGranted,
|
||||
this.showClipboardHint = false,
|
||||
this.clipboardAddress = '',
|
||||
required this.onOpenDnd,
|
||||
required this.onOpenBattery,
|
||||
required this.onOpenNotificationSettings,
|
||||
});
|
||||
|
||||
final bool hasDndAccess;
|
||||
final bool isBatteryIgnored;
|
||||
final bool notificationsGranted;
|
||||
final bool showClipboardHint;
|
||||
final String clipboardAddress;
|
||||
final VoidCallback onOpenDnd;
|
||||
final VoidCallback onOpenBattery;
|
||||
final VoidCallback onOpenNotificationSettings;
|
||||
|
||||
@override
|
||||
/// 按需展示权限/剪贴板提示
|
||||
Widget build(BuildContext context) {
|
||||
final showPermissionHints =
|
||||
!hasDndAccess || !isBatteryIgnored || !notificationsGranted;
|
||||
final showClipboardHint = this.showClipboardHint;
|
||||
if (!showPermissionHints && !showClipboardHint) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 16.r, vertical: 8.r),
|
||||
child: Column(
|
||||
children: [
|
||||
if (!notificationsGranted) ...[
|
||||
_HintChip(
|
||||
label: '开启通知权限以显示录制前台服务',
|
||||
onTap: onOpenNotificationSettings,
|
||||
),
|
||||
SizedBox(height: 8.h),
|
||||
],
|
||||
if (!hasDndAccess)
|
||||
_HintChip(label: '开启勿扰权限可减少录制中断', onTap: onOpenDnd),
|
||||
if (!isBatteryIgnored) ...[
|
||||
SizedBox(height: 8.h),
|
||||
_HintChip(label: '关闭电池优化可提升息屏续录稳定性', onTap: onOpenBattery),
|
||||
],
|
||||
if (showClipboardHint) ...[
|
||||
SizedBox(height: 8.h),
|
||||
_ClipboardAddressClockChip(address: clipboardAddress),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 显示剪贴板地址与实时时钟的提示芯片
|
||||
class _ClipboardAddressClockChip extends StatefulWidget {
|
||||
const _ClipboardAddressClockChip({required this.address});
|
||||
|
||||
final String address;
|
||||
|
||||
@override
|
||||
/// 创建芯片状态
|
||||
State<_ClipboardAddressClockChip> createState() =>
|
||||
_ClipboardAddressClockChipState();
|
||||
}
|
||||
|
||||
class _ClipboardAddressClockChipState
|
||||
extends State<_ClipboardAddressClockChip> {
|
||||
Timer? _clockTimer;
|
||||
|
||||
@override
|
||||
/// 启动每秒刷新时钟
|
||||
void initState() {
|
||||
super.initState();
|
||||
_clockTimer = Timer.periodic(const Duration(seconds: 1), (_) {
|
||||
if (mounted) setState(() {});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
/// 取消定时器
|
||||
void dispose() {
|
||||
_clockTimer?.cancel();
|
||||
_clockTimer = null;
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// 拼接地址与当前时间文本
|
||||
String _buildLabel() {
|
||||
final nowText = DateTimeFormatter.format(
|
||||
DateTime.now(),
|
||||
pattern: 'yyyy-M-d-H:mm:ss',
|
||||
);
|
||||
if (widget.address.isEmpty) return nowText;
|
||||
return '${widget.address}\n$nowText';
|
||||
}
|
||||
|
||||
@override
|
||||
/// 渲染时钟芯片
|
||||
Widget build(BuildContext context) {
|
||||
return _HintChip(label: _buildLabel(), onTap: () {});
|
||||
}
|
||||
}
|
||||
|
||||
/// 可点击的提示条组件
|
||||
class _HintChip extends StatelessWidget {
|
||||
const _HintChip({required this.label, required this.onTap});
|
||||
|
||||
final String label;
|
||||
final VoidCallback onTap;
|
||||
|
||||
@override
|
||||
/// 构建提示条 UI
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: onTap,
|
||||
child: DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white12,
|
||||
borderRadius: BorderRadius.circular(8.r),
|
||||
),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 12.r, vertical: 8.r),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
label,
|
||||
style: TextStyle(color: Colors.white70, fontSize: 12.sp),
|
||||
),
|
||||
),
|
||||
Icon(Icons.chevron_right, color: Colors.white54, size: 18.r),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user