Skip to content

Commit 4adbf28

Browse files
Fix Actions.handler to forward the intent type to maybeFind (flutter#191052)
`Actions.handler` was calling `Actions.maybeFind(context)` without an explicit type argument or the intent, so downward inference resolved `T` to `Intent` and the action lookup used the literal `Intent` type instead of the intent's concrete runtime type. Since apps register actions keyed by their concrete `Intent` subtype, the lookup always missed and `handler` returned `null` for every intent. This regressed in flutter#180883 and first shipped in 3.47.0. The fix forwards the type and intent explicitly: `Actions.maybeFind<T>(context, intent: intent)`. ## Tests Adds regression tests for `Actions.handler`: it now finds and invokes the bound action for the intent's runtime type, and still returns null when the action is disabled or when no matching action is found. The first test fails without the fix. Fixes flutter#191045. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md Co-authored-by: Victor Sanni <[email protected]>
1 parent a720282 commit 4adbf28

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎packages/flutter/lib/src/widgets/actions.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ class Actions extends StatefulWidget {
809809
/// returned callback is called. If the return value is needed, consider using
810810
/// [Actions.invoke] instead.
811811
static VoidCallback? handler<T extends Intent>(BuildContext context, T intent) {
812-
final Action<Intent>? action = Actions.maybeFind(context);
812+
final Action<Intent>? action = Actions.maybeFind<T>(context, intent: intent);
813813
if (action != null && action._isEnabled(intent, context)) {
814814
return () {
815815
// Could be that the action was enabled when the closure was created,

‎packages/flutter/test/widgets/actions_test.dart‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,64 @@ void main() {
316316
expect(Actions.maybeFind<DoNothingIntent>(containerKey.currentContext!), isNull);
317317
});
318318

319+
testWidgets('Actions.handler forwards the intent type and finds the bound action', (
320+
WidgetTester tester,
321+
) async {
322+
// Regression test for https://github.com/flutter/flutter/issues/191045.
323+
final GlobalKey containerKey = GlobalKey();
324+
var invoked = false;
325+
final testAction = TestAction(
326+
onInvoke: (Intent intent) {
327+
invoked = true;
328+
return invoked;
329+
},
330+
);
331+
await tester.pumpWidget(
332+
Actions(
333+
actions: <Type, Action<Intent>>{TestIntent: testAction},
334+
child: Container(key: containerKey),
335+
),
336+
);
337+
338+
final VoidCallback? handler = Actions.handler(
339+
containerKey.currentContext!,
340+
const TestIntent(),
341+
);
342+
expect(handler, isNotNull);
343+
344+
handler!();
345+
expect(invoked, isTrue);
346+
});
347+
348+
testWidgets('Actions.handler returns null when the action is disabled', (
349+
WidgetTester tester,
350+
) async {
351+
final GlobalKey containerKey = GlobalKey();
352+
final testAction = TestAction(onInvoke: (Intent intent) => null)..enabled = false;
353+
await tester.pumpWidget(
354+
Actions(
355+
actions: <Type, Action<Intent>>{TestIntent: testAction},
356+
child: Container(key: containerKey),
357+
),
358+
);
359+
360+
expect(Actions.handler(containerKey.currentContext!, const TestIntent()), isNull);
361+
});
362+
363+
testWidgets('Actions.handler returns null when no matching action is found', (
364+
WidgetTester tester,
365+
) async {
366+
final GlobalKey containerKey = GlobalKey();
367+
await tester.pumpWidget(
368+
Actions(
369+
actions: const <Type, Action<Intent>>{},
370+
child: Container(key: containerKey),
371+
),
372+
);
373+
374+
expect(Actions.handler(containerKey.currentContext!, const TestIntent()), isNull);
375+
});
376+
319377
testWidgets('FocusableActionDetector keeps track of focus and hover even when disabled.', (
320378
WidgetTester tester,
321379
) async {

0 commit comments

Comments
 (0)