【发布时间】:2020-04-27 22:12:28
【问题描述】:
刚到这里,我的项目应用程序现在差不多好了。但由于某种原因,我无法回到以前的 webview。它似乎没有检测到我的水龙头。
顺便说一句,我正在使用 Flutter 开发团队的 WebView
我尝试过的:将 Scaffold 包裹在 WillPopScope 中 - 不起作用
另外,我的Future<bool> 有一个返回语句,但它似乎没有检测或读取该返回语句。
_exitApp 也是一条蓝色波浪线,表示我应该有一个返回语句,您可以在下面看到它,但由于某种原因它没有阅读它。
我的代码:
class HomePage extends StatefulWidget {
// HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@override
void initState() {
super.initState();
firebaseCloudMessagingListeners();
}
void firebaseCloudMessagingListeners() {
if (Platform.isIOS) iOSPermission();
_firebaseMessaging.getToken().then((token){
print(token);
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
setState(() {
print("${message['data']['url']}");
Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => NotificationClicked()));
});
},
onResume: (Map<String, dynamic> message) async {
print("${message['data']['url']}");
},
onLaunch: (Map<String, dynamic> message) async {
print("${message['data']['url']}");
},
);
}
void iOSPermission() {
_firebaseMessaging.requestNotificationPermissions(
IosNotificationSettings(sound: true, badge: true, alert: true)
);
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings)
{
print("Settings registered: $settings");
});
}
WebViewController myController;
final Completer<WebViewController> _controller =
Completer<WebViewController>();
/* The _exitApp also as a blue squiggly line which says I should have a return statement, below you can see it but for some reason its not reading it. */
Future<bool> _exitApp(BuildContext context) async {
if (await myController.canGoBack()) {
print("onwill goback");
myController.goBack();
} else {
Scaffold.of(context).showSnackBar(
const SnackBar(content: Text("No back history item")),
);
return Future.value(false);
}
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: WillPopScope(
onWillPop: () => _exitApp(context),
child: Scaffold(
body: WebView(
initialUrl: 'https://syncshop.online/en/',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (controller) {
_controller.complete(controller);
},
onPageFinished: (controller) async {
(await _controller.future).evaluateJavascript("document.getElementsByClassName('footer-container')[0].style.display='none';");
(await _controller.future).evaluateJavascript("document.getElementById('st_notification_1').style.display='none';");
(await _controller.future).evaluateJavascript("document.getElementById('sidebar_box').style.display='none';");
},
),
【问题讨论】:
-
在
_exitApp中if的第一部分没有返回语句 -
谢谢,但我仍然无法回到以前的 webview :( 我仍在尝试找到修复程序。我找到了这个 stackoverflow.com/questions/57878887/using-flutter-webview-as-home- and-pressing-back-button-closes-application 这是我尝试过的,但它仍然没有回到 webview 的上一个屏幕
-
为什么在你的脚手架中使用 WillPopScope
-
@Avinash 我也将它用于 WebView 但仍然无法正常工作。我仍在寻找解决此问题的方法。它没有关闭应用程序,但它没有返回到以前的 webview 网页
标签: android flutter flutter-layout flutter-web