Sub topic 3
Pinch and rotate with scale callbacks
This chapter assumes you know Dart closures, StatefulWidget with setState, and layout widgets such as Column and Stack.
Pinch and rotate with scale callbacks
GestureDetector exposes onScaleStart, onScaleUpdate and onScaleEnd. A pinch changes details.scale, which is relative to the start of the gesture. details.rotation is the accumulated angle in radians. Store the scale at gesture start so each update multiplies the original value, not the running value.
double _baseScale = 1.0;
double _scale = 1.0;
double _angle = 0.0;
GestureDetector(
onScaleStart: (details) {
_baseScale = _scale;
},
onScaleUpdate: (details) {
setState(() {
_scale = _baseScale * details.scale;
_angle = details.rotation;
});
},
child: Transform.rotate(
angle: _angle,
child: Transform.scale(scale: _scale, child: const FlutterLogo(size: 150)),
),
)How competing gestures are resolved in the gesture arena
On pointer down, Flutter hit tests the widgets under the finger. Each recognizer that wants the pointer joins a gesture arena. The arena declares one winner, and the other recognizers lose. A tap and a pan on the same widget compete, and the tap loses once the finger moves beyond the slop distance.
Swipe to dismiss and drag and drop
Dismissible removes a widget when the user swipes it away. Each Dismissible needs a unique key. Remove the underlying data in onDismissed. Draggable<T> carries data, and DragTarget<T> accepts it. Both pair through a shared type parameter.
Dismissible(
key: ValueKey(item),
background: Container(color: Colors.red),
onDismissed: (direction) {
setState(() => items.remove(item));
},
child: ListTile(title: Text(item)),
)Composing a custom tappable widget
Wrap a GestureDetector in Semantics so screen readers announce it as a button. Set HitTestBehavior.opaque so transparent areas respond to taps.
import 'package:flutter/material.dart';
class Tappable extends StatelessWidget {
const Tappable({
super.key,
required this.label,
required this.onTap,
required this.child,
});
final String label;
final VoidCallback onTap;
final Widget child;
@override
Widget build(BuildContext context) {
return Semantics(
button: true,
label: label,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: onTap,
child: ConstrainedBox(
constraints: const BoxConstraints(minWidth: 48, minHeight: 48),
child: Center(child: child),
),
),
);
}
}Accessible touch targets and semantics
Keep every touch target at least 48 by 48 logical pixels, the Material guideline. Give icon-only controls a tooltip, which also supplies a label for screen readers.
Testing taps and drags with flutter_test
Use tester.tap and tester.drag to send input, then call pump to rebuild. Use pumpAndSettle when an animation runs over several frames.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('tap and swipe update the screen', (tester) async {
var taps = 0;
var dismissed = false;
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Column(
children: [
Tappable(
label: 'Like',
onTap: () {
taps++;
},
child: const Text('Like'),
),
Dismissible(
key: const ValueKey('a'),
onDismissed: (_) {
dismissed = true;
},
child: const Text('Swipe me'),
),
],
),
),
));
await tester.tap(find.text('Like'));
await tester.pump();
expect(taps, 1);
await tester.drag(find.text('Swipe me'), const Offset(-500, 0));
await tester.pumpAndSettle();
expect(dismissed, isTrue);
});
}Exercise
Why does the test call pumpAndSettle after the drag, but only pump after the tap?
Show answer
The tap changes state in a single frame, so one pump rebuilds the tree. The dismiss animation runs across several frames, and pumpAndSettle keeps advancing time until no frames remain, so onDismissed has fired before the assertion.