import 'dart:io'; import 'dart:ui' as ui; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:path_provider/path_provider.dart'; import 'package:share_plus/share_plus.dart'; import '../models/target_style.dart'; import '../theme/app_theme.dart'; import '../utils/format.dart'; import 'bar_list.dart'; import 'target_face.dart'; import 'ui.dart'; /// Everything the share image shows, taken from the result screen as it is. class ResultShareData { final String title; // "Trening 18" or "Tarča 3" final String subtitle; // date, counts final int total; final double? averagePerShot; final TargetStyle style; final List hits; // in target space final List<(String? top, String value, bool filled)> tiles; final String chartTitle; final List bars; final double barMax; final String Function(String) t; const ResultShareData({ required this.title, required this.subtitle, required this.total, required this.averagePerShot, required this.style, required this.hits, required this.tiles, required this.chartTitle, required this.bars, required this.barMax, required this.t, }); } bool _sharing = false; /// Builds the result image off screen and opens the phone's share menu with it. Future shareResult(BuildContext context, ResultShareData data) async { if (_sharing) return; // double tap _sharing = true; final overlay = Overlay.of(context); final messenger = ScaffoldMessenger.maybeOf(context); final key = GlobalKey(); OverlayEntry? entry; try { // Decode the target first so it is in the very first frame final asset = data.style.asset; if (asset != null) await precacheImage(AssetImage(asset), context); // Laid out and painted, but outside the screen entry = OverlayEntry( builder: (_) => Positioned( left: -ResultShareCard.size.width - 100, top: 0, child: IgnorePointer( child: RepaintBoundary(key: key, child: ResultShareCard(data: data)), ), ), ); overlay.insert(entry); await WidgetsBinding.instance.endOfFrame; final render = key.currentContext!.findRenderObject() as RenderRepaintBoundary; if (render.debugNeedsPaint) await WidgetsBinding.instance.endOfFrame; // Card is 960x540 logical: pixel ratio 2 gives a 1920x1080 image final image = await render.toImage(pixelRatio: 2); final png = await image.toByteData(format: ui.ImageByteFormat.png); image.dispose(); entry.remove(); entry = null; final dir = await getTemporaryDirectory(); final file = File('${dir.path}/sdk_kovinar_${DateTime.now().millisecondsSinceEpoch}.png'); await file.writeAsBytes(png!.buffer.asUint8List()); await SharePlus.instance.share(ShareParams( files: [XFile(file.path, mimeType: 'image/png')], text: '${data.title} · ${data.total}', )); } catch (e) { debugPrint('Share failed: $e'); messenger?.showSnackBar(SnackBar(content: Text(data.t('Sharing failed')))); } finally { entry?.remove(); _sharing = false; } } /// The 960×540 image: the result in the header, the target with its tiles on /// the left, the bar chart with every value on the right. class ResultShareCard extends StatelessWidget { static const Size size = Size(960, 540); final ResultShareData data; const ResultShareCard({super.key, required this.data}); @override Widget build(BuildContext context) { final c = Palette.current; final t = data.t; return SizedBox.fromSize( size: size, child: Material( color: c.bg, child: Stack( children: [ // Faint rings behind the header, like a target in the corner Positioned( right: -260, top: -330, child: CustomPaint(size: const Size(700, 700), painter: _Rings(c.line2)), ), Padding( padding: const EdgeInsets.fromLTRB(32, 26, 32, 32), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Row( crossAxisAlignment: CrossAxisAlignment.end, children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('SD KOVINAR MUTA', style: TextStyle(color: c.accent, fontSize: 12, fontWeight: FontWeight.w800, letterSpacing: 2.2)), const SizedBox(height: 4), Text(data.title, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(color: c.ink, fontSize: 30, fontWeight: FontWeight.w700, height: 1.15)), const SizedBox(height: 4), Text(data.subtitle, style: TextStyle(color: c.muted, fontSize: 14)), ], ), ), if (data.averagePerShot != null) _HeaderFigure(label: t('Average per shot'), value: dec(data.averagePerShot!), color: c.ink), const SizedBox(width: 40), _HeaderFigure(label: t('Total'), value: '${data.total}', color: c.accent), ], ), const SizedBox(height: 20), Expanded( child: Row( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ SizedBox( width: 400, child: AppCard( padding: const EdgeInsets.fromLTRB(14, 10, 14, 14), child: Column( children: [ Expanded( child: Center( child: TargetFace(style: data.style, hits: data.hits, hitColor: c.accent), ), ), const SizedBox(height: 10), Row( children: [ for (int i = 0; i < data.tiles.length; i++) ...[ if (i > 0) const SizedBox(width: 6), Expanded( child: ScoreTile( top: data.tiles[i].$1, value: data.tiles[i].$2, filled: data.tiles[i].$3, height: 60, ), ), ], ], ), ], ), ), ), const SizedBox(width: 20), Expanded( child: AppCard( padding: const EdgeInsets.fromLTRB(20, 20, 20, 16), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Kicker(data.chartTitle, padding: const EdgeInsets.only(bottom: 14)), Expanded( child: BarList( entries: data.bars, max: data.barMax, ticks: [0, data.barMax / 2, data.barMax], labelWidth: 36, valueWidth: 56, scale: 1.1, ), ), ], ), ), ), ], ), ), ], ), ), ], ), ), ); } } class _Rings extends CustomPainter { final Color color; const _Rings(this.color); @override void paint(Canvas canvas, Size size) { final p = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.5 ..color = color; for (double r = 80; r < size.width / 2; r += 55) { canvas.drawCircle(size.center(Offset.zero), r, p); } } @override bool shouldRepaint(_Rings old) => old.color != color; } class _HeaderFigure extends StatelessWidget { final String label; final String value; final Color color; const _HeaderFigure({required this.label, required this.value, required this.color}); @override Widget build(BuildContext context) { final c = Palette.current; return Column( crossAxisAlignment: CrossAxisAlignment.end, mainAxisSize: MainAxisSize.min, children: [ Text(label.toUpperCase(), style: AppTheme.kicker(c).copyWith(letterSpacing: 1.5)), const SizedBox(height: 4), Text(value, style: TextStyle( color: color, fontSize: 44, height: 1.05, fontWeight: FontWeight.w800, fontFeatures: AppTheme.tnum)), ], ); } }