Ce n’est pas encore bien documenté, mais Flutter propose un mécanisme assez proche de la propagation d’évènements façon DOM : il s’agit des notifications.

Définir un type de notification

Le principe est d’étendre la classe Notification pour définir un signal, et éventuellement y associer des données à transmettre aux composants parents.

class ColorNotification extends Notification {
  final Color color;
  ColorNotification(this.color);
}

Émettre une notification

On crée ici un composant ColorBox, contenant un InkWell, émettant une ColorNotification à chaque tap.

class ColorBox extends StatelessWidget {
  final Color color;
  final ColorNotification notif;

  ColorBox(this.color) : notif = new ColorNotification(color);

  @override
  Widget build(BuildContext context) => new Padding(
    padding: new EdgeInsets.symmetric(horizontal: 20.0),
    child: new InkWell(
        onTap: () => notif.dispatch(context),
        child: new Container(
          color: color,
          width: 100.0,
          height: 100.0,
        )));
}

Après dispatch(), la notif est “signalée” aux écouteurs de notifications, NotificationListener, déclarés dans les widgets parents.

Écoute de notifications

Pour déclarer un écouteur de Notification, on rajoute une “couche” **NotificationListener**.

@override
  Widget build(BuildContext context) =>  new NotificationListener<ColorNotification>(
    onNotification: onColorNotif, child: getColorBoxes());

  bool onColorNotif(ColorNotification notification) {
    setState(() => selectedColor = notification.color);
    return false; // arrête le *bubbling* de la notification
  }

Bubbling

Le callback onColorNotif doit renvoyer un booléen indiquant si la propagation doit être poursuivie.

Communication widget <-> children : alternatives

En ce qui concernent la transmission de données entre composants, il me semble que les exemples officiels utilisent principalement des VoidCallBack, des ChangeNotifier ou des ValueNotifier.

On peut retrouver quelques Notifications dans le code source du framework, en particulier autour de la gestion du scrolling ( ScrollNotification ) et du layout (SizeChangedLayoutNotification).

=> Sources de l’exemple