1

I want to display a custom dialog through a button. But i don't want user dismiss the dialog when touching exterior area.

I know that barrierDismissible: false, works but only in showDialog widget. In this case i need to do that but in Dialog widget.

This is what i have:

//RC
class ShowDialogGameOver2RC extends StatefulWidget {
  ShowDialogGameOver2RC({required this.score});

  late int score;

  @override
  State<ShowDialogGameOver2RC> createState() => _ShowDialogGameOver2RC();
}


// class dialog GameOver
class _ShowDialogGameOver2RC extends State<ShowDialogGameOver2RC> {
  @override
  Widget build(BuildContext context) {
    return Dialog(

      barrierDismissible: false //it doesn't work

      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
      elevation: 2,
      backgroundColor: Colors.transparent,
      child: _buildChild(context),
    );
  }
4
  • How do you show this ShowDialogGameOver2RC Widget as a popup? You may need to pass the dismissible flag there. Commented Aug 16, 2022 at 16:37
  • yes, it shows as a popup Commented Aug 16, 2022 at 17:49
  • Dialog is just a Widget, rendered like any other widget. Could you add the code where you create your ShowDialogGameOver2RC, please? Commented Aug 16, 2022 at 19:23
  • ok, i've added it now Commented Aug 16, 2022 at 20:06

1 Answer 1

1

Use Stack and place 1st widget as GestureDetector

class _ShowDialogGameOver2RCState extends State<ShowDialogGameOver2RC> {
  bool showDialog = true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          GestureDetector(
            onTap: () {
              setState(() {
                showDialog = !showDialog;
              });
            },
          ),
          if (showDialog)
            Dialog(
              // barrierDismissible: false //it doesn't work

              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(15)),
              elevation: 2,
              backgroundColor: Colors.transparent,
              child: Container(
                child: Column(
                  children: [Text("tada")],
                ),
              ),
            ),
        ],
      ),
    );
  }
}

Sign up to request clarification or add additional context in comments.

2 Comments

i am not working with showDialog widget. I am working with only Dialog
Try current snippet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.