0

enter image description here

Currently i am working on music app and according to my ui i have to display download, downloading progress and downloaded status shown inside popup menu item.But according to Popup menu button widget behaviour, it is dispose and unmounted.So when i closed popup menu item and again open the last status always display download instead of downloading.So it is possible to prevent popup menu button after close.

I tried callback functions, provider, getx, auto keep alive and also stateful builder but it is not working.

  • You don't need to keep the state alive for it to work the way you want, you can rebuild it with the state same state but have different results based on music file. Could you share your PopupMenuButton? – noxgood Nov 14 '22 at 13:36
  • Can you include sample snippet that will reproduce the same issue – Md. Yeasin Sheikh Nov 14 '22 at 15:07
  • @noxgood, I updated question and can you please explain in brief. – Kesmi Topiwala Nov 14 '22 at 16:39
  • @YeasinSheikh, It is not possible because of privacy. But i updated question and added image of popup menu button according to my ui. So can you give me any suggestion from image of popup menu button. – Kesmi Topiwala Nov 14 '22 at 16:41
  • Download progress will be continued even if the button get closed – Md. Yeasin Sheikh Nov 14 '22 at 16:57
  • @YeasinSheikh Yes it will be continued even if the button get closed but when download progress is running and user click on the popup that time also i want to display the download progress and downloading text. But if user close and then again open then display progress is download. – Kesmi Topiwala Nov 14 '22 at 17:00
  • yap, got it, test my post, you can play with `Duration` and `if (downloadProgress.value! > 1) timer.cancel();`. used `ValueNotifier` as for this widget, you can make it global if needed, perhaps it will be convert into state-management property – Md. Yeasin Sheikh Nov 14 '22 at 17:01
  • 1
    @YeasinSheikh, Okay i will try and test and let you know and thanks for given response in short time. – Kesmi Topiwala Nov 14 '22 at 17:04

1 Answers1

2

I am using ValueNotifier to preserve the download progress. To preserve the state you can follow this structure and use state-management property like riverpod/bloc

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

  @override
  State<DTest> createState() => _DTestState();
}

class _DTestState extends State<DTest> {
  /// some state-management , also can be add a listener 
  ValueNotifier<double?> downloadProgress = ValueNotifier(null);
  Timer? timer;
  _startDownload() {
    timer ??= Timer.periodic(
      Duration(milliseconds: 10),
      (timer) {
        downloadProgress.value = (downloadProgress.value ?? 0) + .01;
        if (downloadProgress.value! > 1) timer.cancel();
      },
    );
  }

  @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          PopupMenuButton(
            itemBuilder: (context) {
              return [
                PopupMenuItem(
                  child: ValueListenableBuilder(
                    valueListenable: downloadProgress,
                    builder: (context, value, child) => InkWell(
                        onTap: value == null ? _startDownload : null,
                        child: Text("${value ?? "Download"}")),
                  ),
                )
              ];
            },
          )
        ],
      ),
    );
  }
}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • 1
    Yes It is working fine when i put download code in parent widget and i identified my mistake , i put in child common widget so it was disposed and not getting preserve state.Thanks for help @Yeasin Sheikh – Kesmi Topiwala Nov 18 '22 at 05:18