Site icon Flutter Packages | Pub dev Packages – Flutter Mobile App World

Flutter Overlay/OverlayEntry Class

Motivation

Flutter comes with two classes for manipulating “overlays”:

But OverlayEntry is very awkward to use. As opposed to most of the framework, OverlayEntry is not a widget (which comes with a nice and clean declarative API).

Instead, is uses an imperative API. This comes with a few drawbacks:

That’s where portal comes into play.

This library is effectively a reimplementation of Overlay/OverlayEntry, under the name Portal/PortalEntry (the name that React uses for overlays) while fixing all the previously mentioned issues.

Install

First, you will need to add portal to your pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  flutter_portal: ^0.0.1

Then, run flutter packages get in your terminal.

Usage

To use portal, we have to rely on two widgets:

Aligning the overlay around a widget

Sometimes, we want to align our overlay around another widget. PortalEntry comes with built-in support for this kind of feature.

For example, consider that we have a Text centered in our app:

Center(
  child: Text('whatever'),
)

If we wanted to add an overlay that is aligned on the top center of our Text, we would write:

Center(
  child: PortalEntry(
    portalAnchor: Alignment.bottomCenter,
    childAnchor: Alignment.topCenter,
    portal: Card(child: Text('portal')),
    child: Text('whatever'),
  ),
)

This will align the top-center of child with the bottom-center of portal, which renders the following:

Download Flutter Overlay/OverlayEntry widget source code on GitHub

https://github.com/rrousselGit/flutter_portal

Exit mobile version