belovance / QuickAlert

An instantly ready, full-featured alerts for development on any platform with flutter. Enabling you to complete projects and deploy quickly. With QuickAlert, you can display animated alert dialogs such as success, error, warning, confirm, loading or even a custom dialog.
https://pub.dev/packages/quickalert
MIT License
46 stars 42 forks source link

Popping context pops current screen not alert #33

Closed abhay-s-rawat closed 4 months ago

abhay-s-rawat commented 4 months ago

This is the 3rd time I am reporting this issue and 2 times I have given pull request for same but I don't know why this issue is always supressed.

The issue is so intermittent that it's very hard to catch. The context we are passing and the context we are popping is same which pops screen behind the alert. I gave solution to simply return context alert is using in onConfirmBtnTap function . onConfirmBtnTap(context)

We are always popping parent context in onConfirmBtnTap if we are defining that function , if function is null it pops context that alert widget has defined (child context of what we have passed)

see quickalert_buttons.dart

// quickalert_buttons.dart
@override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(top: 10.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          cancelBtn(context),
          okayBtn(context), // the alert context not the parent which was passed
        ],
      ),
    );
  }

  Widget okayBtn(context) { // the alert context not the parent which was passed
    if (!options!.showConfirmBtn!) {
      return const SizedBox();
    }
    final showCancelBtn = options!.type == QuickAlertType.confirm
        ? true
        : options!.showCancelBtn!;

    final okayBtn = buildButton(
        context: context,
        isOkayBtn: true,
        text: options!.confirmBtnText!,
        onTap: () {
          options!.timer?.cancel();
// the alert context not the parent which was passed
// if options!.onConfirmBtnTap != null then user will use the parent context to pop which will pop current screen not alert
          options!.onConfirmBtnTap != null
              ? options!.onConfirmBtnTap!()
              : Navigator.pop(context);
        });

    if (showCancelBtn) {
      return Expanded(child: okayBtn);
    } else {
      return okayBtn;
    }
  }
var res = await QuickAlert.show(
      context: context,
      width: width,
      type: QuickAlertType.error,
      title: 'Delete record',
      widget: const Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            'Are you sure to remove this ?',
            textAlign: TextAlign.center,
            style: TextStyle(
              color: Colors.grey,
              fontSize: 13,
            ),
          ),
        ],
      ),
      showCancelBtn: true,
      showConfirmBtn: true,
      onConfirmBtnTap: () {
        Navigator.pop(context);
      },
    );
abhay-s-rawat commented 4 months ago

some of the package are already using that approach see below https://pub.dev/packages/kumi_popup_window

//both sowPopupWindow and createPopupWindow
showPopupWindow(
  context,
  gravity: KumiPopupGravity.rightBottom,
  //curve: Curves.elasticOut,
  bgColor: Colors.grey.withOpacity(0.5),
  clickOutDismiss: true,
  clickBackDismiss: true,
  customAnimation: false,
  customPop: false,
  customPage: false,
  //targetRenderBox: (btnKey.currentContext.findRenderObject() as RenderBox),
  //needSafeDisplay: true,
  underStatusBar: false,
  underAppBar: true,
  offsetX: 0,
  offsetY: 0,
  duration: Duration(milliseconds: 200),
  onShowStart: (pop) {
    print("showStart");
  },
  onShowFinish: (pop) {
    print("showFinish");
  },
  onDismissStart: (pop) {
    print("dismissStart");
  },
  onDismissFinish: (pop) {
    print("dismissFinish");
  },
  onClickOut: (pop){
    print("onClickOut");
  },
  onClickBack: (pop){ 
    print("onClickBack");
  },
  childFun: (pop) {
    return Container(
      key: GlobalKey(),
      padding: EdgeInsets.all(10),
      height: 100,
      width: 100,
      color: Colors.redAccent,
    );
  },
);
abhay-s-rawat commented 4 months ago

surprisingly this only happens in macos

abhay-s-rawat commented 4 months ago

Unforunately I have to create own package for this. Thanks for your time https://pub.dev/packages/simple_alert_dialog