Sub topic 10
Pinch and rotate with scale callbacks
Prerequisites and the scale gesture
This chapter assumes that you can build StatefulWidget classes, use setState, and recognize basic GestureDetector callbacks.
A scale gesture is a pointer interaction that can include:
- Pinching with two fingers to zoom.
- Moving two fingers to pan.
- Rotating two fingers around a focal point.
GestureDetector reports this interaction through scale callbacks. The callbacks use ScaleStartDetails, ScaleUpdateDetails, and ScaleEndDetails objects to describe the gesture.
The most useful values in ScaleUpdateDetails are:
scale: The relative zoom factor. A value of1.0means no change from the start.rotation: The rotation in radians since the gesture started.focalPoint: The point around which the pointers are interacting.
The following diagram shows the usual callback sequence:
Applying scale and rotation
Store the current transform values in the widget state. At the beginning of each gesture, save the values that were already applied. During updates, combine those starting values with the current gesture details.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: TransformDemo()));
}
class TransformDemo extends StatefulWidget {
const TransformDemo({super.key});
@override
State<TransformDemo> createState() => _TransformDemoState();
}
class _TransformDemoState extends State<TransformDemo> {
double _scale = 1.0;
double _rotation = 0.0;
double _startScale = 1.0;
double _startRotation = 0.0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Pinch and rotate')),
body: Center(
child: GestureDetector(
onScaleStart: (details) {
_startScale = _scale;
_startRotation = _rotation;
},
onScaleUpdate: (details) {
setState(() {
_scale = (_startScale * details.scale).clamp(0.5, 3.0);
_rotation = _startRotation + details.rotation;
});
},
child: Transform(
alignment: Alignment.center,
transform: Matrix4.identity()
..rotateZ(_rotation)
..scale(_scale),
child: Container(
width: 180,
height: 120,
color: Colors.teal,
alignment: Alignment.center,
child: const Text(
'Pinch or rotate',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
),
),
),
);
}
}Transform changes how its child is painted. It does not change the child's layout space. rotateZ rotates around the z-axis, which produces the expected two-dimensional rotation. The clamp call prevents the object from becoming too small or too large.
Do not assign details.scale directly to _scale unless you intentionally want every gesture to reset the zoom. The value is relative to the current gesture, so multiplying it by _startScale preserves the previous zoom.
Common errors
- Forgetting
setState, so the transform never repaints. - Treating
rotationas degrees. Flutter reports radians. Userotation * 180 / pionly when displaying degrees. - Omitting
onScaleStart, then losing the transform that existed before the newest gesture. - Allowing unlimited scale, which can make content disappear or become difficult to recover.
- Expecting
Transformto resize surrounding widgets. Use layout constraints separately when the layout itself must change.
Exercise
Why should a scale gesture save the existing scale and rotation in onScaleStart?
Show answer
ScaleUpdateDetails values are relative to the current gesture. Saving the existing values lets the update combine the new pinch and rotation with transformations from earlier gestures.