import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../widgets/record_bottom_sheet.dart'; import 'settings_screen.dart'; class HomeScreen extends StatefulWidget { const HomeScreen({super.key}); @override State createState() => _HomeScreenState(); } class _HomeScreenState extends State { static const _channel = MethodChannel('com.snapledger.snapledger/share'); @override void initState() { super.initState(); _checkForSharedData(); _channel.setMethodCallHandler((call) async { if (call.method == 'onShareReceived') { final args = Map.from(call.arguments as Map); _handleSharedData(args); } }); } Future _checkForSharedData() async { try { final data = await _channel.invokeMethod('getSharedData'); if (data != null && data.containsKey('uri')) { _handleSharedData(Map.from(data)); } } on PlatformException { // No shared data } } Future _handleSharedData(Map data) async { final filename = data['filename'] ?? 'upload.jpg'; // Resolve the content URI to a local file // Use MethodChannel to get the actual file path from content URI try { final filePath = await _channel.invokeMethod('getFilePath', {'uri': data['uri']}); if (filePath == null || !File(filePath).existsSync()) return; if (!mounted) return; showModalBottomSheet( context: context, isScrollControlled: true, builder: (_) => RecordBottomSheet( imageFile: File(filePath), originalFilename: filename, ), ); } on PlatformException { // Could not resolve file } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('SnapLedger'), actions: [ IconButton( icon: const Icon(Icons.settings), onPressed: () => Navigator.of(context).push( MaterialPageRoute(builder: (_) => const SettingsScreen()), ), ), ], ), body: Center( child: Padding( padding: const EdgeInsets.all(32), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon(Icons.receipt_long, size: 64, color: Colors.grey), const SizedBox(height: 24), Text( '如何使用', style: Theme.of(context).textTheme.titleLarge, ), const SizedBox(height: 16), const Text( '1. 在支付成功页面截图\n' '2. 点击分享按钮\n' '3. 选择 SnapLedger\n' '4. 添加备注(可选)并提交', textAlign: TextAlign.center, style: TextStyle(fontSize: 16, color: Color(0xFF616161)), ), ], ), ), ), ); } }