通过点击按钮关闭Flutter Web应用程序(Flutter PWA)。

huangapple go评论138阅读模式
英文:

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)),
    );
  }
}

huangapple
  • 本文由 发表于 2023年4月4日 03:16:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定