The paper

Now that the principle of Aspectd is clear, let’s try to implement the full buried point of Flutter

Gets the clicked button

 @Execute("package:flutter/src/gestures/binding.dart", "GestureBinding",
      "-dispatchEvent")
  @pragma("vm:entry-point")
  dynamic hookHitTest(PointCut pointCut) {
    PointerEvent pointEvent = pointCut.positionalParams[0];
    HitTestResult hitTestResult = pointCut.positionalParams[1];
    if (pointEvent is PointerUpEvent) {
      HookImpl.getInstance().hookHitTest(hitTestResult.path.first, pointEvent);
    }
    return pointCut.proceed();
  }
Copy the code

The PointerEvent and HitTestResult parameters passed to the GestureBinding dispatchEvent method are retrieved.

Blocking click events

@Execute("package:flutter/src/gestures/recognizer.dart", "GestureRecognizer", "-invokeCallback") @pragma("vm:entry-point") dynamic hookInvokeCallback(PointCut pointCut) { dynamic result = pointCut.proceed(); dynamic eventName = pointCut.positionalParams[0]; Print ("GestureRecognizer:::::invokeCallback"); HookImpl.getInstance().hookClick(eventName); return result; }Copy the code

GestureRecognizer invokeCallback == “onTap”

void hookClick(String eventName) { if (eventName == "onTap") { initValues(); _getElementPath(); _getElementType(); _getElementContent(); _printClick(elementInfoMap); _resetValues(); }}Copy the code

Locate whether a Widget was written by itself

This can be changed by referring to the Track_widget_constructor_locations class of Flutter.

kernel/transformations/track_widget_constructor_locations.dart
Copy the code

Changes to this class:

If (importuri.path. contains('hook_impl.dart')) {for (Class class_ in library.classes) {// If (importuri.path. contains('hook_impl.dart')) {// If (importuri.path. contains('hook_impl.dart')) {for (Class class_ in library.classes) {// Access to all the classes, if you have _CustomHasCreationLocation, so for _CustomHasCreationLocation _hasCreationLocationClass assignment, Similarly if below (class_. Name = = '_CustomHasCreationLocation') {_hasCreationLocationClass = class_; } else if (class_.name == '_CustomLocation') { _locationClass = class_; }}}Copy the code

What does _hasCreationLocationClass do

Void _transformClassImplementingWidget (Class clazz) {/ / here is for us to write components added a SuperType _CustomHasCreationLocation clazz.implementedTypes .add(new Supertype(_hasCreationLocationClass, <DartType>[])); }Copy the code

So that we can figure out whether the Widget _CustomHasCreationLocation type, you can know if the Widget is written by ourselves.

The effect

Because most are according to the big guy article implementation, so do not do too much analysis.

Githbu address