我是完全陌生的人,我使用的是Web View窗口小部件,但是每当我按下它时,它都会退出应用程序。按下后退按钮时如何返回上一页?
这是我的代码段
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,
)
)
)
)
)
);
}
}
答案 0 :(得分:0)
您回到true
后返回WebView
。如果您在true
中返回onWillPop
,则Flutter会弹出路线,如果它是您的根路线,则该应用程序将退出。您可能还要在那里返回false。
https://api.flutter.dev/flutter/widgets/WillPopScope/onWillPop.html