skip to Main Content

When you press the back button, you can go to the previous page in the webview. Additionally, we would like to add that when you double-press back, the app will close.

https://pub.dev/packages/double_back_to_close_app

I think I can use this, but I don’t know how to apply both functions at the same time.

This is the code I created.

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


final uri = Uri.parse('https://google.com');

class HomeScreen extends StatelessWidget {

  WebViewController controller = WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setNavigationDelegate(NavigationDelegate(
      onPageFinished: (String url){
        print(url);
      }
    ))
    ..loadRequest(uri);

  HomeScreen({Key? key}) : super(key: key);


  @override
  Widget build(BuildContext context) {

    return PopScope(
      canPop: /* BOOL VALUE THAT CHECK IF ITS OK TO CLOSE APP OR NOT , YOU MAY PASS FALSE:)*/ false ,
      onPopInvoked: (didPop) {
        //FUNCTION THAT CALL WHEN EVER THE BACK CLICKED :)
        controller?.goBack();
      },
      child: Scaffold(

        floatingActionButton: Container(
          height: 40.0,
          width: 40.0,
          child: FittedBox(
            child: FloatingActionButton(
              child: Icon(Icons.arrow_back),
              onPressed: () => controller?.goBack(),
              backgroundColor:  Colors.grey.shade200,
            ),
          ),
        ),

        body: SafeArea(
          child : WebViewWidget(
            controller: controller,
          ),
        ),
      ),
    );

  }
}

2

Answers


  1. Use an if check for onPressed call. Check if there is any route available to go then call the
    Controller?.goBack(): SystemNavigator.pop();

    Using a ternary check will save space and it will be easy. However, you must implement manual logic to check the available routes.

    I hope It works.

    Login or Signup to reply.
  2. Define a currentPress variable above the build method like this:

    DateTime? currentPress;
    

    and use PopScope to handle the backpress

    PopScope(
          canPop: false,
          onPopInvoked: (didPop) async {
            final now = DateTime.now();
            if (currentPress == null ||
                now.difference(currentPress!) > const Duration(seconds: 2)) {
              currentPress = now;
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(
                  content: Text('Press back again to exit'),
                  duration: Duration(seconds: 1),
                ),
              );
              return;
            } else {
              controller?.goBack();
              SystemNavigator.pop();
            }
          },
          child:Scaffold(
    
            floatingActionButton: Container(
              height: 40.0,
              width: 40.0,
              child: FittedBox(
                child: FloatingActionButton(
                  child: Icon(Icons.arrow_back),
                  onPressed: () => controller?.goBack(),
                  backgroundColor:  Colors.grey.shade200,
                ),
              ),
            ),
    
            body: SafeArea(
              child : WebViewWidget(
                controller: controller,
              ),
            ),
          ),
        );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search