コード例 #1
0
 public bool invokeAction(UiWidgetAction action, Intent intent, FocusNode focusNode = null)
 {
     D.assert(action != null);
     D.assert(intent != null);
     focusNode = focusNode ?? FocusManagerUtils.primaryFocus;
     if (action != null && intent.isEnabled(focusNode.context))
     {
         action.invoke(focusNode, intent);
         return(true);
     }
     return(false);
 }
コード例 #2
0
        public static bool invoke(
            BuildContext context,
            Intent intent,
            FocusNode focusNode = null,
            bool nullOk         = false
            )
        {
            D.assert(context != null);
            D.assert(intent != null);
            Element        actionsElement = null;
            UiWidgetAction action         = null;

            bool visitAncestorElement(Element element)
            {
                if (!(element.widget is Actions))
                {
                    return(true);
                }
                actionsElement = element;
                Actions actions = element.widget as Actions;

                action = actions.actions.getOrDefault(intent.key)?.Invoke();

                return(action == null);
            }

            context.visitAncestorElements(visitAncestorElement);
            D.assert(() => {
                if (nullOk)
                {
                    return(true);
                }

                if (actionsElement == null)
                {
                    throw new UIWidgetsError($"Unable to find a {typeof(Actions)} widget in the context.\n" +
                                             "Actions.invoke() was called with a context that does not contain an " +
                                             $"{typeof(Actions)} widget.\n" +
                                             $"No {typeof(Actions)} ancestor could be found starting from the context that " +
                                             "was passed to Actions.invoke(). This can happen if the context comes " +
                                             "from a widget above those widgets.\n" +
                                             "The context used was:\n" +
                                             $"  {context}");
                }

                if (action == null)
                {
                    throw new UIWidgetsError(
                        $"Unable to find an action for an intent in the {typeof(Actions)} widget in the context.\n" +
                        "Actions.invoke() was called on an {typeof(Actions)} widget that doesn't " +
                        "contain a mapping for the given intent.\n" +
                        "The context used was:\n" +
                        $"  {context}\n" +
                        "The intent requested was:\n" +
                        $" {intent}");
                }

                return(true);
            });
            if (action == null)
            {
                return(false);
            }

            return(_findDispatcher(actionsElement).invokeAction(action, intent, focusNode: focusNode));
        }