image_context_menu.dart (9050B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | import 'dart:math' as math; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:creekui/data/models/image_model.dart'; import 'package:creekui/utils/image_actions_helper.dart'; import 'package:creekui/ui/styles/variables.dart'; class ImageContextMenu extends StatelessWidget { final ImageModel image; final Widget child; final VoidCallback onImageDeleted; const ImageContextMenu({ super.key, required this.image, required this.child, required this.onImageDeleted, }); void _showRadialMenu(BuildContext context, Offset globalPosition) { Navigator.of(context).push( PageRouteBuilder( opaque: false, barrierDismissible: true, barrierColor: Colors.black12, // Subtle dim transitionDuration: const Duration(milliseconds: 200), pageBuilder: (context, animation, secondaryAnimation) { return FadeTransition( opacity: animation, child: _RadialMenuOverlay( center: globalPosition, image: image, onImageDeleted: onImageDeleted, ), ); }, ), ); } @override Widget build(BuildContext context) { return GestureDetector( // Get exact touch position onLongPressStart: (details) => _showRadialMenu(context, details.globalPosition), child: child, ); } } class _RadialMenuOverlay extends StatefulWidget { final Offset center; final ImageModel image; final VoidCallback onImageDeleted; const _RadialMenuOverlay({ required this.center, required this.image, required this.onImageDeleted, }); @override State<_RadialMenuOverlay> createState() => _RadialMenuOverlayState(); } class _RadialMenuOverlayState extends State<_RadialMenuOverlay> with SingleTickerProviderStateMixin { late AnimationController _controller; late Animation<double> _scaleAnimation; // Configuration final double radius = 120.0; final double buttonSize = 56.0; final double arcSpan = 150.0; @override void initState() { super.initState(); _controller = AnimationController( duration: const Duration(milliseconds: 350), vsync: this, ); _scaleAnimation = CurvedAnimation( parent: _controller, curve: Curves.easeOutBack, ); _controller.forward(); } @override void dispose() { _controller.dispose(); super.dispose(); } // Closes menu with animation void _closeAnimated() { _controller.reverse().then((_) { if (mounted) Navigator.of(context).pop(); }); } // ACTIONS Future<void> _shareImage() async { Navigator.of(context).pop(); await ImageActionsHelper.shareImage(context, widget.image.filePath); } void _sendToFiles() { Navigator.of(context).pop(); ImageActionsHelper.sendToFiles(context, widget.image.filePath); } Future<void> _renameImage() async { // Hide buttons to not obstruct dialog _controller.reverse(); await ImageActionsHelper.renameImage( context, widget.image, widget.onImageDeleted, // Triggers refresh in parent ); if (mounted) Navigator.of(context).pop(); } Future<void> _deleteImage() async { // Hide buttons _controller.reverse(); await ImageActionsHelper.deleteImage( context, widget.image, widget.onImageDeleted, ); if (mounted) Navigator.of(context).pop(); } void _notImplemented() { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: const Text( "Coming soon", style: TextStyle(fontFamily: 'GeneralSans'), ), backgroundColor: Variables.textPrimary, behavior: SnackBarBehavior.floating, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)), margin: const EdgeInsets.all(20), duration: const Duration(seconds: 1), ), ); } @override Widget build(BuildContext context) { final screenSize = MediaQuery.of(context).size; // 1. Determine Horizontal Side final bool isRightSide = widget.center.dx > screenSize.width / 2; double baseAngle = isRightSide ? 180 : 0; // 2. Determine Vertical clipping adjustments double topBoundary = 120; double bottomBoundary = screenSize.height - 120; double rotationOffset = 0; if (widget.center.dy < topBoundary) { // Too close to TOP -> Rotate DOWN rotationOffset = isRightSide ? -45 : 45; } else if (widget.center.dy > bottomBoundary) { // Too close to BOTTOM -> Rotate UP rotationOffset = isRightSide ? 45 : -45; } baseAngle += rotationOffset; // 3. Define Buttons final buttons = [ _MenuButtonData( svgPath: 'assets/icons/share_icon.svg', onTap: _shareImage, ), _MenuButtonData( svgPath: 'assets/icons/files_icon.svg', onTap: _sendToFiles, ), _MenuButtonData( icon: Icons.drive_file_rename_outline, onTap: _renameImage, ), _MenuButtonData( svgPath: 'assets/icons/trash_icon.svg', onTap: _deleteImage, isDestructive: true, ), _MenuButtonData( svgPath: 'assets/icons/ai-search.svg', onTap: _notImplemented, ), ]; return Stack( children: [ // Backdrop Positioned.fill( child: GestureDetector( onTap: _closeAnimated, behavior: HitTestBehavior.opaque, child: Container(color: Colors.transparent), ), ), // Buttons ...List.generate(buttons.length, (index) { final step = arcSpan / (buttons.length - 1); final startAngle = baseAngle - (arcSpan / 2); final angleDeg = startAngle + (step * index); return _buildAnimatedButton(angleDeg: angleDeg, data: buttons[index]); }), ], ); } Widget _buildAnimatedButton({ required double angleDeg, required _MenuButtonData data, }) { final rad = angleDeg * (math.pi / 180); return AnimatedBuilder( animation: _scaleAnimation, builder: (context, child) { final r = radius * _scaleAnimation.value; final dx = widget.center.dx + (r * math.cos(rad)) - (buttonSize / 2); final dy = widget.center.dy + (r * math.sin(rad)) - (buttonSize / 2); return Positioned( left: dx, top: dy, child: Transform.scale( scale: _scaleAnimation.value, child: _FloatingCircleButton(size: buttonSize, data: data), ), ); }, ); } } // Data Helper class _MenuButtonData { final IconData? icon; final String? svgPath; final VoidCallback onTap; final bool isDestructive; _MenuButtonData({ this.icon, this.svgPath, required this.onTap, this.isDestructive = false, }); } class _FloatingCircleButton extends StatefulWidget { final double size; final _MenuButtonData data; const _FloatingCircleButton({required this.size, required this.data}); @override State<_FloatingCircleButton> createState() => _FloatingCircleButtonState(); } class _FloatingCircleButtonState extends State<_FloatingCircleButton> { bool _isPressed = false; @override Widget build(BuildContext context) { final Color normalIconColor = widget.data.isDestructive ? Colors.red : Variables.textPrimary; final Color activeIconColor = Colors.white; final Color normalBgColor = Colors.white; final Color activeBgColor = const Color(0xFF7C4DFF); final currentColor = _isPressed ? activeIconColor : normalIconColor; final currentBg = _isPressed ? activeBgColor : normalBgColor; final scale = _isPressed ? 1.2 : 1.0; return GestureDetector( onTapDown: (_) => setState(() => _isPressed = true), onTapUp: (_) => setState(() => _isPressed = false), onTapCancel: () => setState(() => _isPressed = false), onTap: widget.data.onTap, child: AnimatedContainer( duration: const Duration(milliseconds: 150), curve: Curves.easeOut, width: widget.size, height: widget.size, transform: Matrix4.identity()..scale(scale), transformAlignment: Alignment.center, decoration: BoxDecoration( color: currentBg, shape: BoxShape.circle, boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.15), blurRadius: 10, offset: const Offset(0, 4), ), ], ), padding: const EdgeInsets.all(14), child: widget.data.svgPath != null ? SvgPicture.asset( widget.data.svgPath!, width: 18, height: 18, colorFilter: ColorFilter.mode(currentColor, BlendMode.srcIn), ) : Icon(widget.data.icon, color: currentColor, size: 24), ), ); } } |