Unverified Commit 67a9ae17 authored by Todd Volkert's avatar Todd Volkert Committed by GitHub

Update Actions.invoke to return the result of invoking the action. (#62012)

The boolean value as to whether the action was found and enabled
is less useful than just returning the result of invoking the action
itself.
parent 629395f7
......@@ -581,7 +581,13 @@ class Actions extends StatefulWidget {
/// that are found.
///
/// Setting `nullOk` to true means that if no ambient [Actions] widget is
/// found, then this method will return false instead of throwing.
/// found, then this method will return null instead of throwing.
///
/// Returns the result of invoking the action's [Action.invoke] method. If
/// no action mapping was found for the specified intent, or if the action
/// that was found was disabled, then this returns null. Callers can detect
/// whether or not the action is available (found, and not disabled) using
/// [Actions.find] with its `nullOk` argument set to true.
static Object invoke<T extends Intent>(
BuildContext context,
T intent, {
......@@ -624,7 +630,7 @@ class Actions extends StatefulWidget {
}());
// Invoke the action we found using the relevant dispatcher from the Actions
// Element we found.
return actionElement != null ? _findDispatcher(actionElement).invokeAction(action, intent, context) != null : null;
return actionElement != null ? _findDispatcher(actionElement).invokeAction(action, intent, context) : null;
}
@override
......
......@@ -423,6 +423,36 @@ void main() {
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.forbidden);
});
testWidgets('Actions.invoke returns the value of Action.invoke', (WidgetTester tester) async {
final GlobalKey containerKey = GlobalKey();
final Object sentinel = Object();
bool invoked = false;
const TestIntent intent = TestIntent();
final Action<Intent> testAction = TestAction(
onInvoke: (Intent intent) {
invoked = true;
return sentinel;
},
);
await tester.pumpWidget(
Actions(
dispatcher: TestDispatcher(postInvoke: collect),
actions: <Type, Action<Intent>>{
TestIntent: testAction,
},
child: Container(key: containerKey),
),
);
await tester.pump();
final Object result = Actions.invoke<TestIntent>(
containerKey.currentContext,
intent,
);
expect(identical(result, sentinel), isTrue);
expect(invoked, isTrue);
});
});
group('Listening', () {
......@@ -484,7 +514,7 @@ void main() {
const TestIntent(),
);
expect(enabled1, isFalse);
expect(result, isFalse);
expect(result, isNull);
expect(invoked1, isFalse);
action1.enabled = true;
......@@ -523,7 +553,7 @@ void main() {
);
expect(enabledChanged, isNull);
expect(enabled2, isFalse);
expect(result, isFalse);
expect(result, isNull);
expect(invoked2, isFalse);
action2.enabled = true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment