Skip to content

Commit 8d64376

Browse files
authored
enableUserInteractionTracing sometimes finds the wrong widget (#1212)
1 parent 3141b75 commit 8d64376

File tree

3 files changed

+108
-55
lines changed

3 files changed

+108
-55
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
### Fixes
66

7-
- Fix: Only call method channels on native platforms ([#1196](https://github.com/getsentry/sentry-dart/pull/1196))
7+
- enableUserInteractionTracing sometimes finds the wrong widget ([#1212](https://github.com/getsentry/sentry-dart/pull/1212))
8+
- Only call method channels on native platforms ([#1196](https://github.com/getsentry/sentry-dart/pull/1196))
89

910
### Dependencies
1011

flutter/lib/src/user_interaction/sentry_user_interaction_widget.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/cupertino.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:flutter/rendering.dart';
34
import 'package:meta/meta.dart';
45

56
import '../../sentry_flutter.dart';
@@ -257,6 +258,13 @@ class _SentryUserInteractionWidgetState
257258
if (renderObject == null) {
258259
return;
259260
}
261+
var hitFound = true;
262+
if (renderObject is RenderPointerListener) {
263+
final hitResult = BoxHitTestResult();
264+
265+
// Returns false if the hit can continue to other objects below this one.
266+
hitFound = renderObject.hitTest(hitResult, position: position);
267+
}
260268

261269
final transform = renderObject.getTransformTo(rootElement.renderObject);
262270
final paintBounds =
@@ -268,7 +276,7 @@ class _SentryUserInteractionWidgetState
268276

269277
tappedWidget = _getDescriptionFrom(element);
270278

271-
if (tappedWidget == null) {
279+
if (tappedWidget == null || !hitFound) {
272280
element.visitChildElements(elementFinder);
273281
}
274282
}

flutter/test/user_interaction/sentry_user_interaction_widget_test.dart

Lines changed: 97 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void main() {
201201
currentTimer = tracer.autoFinishAfterTimer;
202202
});
203203

204-
await tapMe(tester, sut, 'btn_1');
204+
await tapMe(tester, sut, 'btn_1', pumpWidget: false);
205205

206206
Timer? autoFinishAfterTimer;
207207
fixture.hub.configureScope((scope) {
@@ -226,7 +226,7 @@ void main() {
226226
currentTracer = (scope.span as SentryTracer);
227227
});
228228

229-
await tapMe(tester, sut, 'btn_2');
229+
await tapMe(tester, sut, 'btn_2', pumpWidget: false);
230230

231231
SentryTracer? tracer;
232232
fixture.hub.configureScope((scope) {
@@ -238,8 +238,15 @@ void main() {
238238
});
239239
}
240240

241-
Future<void> tapMe(WidgetTester tester, Widget widget, String key) async {
242-
await tester.pumpWidget(widget);
241+
Future<void> tapMe(
242+
WidgetTester tester,
243+
Widget widget,
244+
String key, {
245+
bool pumpWidget = true,
246+
}) async {
247+
if (pumpWidget) {
248+
await tester.pumpWidget(widget);
249+
}
243250

244251
await tester.tap(find.byKey(Key(key)));
245252
}
@@ -278,59 +285,96 @@ class MyApp extends StatelessWidget {
278285
Widget build(BuildContext context) {
279286
return MaterialApp(
280287
title: 'Welcome to Flutter',
281-
home: Scaffold(
282-
appBar: AppBar(
283-
title: const Text('Welcome to Flutter'),
284-
),
285-
body: Center(
286-
child: Column(
287-
children: [
288-
MaterialButton(
289-
key: Key('btn_1'),
290-
onPressed: () {
291-
// print('button pressed');
292-
},
293-
child: const Text('Button 1'),
294-
),
295-
CupertinoButton(
296-
key: Key('btn_2'),
297-
onPressed: () {
298-
// print('button pressed 2');
299-
},
300-
child: const Text('Button 2'),
288+
home: Page1(),
289+
routes: {'page2': (context) => const Page2()},
290+
);
291+
}
292+
}
293+
294+
class Page1 extends StatelessWidget {
295+
const Page1({Key? key}) : super(key: key);
296+
297+
@override
298+
Widget build(BuildContext context) {
299+
return Scaffold(
300+
body: Center(
301+
child: Column(
302+
children: [
303+
MaterialButton(
304+
key: Key('btn_1'),
305+
onPressed: () {
306+
// print('button pressed');
307+
},
308+
child: const Text('Button 1'),
309+
),
310+
CupertinoButton(
311+
key: Key('btn_2'),
312+
onPressed: () {
313+
// print('button pressed 2');
314+
},
315+
child: const Text('Button 2'),
316+
),
317+
IconButton(
318+
key: Key('btn_3'),
319+
onPressed: () {
320+
// print('button pressed 3');
321+
},
322+
icon: Icon(
323+
Icons.dark_mode,
324+
semanticLabel: 'My Icon',
301325
),
302-
IconButton(
303-
key: Key('btn_3'),
304-
onPressed: () {
305-
// print('button pressed 3');
326+
),
327+
Card(
328+
child: GestureDetector(
329+
key: Key('btn_4'),
330+
onTap: () => {
331+
// print('button pressed 4'),
306332
},
307-
icon: Icon(
308-
Icons.dark_mode,
309-
semanticLabel: 'My Icon',
333+
child: Stack(
334+
children: [
335+
//fancy card layout
336+
ElevatedButton(
337+
key: Key('btn_5'),
338+
onPressed: () => {
339+
// print('button pressed 5'),
340+
},
341+
child: const Text('Button 5'),
342+
),
343+
],
310344
),
311345
),
312-
Card(
313-
child: GestureDetector(
314-
key: Key('btn_4'),
315-
onTap: () => {
316-
// print('button pressed 4'),
317-
},
318-
child: Stack(
319-
children: [
320-
//fancy card layout
321-
ElevatedButton(
322-
key: Key('btn_5'),
323-
onPressed: () => {
324-
// print('button pressed 5'),
325-
},
326-
child: const Text('Button 5'),
327-
),
328-
],
329-
),
330-
),
331-
)
332-
],
333-
),
346+
),
347+
MaterialButton(
348+
key: Key('btn_go_to_page2'),
349+
onPressed: () {
350+
Navigator.of(context).pushNamed('page2');
351+
},
352+
child: const Text('Go to page 2'),
353+
),
354+
],
355+
),
356+
),
357+
);
358+
}
359+
}
360+
361+
class Page2 extends StatelessWidget {
362+
const Page2({Key? key}) : super(key: key);
363+
364+
@override
365+
Widget build(BuildContext context) {
366+
return Scaffold(
367+
body: Center(
368+
child: Column(
369+
children: [
370+
MaterialButton(
371+
key: Key('btn_page_2'),
372+
onPressed: () {
373+
// print('button page 2 pressed');
374+
},
375+
child: const Text('Button Page 2'),
376+
),
377+
],
334378
),
335379
),
336380
);

0 commit comments

Comments
 (0)