jamesblasco / modal_bottom_sheet

Flutter | Create advanced modal bottom sheets. Material, Cupertino or your own style
https://pub.dev/packages/modal_bottom_sheet
MIT License
1.86k stars 466 forks source link

Check that the modal bottom sheet is closed by the user manually or by Navigator.pop #224

Closed rrifafauzikomara closed 2 years ago

rrifafauzikomara commented 2 years ago
showCupertinoModalBottomSheet(
      expand: true,
      context: context,
      backgroundColor: ColorPalettes.white,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(10),
          topRight: Radius.circular(10),
        ),
      ),
      isDismissible: true,
      builder: (rootContext) => ManageSavingsPlanSheet(
        argument: ManageSelectArgument(
          iban: widget.argument.iban,
          cashBalance: widget.argument.cashBalance,
          digitalDepotAccountId: widget.argument.digitalDepotAccountId,
          withdrawBalance: widget.argument.withdrawBalance,
          canWithdraw: widget.argument.canWithdraw,
          isPaused: widget.argument.isPaused,
        ),
      ),
    ).then((_) => _getSavingsPlanDetail(context));

Can we check the modal bottom sheet is closed by the user (drag) or by Navigator.pop (from a button inside the modal bottom sheet)?

Because I have a condition when the modal is closed by Navigator.pop want to execute API function (_getSavingsPlanDetail), but if by the user manually is not.

Because for now the _getSavingsPlanDetail is always executed.

jamesblasco commented 2 years ago

There is different ways to achieve this:

  1. Via a callback method that you on the onPressed button after Navigator.pop

    showCupertinoModalBottomSheet(
      expand: true,
      context: context,
      backgroundColor: ColorPalettes.white,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(10),
          topRight: Radius.circular(10),
        ),
      ),
      isDismissible: true,
      builder: (rootContext) => ManageSavingsPlanSheet(
         onClosedByUser: () {
            _getSavingsPlanDetail(context);
         },
         /// more params
      ),
    );
  2. Return a value when tapping the button Navigator.pop(context, true)

    final shouldShowSavings = await  showCupertinoModalBottomSheet<bool>(
      expand: true,
      context: context,
      backgroundColor: ColorPalettes.white,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(10),
          topRight: Radius.circular(10),
        ),
      ),
      isDismissible: true,
      builder: (rootContext) => ManageSavingsPlanSheet(
         onClosedByUser: () {
           Navigator.of(rootContext).pop(true);
         },
         /// more params
      ),
    );
    if(shouldShowSavings)   _getSavingsPlanDetail(context);