【问题标题】:How to make device back button to go to the previous page in flutter webview如何使设备后退按钮转到flutter webview中的上一页
【发布时间】:2021-10-23 00:33:00
【问题描述】:

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

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

}

【问题讨论】:

    标签: flutter webview android-webview back-button


    【解决方案1】:

    用这个小部件包裹脚手架

    WillPopScope(
      onWillPop: controller.onBackPressedHandler,
      child: Scaffold( ... )
    
    Future<bool> onBackPressedHandler() {
    
    return Future.value(false);} //return false disables default functionality
    

    您可以在onBackPressedHandler 函数中控制返回按钮功能。 对于您的情况,您应该将这行代码放入其中

    webviewController.goBack();
    

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 2020-08-03
      • 2020-05-27
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      相关资源
      最近更新 更多