【问题标题】:In flutter how can I go back to previous URL when the back button is pressed?在颤振中,按下后退按钮时如何返回上一个 URL?
【发布时间】:2021-02-19 02:31:46
【问题描述】:

我对 Flutter 完全陌生,我正在使用 web 视图小部件,但每当我按下它时,它就会退出应用程序。按下后退按钮时如何返回上一页?

这是我的代码 sn-p

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(MyApp());
WebViewController controllerGlobal;

Future<bool> _exitApp(BuildContext context) async {
  if (await controllerGlobal.canGoBack()) {
    print("onwill goback");
    controllerGlobal.goBack();
    return Future.value(true);
  } else {
    Scaffold.of(context).showSnackBar(
      const SnackBar(content: Text("No back history item")),
    );
    return Future.value(false);
  }
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'TRAVMAKS',
      home: WillPopScope(
        onWillPop: () => _exitApp(context),
        child:Scaffold(
              body: Container(
                    margin: const EdgeInsets.only(top: 52.0),
                    child: Container(
                            margin: const EdgeInsets.only(top: 0.0),
                            child: WebView(
                                        initialUrl: 'https://www.google.com',
                                        javascriptMode: JavascriptMode.unrestricted,
                        )
              )
        )
    )
    )
    );

  }
}

【问题讨论】:

    标签: flutter dart mobile-application flutter-web flutterwebviewplugin


    【解决方案1】:

    在导航回您的WebView 后,您将返回true。如果你在onWillPop 中返回true,Flutter 会弹出路由,如果它是你的根路由,应用程序将退出。您可能也想在此处返回 false。

    https://api.flutter.dev/flutter/widgets/WillPopScope/onWillPop.html

    【讨论】:

      猜你喜欢
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 2016-10-02
      相关资源
      最近更新 更多