scaleTo method
Scales the child to the specified scale value
final GlobalKey<InteractiveScrollViewerState> _interactiveViewerKey =
GlobalKey();
TransformationController transformationController=
TransformationController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('InteractiveScrollViewer'),
actions: <Widget>[
IconButton(
icon: const Icon(
Icons.arrow_circle_down,
color: Colors.white,
),
onPressed: () {
_interactiveViewerKey.currentState?.scaleTo(2);
},
),
],
),
body: InteractiveScrollViewer(
Image.network('https://picsum.photos/250?image=9'),
key: _interactiveViewerKey,
transformationController: transformationController,
));
}
Implementation
void scaleTo(double scale) {
if (widget.transformationController != null) {
scale = scale.clamp(widget.minScale, widget.maxScale);
final double zoomLevel =
widget.transformationController!.value.getMaxScaleOnAxis();
final Offset previousOffset =
widget.transformationController!.toScene(Offset.zero);
widget.transformationController?.value =
widget.transformationController!.value.clone()
..scale(scale / zoomLevel, scale / zoomLevel);
scrollTo(previousOffset);
}
}