Android 设备后退按钮不返回到上一个webview
新来这里,我的项目应用程序几乎是好的。但由于某些原因,我无法回到以前的网络视图。它似乎没有检测到我的水龙头 顺便说一句,我正在使用颤振开发团队WebView 我尝试过的:在Android 设备后退按钮不返回到上一个webview,android,flutter,flutter-layout,flutter-web,Android,Flutter,Flutter Layout,Flutter Web,新来这里,我的项目应用程序几乎是好的。但由于某些原因,我无法回到以前的网络视图。它似乎没有检测到我的水龙头 顺便说一句,我正在使用颤振开发团队WebView 我尝试过的:在WillPopScope中包装Scaffold——不起作用 另外,我的Future有一个返回语句,但它似乎没有检测或读取该返回语句 _exitApp也是一条蓝色的曲线,上面写着我应该有一个返回语句,下面你可以看到它,但出于某种原因,它没有阅读它 我的代码: class HomePage extends StatefulWidg
WillPopScope
中包装Scaffold
——不起作用
另外,我的Future
有一个返回语句,但它似乎没有检测或读取该返回语句
_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';");
},
),
类主页扩展StatefulWidget{
//主页({Key}):超级(Key:Key);
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
最终FirebaseMessaging_FirebaseMessaging=FirebaseMessaging();
@凌驾
void initState(){
super.initState();
firebaseCloudMessagingListeners();
}
void firebaseCloudMessagingListeners(){
if(Platform.isIOS)iOSPermission();
_firebaseMessaging.getToken().then((令牌){
打印(代币);
});
_firebaseMessaging.configure(
onMessage:(映射消息)异步{
设置状态(){
打印(${message['data']['url']});
push(context,materialpage(builder:(BuildContext context)=>NotificationClicked());
});
},
onResume:(映射消息)异步{
打印(${message['data']['url']});
},
onLaunch:(映射消息)异步{
打印(${message['data']['url']});
},
);
}
作废许可证(){
_firebaseMessaging.requestNotificationPermissions(
IONotificationSettings(声音:真、徽章:真、警报:真)
);
_firebaseMessaging.onissettings已注册
.listen((IONotificationSettings)
{
打印(“已注册设置:$Settings”);
});
}
网络视图控制器;
最终完成器控制器=
完成符();
/*_exitApp也是一条蓝色的曲线,上面写着我应该有一个返回语句,下面你可以看到它,但出于某种原因,它没有阅读它*/
Future\u exitap(BuildContext上下文)异步{
if(等待myController.canGoBack()){
打印(“onwill goback”);
myController.goBack();
}否则{
Scaffold.of(上下文).showSnackBar(
const SnackBar(内容:文本(“无历史记录项”),
);
返回Future.value(false);
}
}
@凌驾
小部件构建(构建上下文){
返回安全区(
孩子:Willposcope(
onWillPop:()=>\u exitApp(上下文),
孩子:脚手架(
正文:WebView(
初始URL:'https://syncshop.online/en/',
javascriptMode:javascriptMode.unrestricted,
onWebViewCreated:(控制器){
_控制器。完成(控制器);
},
onPageFinished:(控制器)异步{
(wait _controller.future).evaluateJavascript(“document.getElementsByClassName('footer-container')[0].style.display='none';”;
evaluateJavascript(“document.getElementById('st_notification_1').style.display='none';”;
(wait _controller.future).evaluateJavascript(“document.getElementById('sidebar_box').style.display='none';”;
},
),
请检查此项,它可能会对您有所帮助
_exitApp(BuildContext context,Future<WebViewController> controller) async {
controller.then((data) async {
WebViewController controller= data;
var goback= await controller.canGoBack();
if (goback==true) {
// ignore: missing_return
print("onwill goback");
controller.goBack();
// ignore: missing_return
} else {
print("onwill not goback");
Navigator.pop(context);
}
}, onError: (e) {
print(e);
});
请检查这个,它可能会帮助你
_exitApp(BuildContext context,Future<WebViewController> controller) async {
controller.then((data) async {
WebViewController controller= data;
var goback= await controller.canGoBack();
if (goback==true) {
// ignore: missing_return
print("onwill goback");
controller.goBack();
// ignore: missing_return
} else {
print("onwill not goback");
Navigator.pop(context);
}
}, onError: (e) {
print(e);
});
在
\u exitApp
中,如果谢谢,则的第一部分中没有返回语句,但我仍然无法返回到以前的Web视图:(我仍在努力寻找解决方案。我找到了这个stackoverflow.com/questions/57878887/using-flift-webview-as-home-and-pressing-back-button-closes-application,这是我尝试过的应用程序,但它仍然无法返回到WebViewScope的上一个屏幕为什么在您的应用程序中使用Willposcopescaffold@Avinash我也用它来浏览网页,但仍然不起作用直到找到解决方法。它不会关闭应用程序,但不会返回到以前的webview网页在\u exitApp
中如果的第一部分没有返回语句,谢谢,但我仍然无法返回到以前的webview:(我仍在努力寻找解决方案。我找到了这个stackoverflow.com/questions/57878887/using-flift-webview-as-home-and-pressing-back-button-closes-application,这是我尝试过的应用程序,但它仍然无法返回到WebViewScope的上一个屏幕为什么在您的应用程序中使用Willposcopescaffold@Avinash我也用它来浏览网页,但仍然不起作用直到找到解决方法。它不会关闭应用程序,但不会返回到以前的WebViews网页谢谢。我将分析您的代码以充分了解它的工作原理,我的代码不太感谢您!只想问一下,为什么控制器分配给数据?在您的情况下,您使用myController for myController.canGoBack()但它不返回任何内容,因为它未初始化,所以在我的示例中,我使用_controller.future和_controller在webview中分配,数据是WebViewController。您可以使用任何内容代替数据。但它始终是WebViewController。因为它是在future对象中获取数据的过程。谢谢。我将分析您要完全理解它是如何工作的,我不太感谢您!只想问一下,为什么控制器被分配给数据?在您的例子中,您使用的是myController for myController.canGoBack()但它不返回任何内容,因为它未初始化,所以在我的示例中,我使用_controller.future和_controller在webview中分配,数据是WebViewController。您可以使用任何内容代替数据。但它始终是WebViewController。因为它是在future对象中获取数据的过程。