Flutter learning notes (35) -- Notification

For reprint, please indicate the source: Flutter learning notes (35) -- Notification

The NotificationListener of the notification is the same as the event Listener we wrote before. It is a functional component and bubbles up the widget tree from the child nodes. The difference is that the event Listener cannot be terminated, but the NotificationListener of the notification can be terminated.

Whether to terminate depends on the return value of NotificationListener.

Let's talk about my personal understanding:

Notification notification is sent through disPatch, just like event distribution in Android. When the NotificationListener listens for notification events, it will go to its onNotification callback and decide whether to continue to send notifications to the parent node according to the return value type (true or false) in the callback.

Return true to continue the distribution, return false to terminate the distribution, return false to mean that the NotificationListener of the upper node will not receive the notification event.

For example:

Two layers of notificationlisteners are nested. If the NotificationListener of the child node returns true, the NotificationListener of the parent node can receive notification events. If false is returned, the NotificationListener of the parent node will not receive notification events.

Let's take a look at the demo example:

demo is a simple way to send a notification, listening to the notification event and changing the content of the text.

1. To create an event Notification class that inherits Notification, it is actually a data carrier that defines the type and content of Notification data.

import 'package:flutter/material.dart';

class MyNotification extends Notification{
  String notificationStr;


2. Use of notificationlistener and distribution of notification events

import 'package:flutter/material.dart';
import 'package:study_app/util/MyNotification.dart';

class NotificationDemo extends StatefulWidget {
  State<StatefulWidget> createState() {
    return _NotificationDemoState();

class _NotificationDemoState extends State {
  String _notificationData = 'default_data';

  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'NotificationDemo',
      home: new Scaffold(
          appBar: AppBar(
            title: Text('NotificationDemo'),
          body: NotificationListener<MyNotification>(
            onNotification: (notification) {
              setState(() {
                _notificationData = notification.notificationStr;
              return true;
            child: Column(
              children: <Widget>[
                  builder: (context) {
                    return Container(
                      width: double.infinity,
                      child: RaisedButton(
                          child: Text('Send notifications'),
                          onPressed: () {

In reading, the author emphasizes a wrong way of writing, as shown in the notes below:

The reason is that the Notification needs a context parameter when it is distributed. This parameter refers to the context of the subwidget that Notification listens to. If you write according to the annotation method, the context is the root widget, which will result in no subwidget to be listened to.

So we need to build the context of our child widget s through Builder, which needs special attention here.

Finally, take a look at the screenshot:


above! If you have any questions, please leave a message!

Tags: Android

Posted on Fri, 19 Jun 2020 01:09:55 -0400 by rokchik