3

We all know about WillPopScope.

Example:

    return WillPopScope(
        onWillPop: () async => return false,
        child: Scaffold(...));

That works fine in most cases but not in mine. I'm using Beamer in a complicated way and MaterialApp.router so even if I use WillPopScope the back button still has an effect (exiting the app no matter where I'm at).

So I'd like to disable to back button at a deeper level. And the next level down looks like using the backButtonDispatcher in the MaterialApp.router like so:

class DoNothingBackButtonDispatcher extends BackButtonDispatcher
    with WidgetsBindingObserver {
  DoNothingBackButtonDispatcher();

  @override
  void addCallback(ValueGetter<Future<bool>> callback) {}

  @override
  void removeCallback(ValueGetter<Future<bool>> callback) {}

  @override
  Future<bool> didPopRoute() => invokeCallback(Future<bool>.value(false));

  @override
  ChildBackButtonDispatcher createChildBackButtonDispatcher() => ChildBackButtonDispatcher(this);


  @override
  void deferTo(ChildBackButtonDispatcher child) {}

  @override
  void forget(ChildBackButtonDispatcher child) {}

  @override
  Future<bool> invokeCallback(Future<bool> defaultValue) =>
      SynchronousFuture<bool>(false);

  bool onPop(BuildContext context, Route<dynamic> route) => false;

  @override
  void takePriority() {}
}

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);

  final rootBeamerDelegate = BeamerDelegate(
    initialPath: Sailor.initialPath,
    locationBuilder: RoutesLocationBuilder(
      routes: {
        '*': (context, state, data) => const HomePage(),
      },
    ),
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      debugShowCheckedModeBanner: false,
      routerDelegate: rootBeamerDelegate,
      routeInformationParser: BeamerParser(),
      backButtonDispatcher: DoNothingBackButtonDispatcher(), // <----- HERE
    );
  }
}

Unfortunately, even this closes the app when the user hits the system back button.

So, the question is: how do I implement the abstract BackButtonDispatcher class in order to essentially ignore the button press?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.