Flutter Package: May be used to intercepting the Android back-button, as an alternative to `WillPopScope`.
back_button_interceptor
In simple cases, when you need to intercept the Android back-button, you usually add WillPopScope
to your widget tree. However, when developing stateful widgets that interact with the back button,
it’s more convenient to use the BackButtonInterceptor
.
You may add functions to be called when the back button is tapped. These functions may perform some useful work, and then, if any of them return true, the default button process (usually popping a Route) will not be fired.
In more detail: All added functions are called, in order. If any function returns true, the combined result is true, and the default button process will NOT be fired. Only if all functions return false (or null), the combined result is false, and the default button process will be fired.
Optionally, you may provide a z-index. Functions with a valid z-index will be called before functions with a null z-index, and functions with larger z-index will be called first. When they have the same z-index, functions added last are called first.
Each function gets the boolean stopDefaultButtonEvent
that indicates the current combined
result from all the previous functions. So, if some function doesn’t want to fire if some
other previous function already fired, it can do:
if (stopDefaultButtonEvent) return false;
The same result may be obtained if the optional ifNotYetIntercepted
parameter is true.
Then the function will only be called if all previous functions returned false
(that is, if stopDefaultButtonEvent
is false).
Note: After you’ve finished you MUST remove each function by calling the remove()
method.
Alternatively, you may also provide a name
when adding a function, and then later remove it
by calling the removeByName(name)
method.
Note: If any of your interceptors throws an error, a message will be printed to the console,
but the error will not be thrown. You can change the treatment of errors by changing the
static errorProcessing
field.
Note: If your functions need to know the current route in the navigator, you may use the helper
static method BackButtonInterceptor.getCurrentNavigatorRouteName(context)
.
Import the package
Add back_button_interceptor as a dependency in your pubspec.yaml. Then, import it:
import 'package:back_button_interceptor/back_button_interceptor.dart';
Examples
Intercepting
@override
void initState() {
super.initState();
BackButtonInterceptor.add(myInterceptor);
}
@override
void dispose() {
BackButtonInterceptor.remove(myInterceptor);
super.dispose();
}
bool myInterceptor(bool stopDefaultButtonEvent) {
print("BACK BUTTON!"); // Do some stuff.
return true;
}
Named function and z-index
@override
void initState() {
super.initState();
BackButtonInterceptor.add(myInterceptor, zIndex:2, name:"SomeName");
}
@override
void dispose() {
BackButtonInterceptor.removeByName("SomeName");
super.dispose();
}
bool myInterceptor(bool stopDefaultButtonEvent) {
print("BACK BUTTON!"); // Do some stuff.
return true;
}
Runnable Examples
- main Intercepts the back-button and prints a string to the console.
- main_complex_example The first screen has a button which opens a second screen. The second screen has 3 red squares. By tapping the Android back-button (or the “pop” button) each square turns blue, one by one. Only when all squares are blue, tapping the back-button once more will return to the previous screen.
- main_complex_example_test This package is test friendly, and this examples shows how to test the back button.
Testing
For testing purposes, the BackButtonInterceptor.popRoute()
method may be called directly,
to simulate pressing the back button. The list of all fired functions and their results is
recorded in the BackButtonInterceptor.results
static variable.
Debugging
In complex cases, to make it easier for you to debug your interceptors, you may print them to the console, with their names and z-indexes, by doing:
print(BackButtonInterceptor.describe());
See also:
- https://docs.flutter.io/flutter/widgets/WillPopScope-class.html
- https://stackoverflow.com/questions/45916658/de-activate-system-back-button-in-flutter-app-toddler-navigation
Download Flutter Package: May be used to intercept the Android back-button Source code on GitHub
https://github.com/marcglasberg/back_button_interceptor
Provides the list of the opensource Flutter apps collection with GitHub repository.