skip to Main Content

I use this code for flutter app when I press the back button from home screen in android device than it go back to splash screen. How can I stop this?

abstract class Routes {
  Routes._();
  static const HOME = _Paths.HOME;
}

abstract class _Paths {
  _Paths._();
  static const HOME = '/home';
}





class AppPages {
AppPages._();

static const INITIAL = Routes.HOME;

static final routes = [
GetPage(
name: _Paths.HOME,
page: () => const SplashView(),
binding: HomeBinding(),
),
];
}


2

Answers


  1. Try wrapping your entire screen with this custom widget (recommend extracting it to its own file tho and just calling it by name):

    import 'package:back_button_interceptor/back_button_interceptor.dart';
    import 'package:flutter/material.dart';
    
    class NavBlocker extends StatefulWidget {
      const NavBlocker({
        super.key,
        required this.blocking,
        required this.child,
      });
    
      final bool blocking;
      final Widget child;
    
      @override
      State<NavBlocker> createState() => _NavBlockerState();
    }
    
    class _NavBlockerState extends State<NavBlocker> {
      @override
      void initState() {
        super.initState();
        BackButtonInterceptor.add(myInterceptor);
      }
    
      @override
      void dispose() {
        BackButtonInterceptor.remove(myInterceptor);
        super.dispose();
      }
    
      bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) {
        return widget.blocking;
      }
    
      @override
      Widget build(BuildContext context) {
        return widget.child;
      }
    }
    

    … then just set the blocking bool to be either true or false.

    Login or Signup to reply.
  2. i use Go_route package to manage navigation .. and i get rid of the onboarding page like :enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search