Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Android Back button action in FlutterFlow

For a page, I only see two options to perform actions - "On Page Load" & "On Phone Shake". But I need to perform an action on press of Android back button or back gesture (swipe from the edges). I can write my own custom action, but the problem is I am not aware how to add that trigger (on back press) in FlutterFlow.

Has anyone done similar thing? Any pointers?

FlutterFlow Action Flow Editor

like image 515
Shubhaw Kumar Avatar asked Aug 11 '26 00:08

Shubhaw Kumar


1 Answers

I got the same problem and found a work around.

  1. First you should make an new custom widget:

(don't run the code, it's just for a better overlook)

class BackButtonOverrider extends StatefulWidget {
  const BackButtonOverrider({
    super.key,
    this.width,
    this.height,
    required this.onBack,
  });

  final double? width;
  final double? height;
  final Future Function() onBack;

  @override
  State<BackButtonOverrider> createState() => _BackButtonOverriderState();
}

class _BackButtonOverriderState extends State<BackButtonOverrider> {
  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvoked: (popAllowed) async {
        widget.onBack.call();
      },
      child: Scaffold(
          // Your scaffold content here
          ),
    );
  }
}
  1. Add this widget to a page you want to override the back button.
  2. Do your action in the onBack parameter
  3. Be sure the 'Disable Android Back button' is disabled
like image 97
Jordy Vandemoortele Avatar answered Aug 12 '26 13:08

Jordy Vandemoortele