Home > database >  Handling Android's native back button on flutter_split_view
Handling Android's native back button on flutter_split_view

Time:01-12

I'm currently developing an app which uses the flutter_split_view plugin to automatically display split view. There's one main annoyance, though, pressing the Android's native back button from the child screen (i.e. the right widget) simply closes the app.

I have tried using WillPopScope to call SplitView.of(context) on the child screen, because the SplitView constructor does not accept external controllers (e.g. TabController for tabs) which I could call to redirect the back button calls to the SplitView instead.

import 'package:flutter/material.dart';
import 'package:flutter_split_view/flutter_split_view.dart';

class ChildPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SplitViewState splitView = SplitView.of(context);
    return WillPopScope(
      onWillPop: () async {
        splitView.pop();
        return false;
      },
      child: Scaffold(...),
    );
  }
}

Is there a way to solve this?

CodePudding user response:

You can use the WillPopScope widget to capture the back button press event, and then use the splitView.pop() method to navigate to the previous screen, instead of closing the app.

By wrapping the Scaffold widget with a WillPopScope widget, you can intercept the back button press event, and then use the splitView.pop() method to navigate to the previous screen. The onWillPop callback should return Future, returning false will prevent the back button from doing its default action, in this case close the app. And that way you can redirect the back button calls to the SplitView instead.

This should work as expected if implemented correctly. However, you should test it on the actual device to make sure the behavior is the one you expect.

CodePudding user response:

  • Don't think that split_view can help you split the current page into several single running StateFullWidget's. All the pages you see are only controlled by the context of the current page. So, the pop you reference in the subclassed widget will only apply to the current page. If the current page is the last page, there will be no page after running pop, so the program exits
  • Related