|
1 | 1 | import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/services.dart'; |
2 | 3 | import 'package:provider/provider.dart'; |
3 | 4 | import '../providers/v2ray_provider.dart'; |
4 | 5 | import '../widgets/connection_button.dart'; |
@@ -41,6 +42,102 @@ class _HomeScreenState extends State<HomeScreen> { |
41 | 42 | super.dispose(); |
42 | 43 | } |
43 | 44 |
|
| 45 | + // Share V2Ray link to clipboard |
| 46 | + void _shareV2RayLink(BuildContext context) async { |
| 47 | + try { |
| 48 | + final provider = Provider.of<V2RayProvider>(context, listen: false); |
| 49 | + final activeConfig = provider.activeConfig; |
| 50 | + |
| 51 | + if (activeConfig != null && activeConfig.fullConfig.isNotEmpty) { |
| 52 | + await Clipboard.setData(ClipboardData(text: activeConfig.fullConfig)); |
| 53 | + |
| 54 | + ScaffoldMessenger.of(context).showSnackBar( |
| 55 | + SnackBar( |
| 56 | + content: Row( |
| 57 | + mainAxisSize: MainAxisSize.min, |
| 58 | + children: [ |
| 59 | + Icon( |
| 60 | + Icons.check_circle, |
| 61 | + color: AppTheme.primaryGreen, |
| 62 | + size: 18, |
| 63 | + ), |
| 64 | + const SizedBox(width: 8), |
| 65 | + Expanded( |
| 66 | + child: Text( |
| 67 | + 'V2Ray link copied to clipboard', |
| 68 | + style: TextStyle(color: Colors.white), |
| 69 | + ), |
| 70 | + ), |
| 71 | + ], |
| 72 | + ), |
| 73 | + backgroundColor: AppTheme.cardDark, |
| 74 | + duration: const Duration(seconds: 2), |
| 75 | + behavior: SnackBarBehavior.floating, |
| 76 | + shape: RoundedRectangleBorder( |
| 77 | + borderRadius: BorderRadius.circular(8), |
| 78 | + ), |
| 79 | + ), |
| 80 | + ); |
| 81 | + } else { |
| 82 | + ScaffoldMessenger.of(context).showSnackBar( |
| 83 | + SnackBar( |
| 84 | + content: Row( |
| 85 | + mainAxisSize: MainAxisSize.min, |
| 86 | + children: [ |
| 87 | + Icon( |
| 88 | + Icons.error_outline, |
| 89 | + color: Colors.red, |
| 90 | + size: 18, |
| 91 | + ), |
| 92 | + const SizedBox(width: 8), |
| 93 | + Expanded( |
| 94 | + child: Text( |
| 95 | + 'No V2Ray configuration available to share', |
| 96 | + style: TextStyle(color: Colors.white), |
| 97 | + ), |
| 98 | + ), |
| 99 | + ], |
| 100 | + ), |
| 101 | + backgroundColor: Colors.red.shade700, |
| 102 | + duration: const Duration(seconds: 2), |
| 103 | + behavior: SnackBarBehavior.floating, |
| 104 | + shape: RoundedRectangleBorder( |
| 105 | + borderRadius: BorderRadius.circular(8), |
| 106 | + ), |
| 107 | + ), |
| 108 | + ); |
| 109 | + } |
| 110 | + } catch (e) { |
| 111 | + ScaffoldMessenger.of(context).showSnackBar( |
| 112 | + SnackBar( |
| 113 | + content: Row( |
| 114 | + mainAxisSize: MainAxisSize.min, |
| 115 | + children: [ |
| 116 | + Icon( |
| 117 | + Icons.error_outline, |
| 118 | + color: Colors.red, |
| 119 | + size: 18, |
| 120 | + ), |
| 121 | + const SizedBox(width: 8), |
| 122 | + Expanded( |
| 123 | + child: Text( |
| 124 | + 'Error copying to clipboard: ${e.toString()}', |
| 125 | + style: TextStyle(color: Colors.white), |
| 126 | + ), |
| 127 | + ), |
| 128 | + ], |
| 129 | + ), |
| 130 | + backgroundColor: Colors.red.shade700, |
| 131 | + duration: const Duration(seconds: 3), |
| 132 | + behavior: SnackBarBehavior.floating, |
| 133 | + shape: RoundedRectangleBorder( |
| 134 | + borderRadius: BorderRadius.circular(8), |
| 135 | + ), |
| 136 | + ), |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | + |
44 | 141 | @override |
45 | 142 | Widget build(BuildContext context) { |
46 | 143 | return BackgroundGradient( |
@@ -232,6 +329,19 @@ class _HomeScreenState extends State<HomeScreen> { |
232 | 329 | overflow: TextOverflow.ellipsis, |
233 | 330 | ), |
234 | 331 | ), |
| 332 | + const SizedBox(width: 8), |
| 333 | + InkWell( |
| 334 | + onTap: () => _shareV2RayLink(context), |
| 335 | + borderRadius: BorderRadius.circular(12), |
| 336 | + child: Padding( |
| 337 | + padding: const EdgeInsets.all(4.0), |
| 338 | + child: Icon( |
| 339 | + Icons.share, |
| 340 | + size: 18, |
| 341 | + color: AppTheme.primaryGreen, |
| 342 | + ), |
| 343 | + ), |
| 344 | + ), |
235 | 345 | ], |
236 | 346 | ); |
237 | 347 | } |
|
0 commit comments