A Flutter package that provides a simple and powerful way to show custom overlays, toasts, banners, snackbars, dialogs etc. using the BuildContext.
Introduction
context_show simplifies the process of displaying temporary widgets on the screen. It extends BuildContext with a show() method that allows you to render any widget as an overlay, with full control over alignment, duration, and animations.
Say goodbye to boilerplate code for managing OverlayEntry and AnimationController.
Features
- πͺ Simple API: Show your widget with a single line of code:
context.show(...). - π― Flexible alignment β display widgets at any screen position (
top,bottom,center, etc.). - π¨ Customizable transitions β
fade,scale,slide,rotate, or compose your own. - π§© Composable animations β chain multiple transitions fluently
.fade().scale().rotation(). - ποΈ Programmatic control β close overlays from anywhere using
context.close()with flexible selectors. - πΌοΈ Custom Background: add custom backgrounds or animated backdrops.
- π Dismissible overlays β tap outside to close with ease.
- β±οΈ Auto-dismiss β control duration or disable with
Duration.zero. - β
Type-safe results β returns a
Future<T?>that resolves when the overlay closes. - β‘ Lightweight β zero dependencies, built on pure
Flutter.
Usage Examples
| Simple blue toast that slides up from the bottom and auto-dismisses after 4 seconds. | |
|---|---|
|
|
| Green banner that slides down from the top and can be only closed by clicking on the close icon button. | |
|
|
| Showing multiple banners with random color and random alignment and closing them with context.close() | |
|
|
| Showing Flutter logo in the center with a rotation animation, on a dimmed, dismissible background | |
|
|
| Displays a small red banner at the top center, aligned with the app bar and safe area insets. It slides in from below with a rotation effect, over a reddish, dismissible background | |
|
|
Closing Overlays Programmatically
The context.close() method allows you to close overlays from anywhere in your code. You can close individual overlays, multiple overlays, or all overlays at once using flexible selectors.
Basic Usage
// Close the last shown overlay (default behavior)
context.close();
Closing Specific Overlays
Use the Overlays class to target specific overlays:
// Close the first overlay
context.close(Overlays.first());
// or
context.close((overlays) => overlays.first);
// Close the last overlay
context.close();
// or
context.close(Overlays.last());
// or
context.close((overlays) => overlays.last);
// Close all overlays
context.close(Overlays.all());
// or
context.close((overlays) => overlays);
Closing Overlays by ID
Assign an id when showing an overlay, then close it by that ID:
// Show overlay with an ID
context.show(
(_) => MyWidget(),
id: 'my-banner',
);
// Close specific overlay by ID
context.close(Overlays.first(id: 'my-banner'));
//or
context.close((overlays) => overlays.byId('my-banner').first);
// Close all overlays with the same ID
context.close(Overlays.all(id: 'notification'));
//or
context.close((overlays) => overlays.byId('notification'));
Custom Selectors
Use a custom function to select which overlays to close:
// Close all overlays by ID using a custom selector
context.close((overlays) => overlays.byId('banner-1'));
// Close the first overlay matching a condition
context.close((overlays) => overlays.first);
// Close multiple overlays with custom logic
context.close((overlays) => overlays.where((o) => o.id?.startsWith('temp-') ?? false));
Returning Results from Overlays
You can return values when closing overlays from anywhere in your code:
class Page extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
ElevatedButton(
child: Text('Show'),
onPressed: () async {
// Show overlay and await the result
final result = await context.show<String>((_) => Text('Banner'));
print('$result'); // SOME RESULT
},
),
ElevatedButton(
child: Text('Close with Result'),
onPressed: () {
// Close overlay from a different callback and return some value:
context.close('SOME RESULT');
},
),
],
),
);
}
}
Real-World Example: No Internet Banner
// Show a "no internet" banner
void showNoInternetBanner(BuildContext context) {
context.show(
(_) => Container(
color: Colors.orange,
padding: EdgeInsets.all(20),
child: Text('No Internet Connection'),
),
id: 'no-internet',
duration: Duration.zero, // Won't close automatically
);
}
// Close it when connection is restored from anywhere in the app
// BuildContext can be completely different - any context works
void onConnectionRestored(BuildContext context) {
context.close((overlays) => overlays.byId('no-internet'));
// or use this syntax:
context.close(Overlays.all(id: 'no-internet'));
}
