skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

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

  2. 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…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search