Skip to content

Commit 3ca99c3

Browse files
authored
[ Widget Preview ] Improve zoom behavior and add zoom slider (#186422)
The current zoom behavior uses an `InteractiveViewer`, which zooms in on the previewed widget without actually increasing the size of the preview scaffolding. This makes it difficult to zoom in on details for smaller previews. This change updates the zoom behavior to scale the entire widget, forcing the preview scaffolding to increase in size. A slider has also been added with a zoom level indicator to make it easier to zoom in on a widget.
1 parent 259aeae commit 3ca99c3

9 files changed

Lines changed: 654 additions & 108 deletions

File tree

‎dev/integration_tests/widget_preview_scaffold/lib/src/controls.dart‎

Lines changed: 80 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -84,62 +84,101 @@ class _ContainingPackageSearchFilter extends _SearchFilterConfig {
8484
/// Provides controls to change the zoom level of a [WidgetPreview].
8585
class ZoomControls extends StatelessWidget {
8686
/// Provides controls to change the zoom level of a [WidgetPreview].
87-
const ZoomControls({
88-
super.key,
89-
required TransformationController transformationController,
90-
// ignore: prefer_initializing_formals
91-
}) : _transformationController = transformationController;
87+
const ZoomControls({super.key, required this._transformationController});
88+
89+
static const double _minScale = 1.0;
90+
static const double _maxScale = 4.0;
9291

9392
final TransformationController _transformationController;
9493

9594
@override
9695
Widget build(BuildContext context) {
97-
const iconColor = Colors.black;
96+
final theme = Theme.of(context);
9897
return _ControlDecorator(
99-
child: Row(
100-
mainAxisSize: MainAxisSize.min,
101-
children: [
102-
IconButton(
103-
tooltip: 'Zoom in',
104-
onPressed: _zoomIn,
105-
icon: Icon(Icons.zoom_in_sharp),
106-
color: iconColor,
107-
),
108-
IconButton(
109-
tooltip: 'Zoom out',
110-
onPressed: _zoomOut,
111-
icon: Icon(Icons.zoom_out),
112-
color: iconColor,
113-
),
114-
IconButton(
115-
tooltip: 'Reset zoom',
116-
onPressed: _reset,
117-
icon: Icon(Icons.zoom_out_map),
118-
color: iconColor,
119-
),
120-
],
98+
child: ValueListenableBuilder<Matrix4>(
99+
valueListenable: _transformationController,
100+
builder: (context, matrix, _) {
101+
final double scale = matrix.entry(0, 0);
102+
final String scalePercentage = '${(scale * 100).toStringAsFixed(0)}%';
103+
return Row(
104+
mainAxisSize: MainAxisSize.min,
105+
children: [
106+
IconButton(
107+
tooltip: 'Zoom out',
108+
style: theme.iconButtonTheme.style,
109+
onPressed: scale > _minScale ? _zoomOut : null,
110+
icon: const Icon(Icons.zoom_out),
111+
color: theme.colorScheme.onSurface,
112+
disabledColor: theme.colorScheme.onSurface.withValues(
113+
alpha: 0.38,
114+
),
115+
),
116+
SizedBox(
117+
width: 100,
118+
height: defaultButtonHeight,
119+
child: Slider(
120+
min: _minScale,
121+
max: _maxScale,
122+
value: scale.clamp(_minScale, _maxScale),
123+
onChanged: _setScale,
124+
),
125+
),
126+
IconButton(
127+
tooltip: 'Zoom in',
128+
style: theme.iconButtonTheme.style,
129+
onPressed: scale < _maxScale ? _zoomIn : null,
130+
icon: const Icon(Icons.zoom_in_sharp),
131+
color: theme.colorScheme.onSurface,
132+
disabledColor: theme.colorScheme.onSurface.withValues(
133+
alpha: 0.38,
134+
),
135+
),
136+
IconButton(
137+
tooltip: 'Reset zoom',
138+
style: theme.iconButtonTheme.style,
139+
onPressed: scale != _minScale ? _reset : null,
140+
icon: const Icon(Icons.zoom_out_map),
141+
color: theme.colorScheme.onSurface,
142+
disabledColor: theme.colorScheme.onSurface.withValues(
143+
alpha: 0.38,
144+
),
145+
),
146+
SizedBox(
147+
width: 36,
148+
child: Text(
149+
scalePercentage,
150+
textAlign: TextAlign.end,
151+
style: TextStyle(
152+
color: theme.colorScheme.onSurface,
153+
fontSize: 12,
154+
fontWeight: FontWeight.bold,
155+
),
156+
),
157+
),
158+
],
159+
);
160+
},
121161
),
122162
);
123163
}
124164

125165
void _zoomIn() {
126-
_transformationController.value = Matrix4.copy(
127-
_transformationController.value,
128-
).scaledByDouble(1.1, 1.1, 1.1, 1);
166+
final double currentScale = _transformationController.value.entry(0, 0);
167+
_setScale(currentScale + 0.25);
129168
}
130169

131170
void _zoomOut() {
132-
final Matrix4 updated = Matrix4.copy(
133-
_transformationController.value,
134-
).scaledByDouble(0.9, 0.9, 0.9, 1);
135-
136-
// Don't allow for zooming out past the original size of the widget.
137-
// Assumes scaling is evenly applied to the entire matrix.
138-
if (updated.entry(0, 0) < 1.0) {
139-
updated.setIdentity();
140-
}
171+
final double currentScale = _transformationController.value.entry(0, 0);
172+
_setScale(currentScale - 0.25);
173+
}
141174

142-
_transformationController.value = updated;
175+
void _setScale(double scale) {
176+
final double clampedScale = scale.clamp(_minScale, _maxScale);
177+
_transformationController.value = Matrix4.diagonal3Values(
178+
clampedScale,
179+
clampedScale,
180+
1.0,
181+
);
143182
}
144183

145184
void _reset() {

‎dev/integration_tests/widget_preview_scaffold/lib/src/theme/theme.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ ThemeData _baseTheme({
6565
),
6666
canvasColor: backgroundColor,
6767
scaffoldBackgroundColor: backgroundColor,
68+
sliderTheme: theme.sliderTheme.copyWith(
69+
trackHeight: 2.0,
70+
thumbShape: const RoundSliderThumbShape(enabledThumbRadius: 5.0),
71+
overlayShape: const RoundSliderOverlayShape(overlayRadius: 10.0),
72+
),
6873
iconButtonTheme: IconButtonThemeData(
6974
style: IconButton.styleFrom(
7075
padding: const EdgeInsets.all(densePadding),

‎dev/integration_tests/widget_preview_scaffold/lib/src/widget_preview_rendering.dart‎

Lines changed: 149 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class WidgetPreviewErrorWidget extends StatelessWidget {
6060
height: size.height,
6161
child: SingleChildScrollView(
6262
child: Column(
63-
crossAxisAlignment: CrossAxisAlignment.stretch,
63+
crossAxisAlignment: CrossAxisAlignment.start,
6464
children: [
6565
Text.rich(
6666
TextSpan(
@@ -505,8 +505,10 @@ class WidgetPreviewWidgetState extends State<WidgetPreviewWidget> {
505505
: null,
506506
child: Column(
507507
children: [
508-
InteractiveViewerWrapper(
508+
ZoomablePreviewArea(
509509
transformationController: transformationController,
510+
errorThrownDuringTreeConstruction:
511+
errorThrownDuringTreeConstruction,
510512
child: preview,
511513
),
512514
const VerticalSpacer(),
@@ -766,22 +768,159 @@ class WidgetPreviewerWindowConstraints extends InheritedWidget {
766768
}
767769
}
768770

769-
class InteractiveViewerWrapper extends StatelessWidget {
770-
const InteractiveViewerWrapper({
771+
class ZoomablePreviewArea extends StatelessWidget {
772+
const ZoomablePreviewArea({
771773
super.key,
772774
required this.child,
773775
required this.transformationController,
776+
required this.errorThrownDuringTreeConstruction,
774777
});
775778

776779
final Widget child;
777780
final TransformationController transformationController;
781+
final bool errorThrownDuringTreeConstruction;
778782

779783
@override
780784
Widget build(BuildContext context) {
781-
return InteractiveViewer(
782-
transformationController: transformationController,
783-
scaleEnabled: false,
784-
child: child,
785+
if (errorThrownDuringTreeConstruction) {
786+
return child;
787+
}
788+
return ListenableBuilder(
789+
listenable: transformationController,
790+
builder: (context, _) {
791+
final double scale = transformationController.value.entry(0, 0);
792+
return SingleChildScrollView(
793+
scrollDirection: Axis.vertical,
794+
child: SingleChildScrollView(
795+
scrollDirection: Axis.horizontal,
796+
child: _ScaledLayoutWrapper(scale: scale, child: child),
797+
),
798+
);
799+
},
800+
);
801+
}
802+
}
803+
804+
class _ScaledLayoutWrapper extends SingleChildRenderObjectWidget {
805+
const _ScaledLayoutWrapper({super.child, required this.scale});
806+
807+
final double scale;
808+
809+
@override
810+
RenderObject createRenderObject(BuildContext context) {
811+
return _ScaledLayoutRenderObject(scale: scale);
812+
}
813+
814+
@override
815+
void updateRenderObject(
816+
BuildContext context,
817+
_ScaledLayoutRenderObject renderObject,
818+
) {
819+
renderObject.scale = scale;
820+
}
821+
}
822+
823+
class _ScaledLayoutRenderObject extends RenderShiftedBox {
824+
_ScaledLayoutRenderObject({required this._scale, RenderBox? child})
825+
: super(child);
826+
827+
double _scale;
828+
double get scale => _scale;
829+
set scale(double value) {
830+
if (_scale == value) {
831+
return;
832+
}
833+
_scale = value;
834+
markNeedsLayout();
835+
}
836+
837+
@override
838+
double computeMinIntrinsicWidth(double height) {
839+
if (child == null) {
840+
return 0.0;
841+
}
842+
return child!.getMinIntrinsicWidth(height / scale) * scale;
843+
}
844+
845+
@override
846+
double computeMaxIntrinsicWidth(double height) {
847+
if (child == null) {
848+
return 0.0;
849+
}
850+
return child!.getMaxIntrinsicWidth(height / scale) * scale;
851+
}
852+
853+
@override
854+
double computeMinIntrinsicHeight(double width) {
855+
if (child == null) {
856+
return 0.0;
857+
}
858+
return child!.getMinIntrinsicHeight(width / scale) * scale;
859+
}
860+
861+
@override
862+
double computeMaxIntrinsicHeight(double width) {
863+
if (child == null) {
864+
return 0.0;
865+
}
866+
return child!.getMaxIntrinsicHeight(width / scale) * scale;
867+
}
868+
869+
@override
870+
void performLayout() {
871+
final child = this.child;
872+
if (child == null) {
873+
size = Size.zero;
874+
return;
875+
}
876+
child.layout(constraints, parentUsesSize: true);
877+
size = constraints.constrain(child.size * scale);
878+
879+
final BoxParentData childParentData = child.parentData! as BoxParentData;
880+
childParentData.offset = Offset.zero;
881+
}
882+
883+
@override
884+
void paint(PaintingContext context, Offset offset) {
885+
if (child == null) {
886+
layer = null;
887+
return;
888+
}
889+
if (scale == 1.0) {
890+
super.paint(context, offset);
891+
layer = null;
892+
return;
893+
}
894+
final Matrix4 transform = Matrix4.diagonal3Values(scale, scale, 1.0);
895+
layer = context.pushTransform(
896+
needsCompositing,
897+
offset,
898+
transform,
899+
super.paint,
900+
oldLayer: layer is TransformLayer ? layer as TransformLayer? : null,
901+
);
902+
}
903+
904+
@override
905+
void applyPaintTransform(RenderBox child, Matrix4 transform) {
906+
if (scale != 1.0) {
907+
transform.scaleByDouble(scale, scale, 1.0, 1.0);
908+
}
909+
super.applyPaintTransform(child, transform);
910+
}
911+
912+
@override
913+
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) {
914+
if (child == null) {
915+
return false;
916+
}
917+
final Matrix4 transform = Matrix4.diagonal3Values(scale, scale, 1.0);
918+
return result.addWithPaintTransform(
919+
transform: transform,
920+
position: position,
921+
hitTest: (BoxHitTestResult result, Offset position) {
922+
return super.hitTestChildren(result, position: position);
923+
},
785924
);
786925
}
787926
}
@@ -820,10 +959,8 @@ class _WidgetPreviewWrapper extends SingleChildRenderObjectWidget {
820959
class _WidgetPreviewWrapperBox extends RenderShiftedBox {
821960
_WidgetPreviewWrapperBox({
822961
required RenderBox? child,
823-
required BoxConstraints previewerConstraints,
824-
// ignore: prefer_initializing_formals
825-
}) : _previewerConstraints = previewerConstraints,
826-
super(child);
962+
required this._previewerConstraints,
963+
}) : super(child);
827964

828965
BoxConstraints _constraintOverride = const BoxConstraints();
829966
BoxConstraints _previewerConstraints;

‎dev/integration_tests/widget_preview_scaffold/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: widget_preview_scaffold
22
description: Scaffolding for Flutter Widget Previews
33

44
environment:
5-
sdk: ^3.10.0-0
5+
sdk: ^3.12.0
66

77
resolution: workspace
88

0 commit comments

Comments
 (0)