Directory portal:A Guide to The Quick Start to Flutter

In Flutter, there is a mechanism for transferring events from the lower part of the Widget tree to the upper part. This mechanism is called “notification bubbling”.

The implementation of this mechanism is NotificationListener.

Use NotificationListener to listen for event notifications in a variety of formats.

OnNotification: (notify) {switch (notify.runtimeType) {NotificationListener(// Here is a notification: (notify) {switch (notify.runtimeType) {case ScrollStartNotification:
          print("ScrollStart");
          break;
        case ScrollUpdateNotification:
          print("ScrollUpdate");
          break;
        case ScrollEndNotification:
          print("ScrollEnd");
          break;
        case OverscrollNotification:
          print("Overscroll");
          break;
        case LayoutChangedNotification:
          print("LayoutChanged");
          break;
        case SizeChangedLayoutNotification:
          print("SizeChangedLayout");
          break;
        case UserScrollNotification:
          print("UserScroll");
          break;
        case KeepAliveNotification:
          print("KeepAlive");
          break;
        case MyNotification:
          print("CustomScroll");
          break;
      }
    },
    child: MyWidget())
Copy the code

These events are emitted from nodes in the view tree starting with Child.

For example, slideable widgets emit event notifications from the ScrollNotification family. You can wrap them with NotificationListener and start receiving these event notifications.

Now look at how to send a custom event notification.

1. Customize a Notification

class MyNotification extends Notification {
  MyNotification(this.msg);
  final String msg;
}
Copy the code

2. Send notifications on child nodes

Builder(builder: (context) {
  returnGestureDetector(onTap: () {// Just call dispatch MyNotification('Haha! ').dispatch(context);
    },
    child: Container(
      width: 100,
      height: 100,
      color: Colors.blue,
    ),
  );
}
Copy the code

⚠️ Note that since dispatch() requires the node’s context, not the root context in build(BuildContext Context), you need to wrap the node Widget with Builder, To get the context for that location.

Directory Portals: A Guide to The Quick Start To Flutter

How can I be found?

Portal:CoorChice homepage

Portal:Lot of CoorChice