import 'package:flutter/material.dart'; /// Fits the output canvas within the workspace; native rendering fits media /// within this canvas using its own source geometry (including rotation). class PreviewCanvas extends StatelessWidget { const PreviewCanvas({ super.key, required this.aspectRatio, required this.child, this.overlayBuilder, this.horizontalPadding = 16, this.alignment = Alignment.topCenter, this.backgroundColor = Colors.black, }); final double aspectRatio; final double horizontalPadding; final Alignment alignment; final Color backgroundColor; final Widget child; final Widget Function(Rect canvasRect)? overlayBuilder; @override Widget build(BuildContext context) => RepaintBoundary( child: ColoredBox( key: const Key('preview-surface'), color: Theme.of(context).colorScheme.surface, child: LayoutBuilder( builder: (context, constraints) { final available = Size( (constraints.maxWidth - horizontalPadding * 2).clamp( 0, double.infinity, ), constraints.maxHeight, ); final fitted = applyBoxFit( BoxFit.contain, Size(aspectRatio, 1), available, ).destination; final rect = alignment.inscribe( fitted, Offset.zero & constraints.biggest, ); return Stack( fit: StackFit.expand, clipBehavior: Clip.hardEdge, children: [ Positioned.fromRect( rect: rect, child: ColoredBox( key: const Key('preview-canvas'), color: backgroundColor, child: ClipRect(child: child), ), ), if (overlayBuilder != null && rect.width > 0 && rect.height > 0) Positioned.fill(child: overlayBuilder!(rect)), ], ); }, ), ), ); }