Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to go to the previous page using the button on the smartphone in Flutter WebView?

I implemented a floatingActionButton to move to the previous page as shown in the code below.

Is it possible to use the back button on a smartphone to move to the previous page without using floatingActionButton?

This is the source code I implemented.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

enter image description here

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 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,
        ),
      ),
    );
  }
}

>Solution :

To manage back key press in Flutter, either as a drag gesture or with the -> button in your img :

PopScope

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,
      ),
    ),
  ),
);

deprecated! WillPopScope also may Works

‘WillPopScope’ is deprecated and shouldn’t be used. Use PopScope instead. This feature was deprecated after v3.12.0-1.0.pre. (Documentation)

I hope it help you 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading