如何使设备后退按钮转到 flutter webview 中的上一页

How to make device back button to go to the previous page in flutter webview

我这里有一些简单的代码,只显示一个网页。我想这样做,按下设备后退按钮会将您带到网络视图上的上一页。我尝试在线寻找解决方案,但无法将其中任何一个集成到我的代码中。请帮助我。

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Mobile App",
      home: Scaffold(
          body: SafeArea(
              child: WebView(
        initialUrl: 'https://flutter.dev',
        javascriptMode: JavascriptMode.unrestricted,
      ))),
      debugShowCheckedModeBanner: false,
    );
  }

}

用这个小部件包裹脚手架

WillPopScope(
  onWillPop: controller.onBackPressedHandler,
  child: Scaffold( ... )

Future<bool> onBackPressedHandler() {

return Future.value(false);} //return false disables default functionality

您可以在 onBackPressedHandler 函数中控制后退按钮功能。 对于你的情况,你应该把这行代码放在里面

webviewController.goBack();