import 'package:flutter/material.dart'; import 'package:friendly_map/modules/discovery/discovery.dart'; import 'package:qr_flutter/qr_flutter.dart' show QrImageView; import '../app_scope.dart'; import '../theme.dart'; import '../widgets/async_view.dart'; import '../widgets/common.dart'; /// 내 QR: 상대가 읽으면 내 번호·이름·공개 키가 전해져 바로 친구 요청이 간다. class MyQrScreen extends StatelessWidget { const MyQrScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('내 QR')), body: AsyncView( load: (app) => app.friends.myQr(), builder: (context, q) { if (q == null) return const EmptyNote('내 정보를 먼저 정해 주세요.'); return Center( child: Column(mainAxisSize: MainAxisSize.min, children: [ Container( padding: const EdgeInsets.all(14), decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(16)), child: QrImageView(data: q.encode(), size: 240, backgroundColor: Colors.white), ), const SizedBox(height: 20), Text(q.name, style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w700)), const SizedBox(height: 4), Text(q.phone, style: const TextStyle(color: Pal.muted)), const SizedBox(height: 16), const Padding( padding: EdgeInsets.symmetric(horizontal: 32), child: Text('상대가 Friendly Map 으로 이 QR 을 읽으면 서로 명함을 교환할 수 있어요.', textAlign: TextAlign.center, style: TextStyle(color: Pal.muted, height: 1.5)), ), ]), ); }, ), ); } } /// 상대의 QR 을 카메라로 읽어 친구를 추가한다. class ScanQrScreen extends StatefulWidget { const ScanQrScreen({super.key}); @override State createState() => _ScanQrScreenState(); } class _ScanQrScreenState extends State { bool _handled = false; Future _onCode(String code) async { if (_handled) return; _handled = true; final p = await AppScope.of(context).app.friends.addFromQr(code); if (!mounted) return; if (p == null) { _handled = false; showNote(context, 'Friendly Map 의 QR 이 아니에요.'); return; } showNote(context, '${p.name}님에게 친구 요청을 보냈어요.'); Navigator.pop(context); } @override Widget build(BuildContext context) { final factory = AppScope.of(context).qr; return Scaffold( appBar: AppBar(title: const Text('QR 읽기')), body: factory == null ? const EmptyNote('이 기기에서는 카메라를 쓸 수 없어요.') : factory.build(onCode: _onCode), ); } }