title: showing a dialog doesn't work when the widget is the direct child of MaterialApp
body: <br/>
  I'm trying to create an alert dialog in Flutter, but the dialog doesn't work when it is under MaterialApp and instead gives an error. Below is the code:
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(const MyApp());
  
  class MyApp extends StatelessWidget {
    const MyApp({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('Inputs and alerts'),
          ),
          body: ElevatedButton(
            child: const Text('Show Dialog'),
            onPressed: () {
              showDialog(
                context: context,
                builder: (_) {
                  return AlertDialog(
                    title: Text('This is a text'),
                    content: Text('this is the content'),
                    actions: [
                      TextButton(
                        onPressed: () {
                          Navigator.of(context).pop(false);
                        },
                        child: Text('No'),
                      ),
                      TextButton(
                        onPressed: () {
                          Navigator.of(context).pop(true);
                        },
                        child: Text('Yes'),
                      )
                    ],
                  );
                },
              );
            },
          ),
        ),
      );
    }
  }
  </code></pre>
  <p><strong>Error</strong>
  <a href="https://i.stack.imgur.com/lHjDY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lHjDY.png" alt="enter image description here" /></a></p>
  <p>But when I extract the ElevatedButton to a stand-alone widget, the alert dialog works fine. Here is the code:</p>
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(const MyApp());
  
  class MyApp extends StatelessWidget {
    const MyApp({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('Inputs and alerts'),
          ),
          body: sn(),
        ),
      );
    }
  }
  
  class sn extends StatelessWidget {
    const sn({
      Key? key,
    }) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: ElevatedButton(
          child: const Text('Show Dialog'),
          onPressed: () {
            showDialog(
              context: context,
              builder: (_) {
                return AlertDialog(
                  title: Text('This is a text'),
                  content: Text('this is the content'),
                  actions: [
                    TextButton(
                      onPressed: () {
                        Navigator.of(context).pop(false);
                      },
                      child: Text('No'),
                    ),
                    TextButton(
                      onPressed: () {
                        Navigator.of(context).pop(true);
                      },
                      child: Text('Yes'),
                    )
                  ],
                );
              },
            );
          },
        ),
      );
    }
  }
  </code></pre>
  <p><strong>Output</strong>
  <a href="https://i.stack.imgur.com/8v8jm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8v8jm.png" alt="enter image description here" /></a></p>
  <p>Can anyone tell me the cause of this behaviour? Any help is greatly appreciated.</p>