back_button_interceptor
8.0.7, released yesterday by glasberg.dev
Intercept Android back-button taps with custom functions as an alternative to WillPopScope.
WASM-readyRock solid
What it does
Allows developers to add custom interceptor functions that are executed when the device's back button is tapped. If any interceptor returns true, the default navigation pop action is prevented. Functions can be ordered using z-indexes and named for easy removal.
AI-written summary of version 8.0.5, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need custom handling when the Android back button is pressed within stateful widgets.
- You want an alternative to WillPopScope with support for multiple ordered interceptors.
Skip it when
- You are using deep links with the GoRouter package, as it is currently incompatible.
- You are relying strictly on standard declarative navigation routing without custom back handling.
Strengths
- Supports multiple ordered interceptors via z-index and registration order.
- Allows both synchronous and asynchronous interceptor functions.
- Provides test-friendly methods to simulate back button presses and inspect results.
Weaknesses
- Incompatible with GoRouter deep links.
- Requires manual removal of interceptors in the dispose method to avoid memory leaks.
Setup
- Kind
- Flutter widgets
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 1
To make it work on Android 13 and up, set android:enableOnBackInvokedCallback="false" in the Android manifest.
@override
void initState() {
super.initState();
BackButtonInterceptor.add(myInterceptor);
}
@override
void dispose() {
BackButtonInterceptor.remove(myInterceptor);
super.dispose();
}
bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) {
print("BACK BUTTON!");
return true;
}Works with
Packages commonly used together with back_button_interceptor: ones it builds on, ones built on it, and its own family.
- loader_overlayBuilt on back_button_interceptorShow a loading overlay to prevent user interaction during async operations.
- one_contextBuilt on back_button_interceptorManage navigation, dialogs, overlays, and themes globally without a BuildContext.
- cool_uiBuilt on back_button_interceptorA collection of custom UI widgets for popovers, Weui toasts, and a customizable keyboard.