How can I detect share sheet open or closed in flutter programmatically

Viewed 284

In my page, there is a share button by using this share package. Share package

I want, when share sheet comes, the share button become hide, and when share sheet closed, share button wants to be show.. How can I implement this?

3 Answers

instead using share package use flutter_share and use blow code for your purpose.

 await FlutterShare.share(title: ' ', text: ' ', linkUrl: your link, chooserTitle: your title).then((value){
//insert your code here for make btn disapeare
       print('flutter share now is open') });

You may try WidgetsBindingObserver. When a share sheet is shown the app is in inactive state, when it is closed the app goes into resumed state.

import 'package:flutter/material.dart';

class MyWidget extends StatefulWidget {
  const MyWidget({super.key});

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> with WidgetsBindingObserver {
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        print('sheet dismissed');
        break;
      case AppLifecycleState.inactive:
        print('sheet shown');
        break;
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
        break;
    }
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
Related