modal_bottom_sheet icon indicating copy to clipboard operation
modal_bottom_sheet copied to clipboard

Bug with WillPopScope in inside modal

Open ziqq opened this issue 2 years ago • 10 comments

I want to disable close inside modal but i catch this error.

https://user-images.githubusercontent.com/20646071/190980441-547a5748-7feb-4408-bd27-336011def787.mp4

My ModalWillPopScope
class ModalWillPopScope extends StatefulWidget {
  final Widget child;
  final WillPopCallback? onWillPop;

  const ModalWillPopScope({
    Key? key,
    required this.child,
    required this.onWillPop,
  }) : super(key: key);

  @override
  State<ModalWillPopScope> createState() => _ModalWillPopScopeState();
}

class _ModalWillPopScopeState extends State<ModalWillPopScope> {
  ModalRoute<dynamic>? _route;
  ModalRoute<dynamic>? getModalRoute() {
    final route = ModalRoute.of(context);
    final navigator = Navigator.of(context);
    final navigatorRoute = ModalRoute.of(navigator.context);
    if (route is CupertinoModalBottomSheetRoute ||
        route is MaterialWithModalsPageRoute) {
      return route;
    }
    if (navigatorRoute is CupertinoModalBottomSheetRoute ||
        navigatorRoute is MaterialWithModalsPageRoute) {
      return navigatorRoute;
    }
    return null;
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    if (widget.onWillPop != null) {
      _route?.removeScopedWillPopCallback(widget.onWillPop!);
    }

    _route = getModalRoute();

    if (widget.onWillPop != null) {
      _route?.addScopedWillPopCallback(widget.onWillPop!);
    }
  }

  @override
  void didUpdateWidget(ModalWillPopScope oldWidget) {
    super.didUpdateWidget(oldWidget);
    assert(_route == getModalRoute());
    if (widget.onWillPop != oldWidget.onWillPop && _route != null) {
      if (oldWidget.onWillPop != null) {
        _route!.removeScopedWillPopCallback(oldWidget.onWillPop!);
      }
      if (widget.onWillPop != null) {
        _route!.addScopedWillPopCallback(widget.onWillPop!);
      }
    }
  }

  @override
  void dispose() {
    if (widget.onWillPop != null) {
      _route?.removeScopedWillPopCallback(widget.onWillPop!);
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => widget.child;
}

ziqq avatar Sep 19 '22 08:09 ziqq

Any news?

ziqq avatar Nov 28 '22 13:11 ziqq

This bug is fixed on Android but not fixed on iOS

IOS Represent

https://user-images.githubusercontent.com/20646071/204449982-528299d7-4cd6-4780-b947-353146591aca.mov

Android Represent

https://user-images.githubusercontent.com/20646071/204450190-d699ba0f-3723-4480-9c5b-87387d6482c5.mp4

ziqq avatar Nov 28 '22 13:11 ziqq

@ziqq Do you have any updates or solution for this issue? There is still same bugs in iOS

SuhwanCha avatar Mar 20 '23 02:03 SuhwanCha

@SuhwanCha No i don't have solution. I'm still waiting for the author's decision.

ziqq avatar Mar 20 '23 05:03 ziqq

Why Author never answer ?!

richguma avatar Jul 03 '23 15:07 richguma

@greedy-brady because he doesn't have to.

I'm facing the same issue, btw

agordeev avatar Aug 30 '23 18:08 agordeev

I stumbled above a similar behaviour while migrating from deprecated WillPopScope to PopScope. For me it helped to add a bit of delay to the Future that returns false:

Future.delayed(Duration(milliseconds: 500), () => false)

Maybe it is worth to also try this approach for the problem described in this issue here.

masewo avatar Nov 19 '23 21:11 masewo

I stumbled above a similar behaviour while migrating from deprecated WillPopScope to PopScope. For me it helped to add a bit of delay to the Future that returns false:

Future.delayed(Duration(milliseconds: 500), () => false)

Maybe it is worth to also try this approach for the problem described in this issue here.

No it's not working. Can u get your full variant?

ziqq avatar Nov 22 '23 13:11 ziqq

I basically forked this repo and fixed this problem for me (in a quiet bad way): https://github.com/masewo/modal_bottom_sheet/blob/36a1cfebb4643b89f871aefe57671a6873d173ee/modal_bottom_sheet/lib/src/bottom_sheet_route.dart#L108 After this change the modal sheet was not stuck anymore on half way.

masewo avatar Nov 22 '23 18:11 masewo

I basically forked this repo and fixed this problem for me (in a quiet bad way): https://github.com/masewo/modal_bottom_sheet/blob/36a1cfebb4643b89f871aefe57671a6873d173ee/modal_bottom_sheet/lib/src/bottom_sheet_route.dart#L108 After this change the modal sheet was not stuck anymore on half way.

Hi @masewo, PopScope work correctly?

ziqq avatar Nov 23 '23 07:11 ziqq