OverlayStore constructor

OverlayStore({
  1. required Widget builder(
    1. BuildContext
    ),
  2. required OverlayController controller,
  3. bool dismissOnBoundary = true,
})

OverlayStore handles pushing overlays to multiple contexts and popping them appropriately, or upon user request.

This class works alongside the OverlayController to acheive the desirable results.

Implementation

OverlayStore({
  required this.builder,
  required this.controller,
  bool dismissOnBoundary = true,
}) : _dismissOnBoundary = dismissOnBoundary {
  controller._push = (ctx) {
    if (Overlay.of(ctx) != null) {
      _contexts.add(ctx);
      _backgrounds.add(
        _dismissOnBoundary
            ? OverlayEntry(
                builder: (context) => Positioned.fill(
                  child: GestureDetector(
                    onTap: () => controller.pop(_contexts.last),
                    child: Container(
                      color: Colors.black.withOpacity(0.2),
                    ),
                  ),
                ),
              )
            : null,
      );
      _overlayEntries
          .add(OverlayEntry(builder: (context) => builder(context)));
      List<OverlayEntry> entries = [];
      if (_backgrounds.last != null) {
        entries.add(_backgrounds.last!);
      }
      entries.add(_overlayEntries.last);
      Overlay.of(ctx)!.insertAll(entries);
    }
  };
  controller._pop = (ctx) {
    var i = _contexts.indexOf(ctx);
    if (i >= 0) {
      if (_backgrounds[i] != null) {
        _backgrounds[i]!.remove();
      }
      _backgrounds.removeAt(i);
      _overlayEntries[i].remove();
      _overlayEntries.removeAt(i);
      _contexts.removeAt(i);
    } else {
      debugPrint('OverlayBuilder: No overlays found in the provided context');
    }
  };
}