android - showing black screen on backpress Flutter
问题描述
I am using Webview
with WillPopScope
, webview
internal back button is working fine but on the first page back press is not working.
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SafeArea(child: MyHomePage()),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
InAppWebViewController _controller;
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () => canGoBack(context),
child: Scaffold(
body: InAppWebView(
initialUrlRequest:
URLRequest(url: Uri.parse("https://www.google.com/")),
onWebViewCreated: onWebviewCreated,
),
),
);
}
void onWebviewCreated(InAppWebViewController controller) {
_controller = controller;
}
canGoBack(BuildContext context) async {
if (await _controller.canGoBack()) {
_controller.goBack();
return Future.value(false);
} else {
return Future.value(true);
}
}
}
tried with
canGoBack(BuildContext context) async {
if (await _controller.canGoBack()) {
_controller.goBack();
return Future.value(false);
} else {
Navigator.pop(context, true);
return Future.value(false);
}
}
the issue is showing the black if there is no webview history.
解决方案
您没有任何以前的根并且您正在使用
Navigator.pop(context, true); // in canGoBack function
这就是问题的原因。删除该行,它将起作用。
推荐阅读
- java - 执行创建时出错 - HHH000437 尝试保存一个或多个
- javascript - 向 JS 中的 onClick 事件添加声音?
- reactjs - 取消关注按钮不会删除
- python - 将用户输入从一个 Python 脚本传递到另一个
- doctrine - 无法在我的 symfony 4 项目中使用带有 postgres 数据库的 Acos 学说扩展
- layout - 用 ipycytoscape 绘制图形,节点数有问题
- excel - 使用 Range 而不是单个值的 IF 语句循环
- django - Django:如何将自定义字段添加到表单保存的数据中?
- r - 为什么每个组的 difftime 的输出不是从零开始
- kotlin - Kotlin REPL 对算术运算给出了奇怪的答案