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.
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 🙂

