Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

flutter - How to prevent dismissing CupertinoActionSheet/showCupertinoModalPopup?

It may be a stupid question but I don't know how to figure out it: How to prevent CupertinoActionSheet/showCupertinoModalPopup from dismissing while you tap outside of it?

I made a selector list for gps search (radius) and I have to prevent user for dismissing it without any value selected.


_showDialog2() {
    Platform.isAndroid
        ? showDialog(
            barrierDismissible: false,
            context: context,
            builder: (context) {
              return RadiusSelectorDialog();
            })
        : showCupertinoModalPopup(
            context: context,
            builder: (context) {
              return CupertinoRadiusSelectorPopup();
            }).then((value) => print(value));
  }

In case of Android dialog it's just enough to set barierDismissable to false. But in showCupertinoModalPopup this kind of property do not exist. I've tried to wrap it with Absorb pointer but it can still be dismissed when you tap outside.

my actionSheet

class _CupertinoRadiusSelectorPopupState
    extends State<CupertinoRadiusSelectorPopup> {
  Color color = Colors.grey[900];
  double fontSize = 15.0;
  @override
  Widget build(BuildContext context) {
    return CupertinoActionSheet(
      title: Text('Search in range:',
          style: TextStyle(
            color: Colors.black,
            fontSize: 18,
          )),
      message: Text('Search for points in selected range'),
      actions: [
        CupertinoActionSheetAction(
            onPressed: () => onPressed("10"),
            child: Text('10 km',
                style: TextStyle(color: color, fontSize: fontSize))),
        CupertinoActionSheetAction(
            onPressed: () => onPressed("25"),
            child: Text('25 km',
                style: TextStyle(color: color, fontSize: fontSize))),
        CupertinoActionSheetAction(
            onPressed: () => onPressed("50"),
            child: Text('50 km',
                style: TextStyle(color: color, fontSize: fontSize))),
        CupertinoActionSheetAction(
            onPressed: () => onPressed("100"),
            child: Text('100 km',
                style: TextStyle(color: color, fontSize: fontSize))),
      ],
    );
  }

  onPressed(String value) {
    print(value);
    Navigator.pop(context, value);
  }
}
question from:https://stackoverflow.com/questions/66045528/how-to-prevent-dismissing-cupertinoactionsheet-showcupertinomodalpopup

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Methods like showDialog or showBottomSheet are actually pushing new routes, so you just can use WillPopScope to prevent pop.

sample code:

 showCupertinoModalPopup(
            context: context,
            builder: (BuildContext context) {
              return WillPopScope(
                onWillPop: () async {
                  // if(...) return true;
                  return false;
                },
                child: CupertinoActionSheet(
                  title: Text('title'),
                ),
              );
            },
          );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...