-
Notifications
You must be signed in to change notification settings - Fork 30.6k
[ Widget Preview ] Improve zoom behavior and add zoom slider #186422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
90ba5ee
[ Widget Preview ] Improve zoom behavior and add zoom slider
bkonyi 4c7cc03
Add test
bkonyi 233dff4
Address comments
bkonyi 4fb9eb6
Fix test
bkonyi 4532dce
[Widget Preview] Fix intrinsic dimension calculations for scaled layout
bkonyi 369a9a1
Merge branch 'main' of https://github.com/flutter/flutter into widget…
bkonyi 7b40b27
Fix analysis
bkonyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,7 @@ class WidgetPreviewErrorWidget extends StatelessWidget { | |
| height: size.height, | ||
| child: SingleChildScrollView( | ||
| child: Column( | ||
| crossAxisAlignment: CrossAxisAlignment.stretch, | ||
| crossAxisAlignment: CrossAxisAlignment.start, | ||
| children: [ | ||
| Text.rich( | ||
| TextSpan( | ||
|
|
@@ -505,8 +505,10 @@ class WidgetPreviewWidgetState extends State<WidgetPreviewWidget> { | |
| : null, | ||
| child: Column( | ||
| children: [ | ||
| InteractiveViewerWrapper( | ||
| ZoomablePreviewArea( | ||
| transformationController: transformationController, | ||
| errorThrownDuringTreeConstruction: | ||
| errorThrownDuringTreeConstruction, | ||
| child: preview, | ||
| ), | ||
| const VerticalSpacer(), | ||
|
|
@@ -766,22 +768,159 @@ class WidgetPreviewerWindowConstraints extends InheritedWidget { | |
| } | ||
| } | ||
|
|
||
| class InteractiveViewerWrapper extends StatelessWidget { | ||
| const InteractiveViewerWrapper({ | ||
| class ZoomablePreviewArea extends StatelessWidget { | ||
| const ZoomablePreviewArea({ | ||
| super.key, | ||
| required this.child, | ||
| required this.transformationController, | ||
| required this.errorThrownDuringTreeConstruction, | ||
| }); | ||
|
|
||
| final Widget child; | ||
| final TransformationController transformationController; | ||
| final bool errorThrownDuringTreeConstruction; | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return InteractiveViewer( | ||
| transformationController: transformationController, | ||
| scaleEnabled: false, | ||
| child: child, | ||
| if (errorThrownDuringTreeConstruction) { | ||
| return child; | ||
| } | ||
| return ListenableBuilder( | ||
| listenable: transformationController, | ||
| builder: (context, _) { | ||
| final double scale = transformationController.value.entry(0, 0); | ||
| return SingleChildScrollView( | ||
| scrollDirection: Axis.vertical, | ||
| child: SingleChildScrollView( | ||
| scrollDirection: Axis.horizontal, | ||
| child: _ScaledLayoutWrapper(scale: scale, child: child), | ||
| ), | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class _ScaledLayoutWrapper extends SingleChildRenderObjectWidget { | ||
| const _ScaledLayoutWrapper({super.child, required this.scale}); | ||
|
|
||
| final double scale; | ||
|
|
||
| @override | ||
| RenderObject createRenderObject(BuildContext context) { | ||
| return _ScaledLayoutRenderObject(scale: scale); | ||
| } | ||
|
|
||
| @override | ||
| void updateRenderObject( | ||
| BuildContext context, | ||
| _ScaledLayoutRenderObject renderObject, | ||
| ) { | ||
| renderObject.scale = scale; | ||
| } | ||
| } | ||
|
|
||
| class _ScaledLayoutRenderObject extends RenderShiftedBox { | ||
| _ScaledLayoutRenderObject({required this._scale, RenderBox? child}) | ||
| : super(child); | ||
|
|
||
| double _scale; | ||
| double get scale => _scale; | ||
| set scale(double value) { | ||
| if (_scale == value) { | ||
| return; | ||
| } | ||
| _scale = value; | ||
| markNeedsLayout(); | ||
| } | ||
|
|
||
| @override | ||
| double computeMinIntrinsicWidth(double height) { | ||
| if (child == null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These could all use a local variable pattern which would use one less instance field retrieval and one less null-assert, but no biggie: if (child case var child?) {
return child.getMinIntrinsicWidth(height / scale) * scale;
} else {
return 0.0;
} |
||
| return 0.0; | ||
| } | ||
| return child!.getMinIntrinsicWidth(height / scale) * scale; | ||
| } | ||
|
|
||
| @override | ||
| double computeMaxIntrinsicWidth(double height) { | ||
| if (child == null) { | ||
| return 0.0; | ||
| } | ||
| return child!.getMaxIntrinsicWidth(height / scale) * scale; | ||
| } | ||
|
|
||
| @override | ||
| double computeMinIntrinsicHeight(double width) { | ||
| if (child == null) { | ||
| return 0.0; | ||
| } | ||
| return child!.getMinIntrinsicHeight(width / scale) * scale; | ||
| } | ||
|
|
||
| @override | ||
| double computeMaxIntrinsicHeight(double width) { | ||
| if (child == null) { | ||
| return 0.0; | ||
| } | ||
| return child!.getMaxIntrinsicHeight(width / scale) * scale; | ||
| } | ||
|
|
||
|
bkonyi marked this conversation as resolved.
|
||
| @override | ||
| void performLayout() { | ||
| final child = this.child; | ||
| if (child == null) { | ||
| size = Size.zero; | ||
| return; | ||
| } | ||
| child.layout(constraints, parentUsesSize: true); | ||
| size = constraints.constrain(child.size * scale); | ||
|
|
||
| final BoxParentData childParentData = child.parentData! as BoxParentData; | ||
| childParentData.offset = Offset.zero; | ||
| } | ||
|
|
||
| @override | ||
| void paint(PaintingContext context, Offset offset) { | ||
| if (child == null) { | ||
| layer = null; | ||
| return; | ||
| } | ||
| if (scale == 1.0) { | ||
| super.paint(context, offset); | ||
| layer = null; | ||
| return; | ||
| } | ||
| final Matrix4 transform = Matrix4.diagonal3Values(scale, scale, 1.0); | ||
| layer = context.pushTransform( | ||
| needsCompositing, | ||
| offset, | ||
| transform, | ||
| super.paint, | ||
| oldLayer: layer is TransformLayer ? layer as TransformLayer? : null, | ||
| ); | ||
| } | ||
|
bkonyi marked this conversation as resolved.
|
||
|
|
||
| @override | ||
| void applyPaintTransform(RenderBox child, Matrix4 transform) { | ||
| if (scale != 1.0) { | ||
| transform.scaleByDouble(scale, scale, 1.0, 1.0); | ||
| } | ||
| super.applyPaintTransform(child, transform); | ||
| } | ||
|
|
||
| @override | ||
| bool hitTestChildren(BoxHitTestResult result, {required Offset position}) { | ||
| if (child == null) { | ||
| return false; | ||
| } | ||
| final Matrix4 transform = Matrix4.diagonal3Values(scale, scale, 1.0); | ||
| return result.addWithPaintTransform( | ||
| transform: transform, | ||
| position: position, | ||
| hitTest: (BoxHitTestResult result, Offset position) { | ||
| return super.hitTestChildren(result, position: position); | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
@@ -820,10 +959,8 @@ class _WidgetPreviewWrapper extends SingleChildRenderObjectWidget { | |
| class _WidgetPreviewWrapperBox extends RenderShiftedBox { | ||
| _WidgetPreviewWrapperBox({ | ||
| required RenderBox? child, | ||
| required BoxConstraints previewerConstraints, | ||
| // ignore: prefer_initializing_formals | ||
| }) : _previewerConstraints = previewerConstraints, | ||
| super(child); | ||
| required this._previewerConstraints, | ||
| }) : super(child); | ||
|
|
||
| BoxConstraints _constraintOverride = const BoxConstraints(); | ||
| BoxConstraints _previewerConstraints; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.