X-SLAYER / flutter_overlay_window

Flutter plugin for displaying flutter app over other apps on the screen
https://pub.dev/packages/flutter_overlay_window
MIT License
94 stars 98 forks source link

Not able to receive message from flutter_background_service #88

Open ugirishm opened 7 months ago

ugirishm commented 7 months ago

When background service is added with flutter_background_service, able to send data using FlutterBackgroundService.invoke, but not able to receive data using FlutterBackgroundService.on.

FlutterBackgroundService.on works fine when implemented in non-overlay screens.

M-ImranNawaz commented 7 months ago

Hi @ugirishm you need to use shareData method to share data from variables to overlay and to get data on overlay you need to use overlayListener so that you can listen to the data and get it and update data on the overlay. I hope you'll find this helpfull.

share data example:

if (!(await FlutterOverlayWindow.isPermissionGranted())) return; if (await FlutterOverlayWindow.isActive()) return;

await FlutterOverlayWindow.showOverlay( enableDrag: true, flag: OverlayFlag.defaultFlag, visibility: NotificationVisibility.visibilityPublic, height: 300, width: WindowSize.matchParent, ); await Future.delayed(const Duration(milliseconds: 200)); await FlutterOverlayWindow.shareData('$order,$pNumber');

Listen Data Example: String firstTitle = ''; String secTitle = '';

@override void initState() { super.initState(); FlutterOverlayWindow.overlayListener.listen((event) { log("$event"); setState(() { // isGold = !isGold; print('true call event $event'); if (event.toString().contains(',')) { String data = event.toString();

      try {
        print('try to set data');
        firstTitle = data.split(',').first;
        secTitle = data.split(',')[1];
      } catch (e) {
        print('exception $e');
        firstTitle = '';
        secTitle = '';
      }
      print('after all result on overlay: $firstTitle     $secTitle');
    }
  });
});
setState(() {});
Future.delayed(const Duration(milliseconds: 400), () {
  setState(() {});
});

}

YoavSl commented 3 weeks ago

@M-ImranNawaz How to do the opposite direction? Share data from the overlay. The above method doesn't work as the listen doesn't get any events. Thanks