Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 80 additions & 41 deletions dev/integration_tests/widget_preview_scaffold/lib/src/controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,62 +84,101 @@ class _ContainingPackageSearchFilter extends _SearchFilterConfig {
/// Provides controls to change the zoom level of a [WidgetPreview].
class ZoomControls extends StatelessWidget {
/// Provides controls to change the zoom level of a [WidgetPreview].
const ZoomControls({
super.key,
required TransformationController transformationController,
// ignore: prefer_initializing_formals
}) : _transformationController = transformationController;
const ZoomControls({super.key, required this._transformationController});

static const double _minScale = 1.0;
static const double _maxScale = 4.0;

final TransformationController _transformationController;

@override
Widget build(BuildContext context) {
const iconColor = Colors.black;
final theme = Theme.of(context);
return _ControlDecorator(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
tooltip: 'Zoom in',
onPressed: _zoomIn,
icon: Icon(Icons.zoom_in_sharp),
color: iconColor,
),
IconButton(
tooltip: 'Zoom out',
onPressed: _zoomOut,
icon: Icon(Icons.zoom_out),
color: iconColor,
),
IconButton(
tooltip: 'Reset zoom',
onPressed: _reset,
icon: Icon(Icons.zoom_out_map),
color: iconColor,
),
],
child: ValueListenableBuilder<Matrix4>(
valueListenable: _transformationController,
builder: (context, matrix, _) {
final double scale = matrix.entry(0, 0);
final String scalePercentage = '${(scale * 100).toStringAsFixed(0)}%';
return Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
tooltip: 'Zoom out',
style: theme.iconButtonTheme.style,
onPressed: scale > _minScale ? _zoomOut : null,
icon: const Icon(Icons.zoom_out),
color: theme.colorScheme.onSurface,
disabledColor: theme.colorScheme.onSurface.withValues(
alpha: 0.38,
),
),
SizedBox(
width: 100,
height: defaultButtonHeight,
child: Slider(
min: _minScale,
max: _maxScale,
value: scale.clamp(_minScale, _maxScale),
onChanged: _setScale,
),
),
IconButton(
tooltip: 'Zoom in',
style: theme.iconButtonTheme.style,
onPressed: scale < _maxScale ? _zoomIn : null,
icon: const Icon(Icons.zoom_in_sharp),
color: theme.colorScheme.onSurface,
disabledColor: theme.colorScheme.onSurface.withValues(
alpha: 0.38,
),
),
IconButton(
tooltip: 'Reset zoom',
style: theme.iconButtonTheme.style,
onPressed: scale != _minScale ? _reset : null,
icon: const Icon(Icons.zoom_out_map),
color: theme.colorScheme.onSurface,
disabledColor: theme.colorScheme.onSurface.withValues(
alpha: 0.38,
),
),
SizedBox(
width: 36,
child: Text(
scalePercentage,
textAlign: TextAlign.end,
style: TextStyle(
color: theme.colorScheme.onSurface,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
),
],
);
},
),
);
}

void _zoomIn() {
_transformationController.value = Matrix4.copy(
_transformationController.value,
).scaledByDouble(1.1, 1.1, 1.1, 1);
final double currentScale = _transformationController.value.entry(0, 0);
_setScale(currentScale + 0.25);
}

void _zoomOut() {
final Matrix4 updated = Matrix4.copy(
_transformationController.value,
).scaledByDouble(0.9, 0.9, 0.9, 1);

// Don't allow for zooming out past the original size of the widget.
// Assumes scaling is evenly applied to the entire matrix.
if (updated.entry(0, 0) < 1.0) {
updated.setIdentity();
}
final double currentScale = _transformationController.value.entry(0, 0);
_setScale(currentScale - 0.25);
}

_transformationController.value = updated;
void _setScale(double scale) {
final double clampedScale = scale.clamp(_minScale, _maxScale);
_transformationController.value = Matrix4.diagonal3Values(
clampedScale,
clampedScale,
1.0,
);
}

void _reset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ ThemeData _baseTheme({
),
canvasColor: backgroundColor,
scaffoldBackgroundColor: backgroundColor,
sliderTheme: theme.sliderTheme.copyWith(
trackHeight: 2.0,
thumbShape: const RoundSliderThumbShape(enabledThumbRadius: 5.0),
overlayShape: const RoundSliderOverlayShape(overlayRadius: 10.0),
),
iconButtonTheme: IconButtonThemeData(
style: IconButton.styleFrom(
padding: const EdgeInsets.all(densePadding),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -505,8 +505,10 @@ class WidgetPreviewWidgetState extends State<WidgetPreviewWidget> {
: null,
child: Column(
children: [
InteractiveViewerWrapper(
ZoomablePreviewArea(
transformationController: transformationController,
errorThrownDuringTreeConstruction:
errorThrownDuringTreeConstruction,
child: preview,
),
const VerticalSpacer(),
Expand Down Expand Up @@ -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),
),
);
Comment thread
bkonyi marked this conversation as resolved.
},
);
}
}

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
}

Comment thread
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,
);
}
Comment thread
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);
},
);
}
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion dev/integration_tests/widget_preview_scaffold/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: widget_preview_scaffold
description: Scaffolding for Flutter Widget Previews

environment:
sdk: ^3.10.0-0
sdk: ^3.12.0

resolution: workspace

Expand Down
Loading
Loading