Visitar URL original
Make the AbsorbPointer sample show the difference from IgnorePointer by theprantadutta · Pull Request #188940 · flutter/flutter · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
131 changes: 104 additions & 27 deletions packages/flutter/examples/api/lib/widgets/basic/absorb_pointer.0.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
Comment thread
navaronbracke marked this conversation as resolved.
Resolved

/// Flutter code sample for [AbsorbPointer].

Expand All @@ -13,42 +13,119 @@ class AbsorbPointerApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('AbsorbPointer Sample')),
body: const Center(child: AbsorbPointerExample()),
),
return WidgetsApp(
Comment thread
victorsanni marked this conversation as resolved.
Resolved
color: const Color(0xFFFFFFFF),
builder: (BuildContext context, Widget? child) {
return const AbsorbPointerExample();
},
);
}
}

class AbsorbPointerExample extends StatelessWidget {
class AbsorbPointerExample extends StatefulWidget {
const AbsorbPointerExample({super.key});

@override
State<AbsorbPointerExample> createState() => _AbsorbPointerExampleState();
}

class _AbsorbPointerExampleState extends State<AbsorbPointerExample> {
int _absorbTapCount = 0;
int _ignoreTapCount = 0;

@override
Widget build(BuildContext context) {
return Stack(
alignment: .center,
children: <Widget>[
SizedBox(
width: 200.0,
height: 100.0,
child: ElevatedButton(onPressed: () {}, child: null),
),
SizedBox(
width: 100.0,
height: 200.0,
child: AbsorbPointer(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue.shade200,
return ColoredBox(
color: const Color(0xFFFFFFFF),
child: Row(
mainAxisAlignment: .spaceEvenly,
children: <Widget>[
Column(
mainAxisAlignment: .center,
children: <Widget>[
const Text('AbsorbPointer'),
const SizedBox(height: 16.0),
Stack(
alignment: .center,
children: <Widget>[
GestureDetector(
onTap: () {
setState(() {
_absorbTapCount += 1;
});
},
child: Container(
width: 200.0,
height: 100.0,
color: const Color(0xFF2196F3),
alignment: .center,
child: const Text(
'Tap me',
style: TextStyle(color: Color(0xFFFFFFFF)),
),
),
),
// The AbsorbPointer absorbs the pointer events itself: over
// the overlapping region, neither its child nor the target
// behind it in the stack receives the tap, so the counter
// does not change.
AbsorbPointer(
child: Container(
width: 100.0,
height: 200.0,
color: const Color(0x88BBDEFB),
),
),
],
),
const SizedBox(height: 16.0),
Text('Taps received: $_absorbTapCount'),
],
),
Column(
mainAxisAlignment: .center,
children: <Widget>[
const Text('IgnorePointer'),
const SizedBox(height: 16.0),
Stack(
alignment: .center,
children: <Widget>[
GestureDetector(
onTap: () {
setState(() {
_ignoreTapCount += 1;
});
},
child: Container(
width: 200.0,
height: 100.0,
color: const Color(0xFF4CAF50),
alignment: .center,
child: const Text(
'Tap me',
style: TextStyle(color: Color(0xFFFFFFFF)),
),
),
),
// The IgnorePointer is invisible to hit testing: over the
// overlapping region, its child does not receive the tap,
// but the target behind it in the stack does, so the
// counter increases.
IgnorePointer(
child: Container(
width: 100.0,
height: 200.0,
color: const Color(0x88C8E6C9),
),
),
],
),
onPressed: () {},
child: null,
),
const SizedBox(height: 16.0),
Text('Taps received: $_ignoreTapCount'),
],
),
),
],
],
),
);
}
}
Comment thread
navaronbracke marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,53 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_api_samples/widgets/basic/absorb_pointer.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';

void main() {
testWidgets('AbsorbPointer prevents hit testing on its child', (
testWidgets('AbsorbPointer absorbs taps over the overlapping region', (
WidgetTester tester,
) async {
await tester.pumpWidget(const example.AbsorbPointerApp());

// Get the center of the stack.
final Offset center = tester.getCenter(find.byType(Stack).first);

final TestGesture gesture = await tester.createGesture(
kind: PointerDeviceKind.mouse,
pointer: 1,
final Finder absorbPointer = find.descendant(
of: find.byType(example.AbsorbPointerExample),
matching: find.byType(AbsorbPointer),
);
// Add the point to the center of the stack where the AbsorbPointer is.
await gesture.addPointer(location: center);
expect(
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic,

// Tapping the overlapping region does nothing: the AbsorbPointer claims
// the hit, so neither its child nor the target behind it in the stack
// receives the tap.
await tester.tapAt(tester.getCenter(absorbPointer));
await tester.pump();
expect(find.text('Taps received: 0'), findsNWidgets(2));

// The part of the target that the AbsorbPointer does not cover still
// receives taps.
await tester.tapAt(
tester.getCenter(absorbPointer) + const Offset(-75.0, 0.0),
);
await tester.pump();
expect(find.text('Taps received: 1'), findsOneWidget);
});

// Move the pointer to the left of the stack where the AbsorbPointer is not.
await gesture.moveTo(center + const Offset(-100, 0));
expect(
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
SystemMouseCursors.basic,
testWidgets('IgnorePointer passes taps through to the widget behind', (
WidgetTester tester,
) async {
await tester.pumpWidget(const example.AbsorbPointerApp());

final Finder ignorePointer = find.descendant(
of: find.byType(example.AbsorbPointerExample),
matching: find.byType(IgnorePointer),
);

// Tapping the overlapping region taps the target behind the
// IgnorePointer. The IgnorePointer is invisible to hit testing, so the
// pointer event goes through to the next target in the stack.
await tester.tapAt(tester.getCenter(ignorePointer));
await tester.pump();
expect(find.text('Taps received: 1'), findsOneWidget);
});
}
11 changes: 8 additions & 3 deletions packages/flutter/lib/src/widgets/basic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7705,9 +7705,14 @@ class IgnorePointer extends SingleChildRenderObjectWidget {
/// {@youtube 560 315 https://www.youtube.com/watch?v=65HoWqBboI8}
///
/// {@tool dartpad}
/// The following sample has an [AbsorbPointer] widget wrapping the button on
/// top of the stack, which absorbs pointer events, preventing its child button
/// __and__ the button below it in the stack from receiving the pointer events.
/// The following sample shows an [AbsorbPointer] and an [IgnorePointer] side
Comment thread
victorsanni marked this conversation as resolved.
Resolved
/// by side, each wrapping a box that partially covers a tappable target
/// behind it in a stack. Tapping the overlapping region on the
/// [AbsorbPointer] side does nothing: the [AbsorbPointer] absorbs the pointer
/// events itself, so neither its child nor the target behind it receives the
/// tap. Tapping the same region on the [IgnorePointer] side taps the target
/// behind: the [IgnorePointer] is invisible to hit testing, so the pointer
/// event goes through to the next target in the stack.
///
/// ** See code in examples/api/lib/widgets/basic/absorb_pointer.0.dart **
/// {@end-tool}
Expand Down