英文:
Closing flutter web app (flutter PWA) by clicking on a button
问题
我创建了一个Flutter web应用,并在我的JavaScript网站中将其用作PWA,我使用iframe打开它。
我想通过点击一个按钮来关闭我的PWA,使用SystemChannels.platform.invokeMethod('SystemNavigator.pop')
。
我尝试了这段代码,但不起作用:
GestureDetector(
onTap: () {
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
},
child: Container(padding: const EdgeInsets.all(4), child: Icon(Icons.close, color: BpColors.white, size: 20)),
);
我还尝试使用exit(0)
,但它会导致网站返回到上一页,而不是只是关闭PWA。
英文:
I create a flutter web app and use it as a PWA in my java-script website I opened it using iframe.
I want to close my PWA by tapping on a button by SystemChannels.platform.invokeMethod('SystemNavigator.pop')
.
I tried this code but not working:
GestureDetector(
onTap: () {
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
},
child: Container(padding: const EdgeInsets.all(4), child: Icon(Icons.close, color: BpColors.white, size: 20)),
);
I also tried using exit(0) but it makes the website to go to previous page not just closing the PWA.
答案1
得分: 0
代码部分不需要翻译,以下是翻译好的文本部分:
"It is not possible to close web app by SystemChannels.platform.invokeMethod('SystemNavigator.pop')
because this only works for mobile applications. anyway, instead of it you can use window.close()
to close web app window. but unfortunately it may not working in some browsers because they do not allow to close window from JS for security reasons.
By the way, there is a package named flutter_window_close you can try it.
happy coding..."
英文:
It is not possible to close web app by SystemChannels.platform.invokeMethod('SystemNavigator.pop')
because this only works for mobile applications. anyway, instead of it you can use window.close()
to close web app window. but unfortunately it may not working in some browsers because they do not allow to close window from JS for security reasons.
import 'dart:html';
/// your another codess
GestureDetector(
onTap: () {
window.close();
},
child: Container(padding: const EdgeInsets.all(4), child: Icon(Icons.close, color: BpColors.white, size: 20)),
);
By the way, there is a package named flutter_window_close you can try it.
happy coding...
答案2
得分: 0
这对我有用
close_clicked是我JavaScript正在监听的事件名称。
import 'dart:html' as html;
import 'package:flutter/material.dart';
class GlobalCloseButton extends NoControllerWidget {
const GlobalCloseButton({super.key});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
html.window.parent?.postMessage("close_clicked", "*");
},
child: Container(padding: const EdgeInsets.all(4), child: Icon(Icons.close, color: Colors.red, size: 20)),
);
}
}
英文:
This worked for me
close_clicked is the event name that my javascript is listening to.
import 'dart:html' as html;
import 'package:flutter/material.dart';
class GlobalCloseButton extends NoControllerWidget {
const GlobalCloseButton({super.key});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
html.window.parent?.postMessage("close_clicked", "*");
},
child: Container(padding: const EdgeInsets.all(4), child: Icon(Icons.close, color: Colors.red, size: 20)),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论