isWrappedInFocusWidget function

bool isWrappedInFocusWidget(
  1. InstanceCreationExpression node
)

Check if the GestureDetector is wrapped in a Focus or InkWell widget

Implementation

bool isWrappedInFocusWidget(InstanceCreationExpression node) {
  // Walk up the AST to find parent widget
  AstNode? current = node.parent;

  while (current != null) {
    if (current is NamedExpression && current.name.label.name == 'child') {
      // Check if the parent is a Focus or Focusable widget
      final grandParent = current.parent?.parent;
      if (grandParent is InstanceCreationExpression) {
        final parentType = grandParent.staticType?.element?.name;
        if (parentType == 'Focus' ||
            parentType == 'FocusableActionDetector' ||
            parentType == 'InkWell') {
          return true;
        }
        // Continue searching up the tree instead of breaking
      }
    }
    current = current.parent;
  }

  return false;
}