import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../../core/pairing.dart'; import '../../core/pairing_input.dart'; class PairingPasteDialog extends StatefulWidget { const PairingPasteDialog( {super.key, this.readClipboard = readPairingClipboard, this.decodeImage}); final Future Function() readClipboard; final Future Function(Uint8List)? decodeImage; @override State createState() => _PairingPasteDialogState(); } class _PairingPasteDialogState extends State { PairingClipboardContent? _content; bool _busy = false; String? _error; Future _paste() async { if (_busy) return; setState(() { _busy = true; _error = null; }); try { final content = await widget.readClipboard(); if (mounted) setState(() => _content = content); } on Object catch (error) { if (mounted) setState(() { _content = null; _error = _message(error); }); } finally { if (mounted) setState(() => _busy = false); } } String _message(Object error) => error is FormatException ? error.message : '读取或识别失败,请重新复制后重试'; Future _confirm() async { final content = _content; if (_busy || content == null) return; setState(() { _busy = true; _error = null; }); try { final image = content.image; final PairingUri pairing; if (image != null) { pairing = await (widget.decodeImage?.call(image) ?? compute(parsePairingImage, image)); } else { pairing = parsePairingText(content.text ?? ''); } if (mounted) Navigator.of(context).pop(pairing); } on Object catch (error) { if (mounted) setState(() => _error = _message(error)); } finally { if (mounted) setState(() => _busy = false); } } @override Widget build(BuildContext context) { final image = _content?.image; return CallbackShortcuts( bindings: { const SingleActivator(LogicalKeyboardKey.keyV, control: true): _paste, const SingleActivator(LogicalKeyboardKey.keyV, meta: true): _paste, }, child: Focus( autofocus: true, child: AlertDialog( title: const Text('扫码配对'), content: SizedBox( width: 480, child: SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Text('按 Ctrl+V 粘贴配对文本或二维码图片。核对预览后,点击确认配对进行识别和校验。'), const SizedBox(height: 16), Container( constraints: const BoxConstraints(minHeight: 140, maxHeight: 260), padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: image == null ? null : Colors.white, border: Border.all( color: Theme.of(context).colorScheme.outline), borderRadius: BorderRadius.circular(8)), child: image != null ? Image.memory(image, fit: BoxFit.contain, cacheWidth: 640, errorBuilder: (_, __, ___) => const Text('图片预览不可用,可点击确认尝试识别或重新粘贴')) : SingleChildScrollView( child: Text(_content?.text ?? '等待粘贴…')), ), if (_error != null) Padding( padding: const EdgeInsets.only(top: 12), child: Text(_error!, style: TextStyle( color: Theme.of(context).colorScheme.error))), if (_busy) const Padding( padding: EdgeInsets.only(top: 12), child: LinearProgressIndicator()), ]), ), ), actions: [ TextButton( onPressed: _busy ? null : () => Navigator.of(context).pop(), child: const Text('取消')), TextButton( onPressed: _busy ? null : _paste, child: const Text('粘贴')), FilledButton( onPressed: _busy || _content == null ? null : _confirm, child: const Text('确认配对')), ], ), ), ); } }