What is an Overlay?
An overlay in Flutter lets you show something that floats on top of your regular page content. Think of things like pop-ups, tooltips, or loading indicators that appear over everything else.
Why use Overlays?
They are useful when you need to display information or controls that aren't part of the standard page flow and need to be visible above everything else.
Where do Overlays work?
To use Overlay.of
, your app needs to be inside a MaterialApp
or have a Navigator
higher up in the widget tree. This gives Flutter a place to show the overlay.
How to Show an Overlay
Here's a simple way to put something like a widget on top of your screen:
- First, you create an
OverlayEntry
. This entry holds the widget you want to show.OverlayEntry overlayEntry = OverlayEntry( opaque: false, // set to true if it covers the whole screen below it builder: (context) => Container(...), // Put the widget you want to show here );
- Then, you find the
Overlay
for the current screen usingOverlay.of(context)
.final overlay = Overlay.of(context);
- Finally, you insert your
OverlayEntry
into theOverlay
.if (overlay != null) { overlay.insert(overlayEntry); }
This will place your Container(...)
(or whatever widget you used) on top of your app's content.