flutterdartflutter-layoutflutter-alertdialog

Why the screen becomes dark when an alert was displaying with flutter?


I don't know why the screen becomes dark when I try to use the alert on initState() in my flutter application. This is the code :

  if (element.isCharacteristic42) {
             
         
              Scaffold:
              (showDialog(
                  context: context,
                  builder: (context) {
                    return AlertDialog(
                      title: Text('Be Careful an obstacle was detected'),
                      backgroundColor: Color.fromARGB(45, 231, 148, 54),
                      alignment: Alignment.topCenter,
                    );
                  }));
            }

Thanks in advance for your help


Solution

  • The outer shadow is provided by barrierColor of showDialog. You can make it transparent.

    And to show dialog inside initState, use addPostFrameCallback to complete the first frame

      @override
      void initState() {
        super.initState();
    
        WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
          showDialog(
            context: context,
            barrierColor: Colors.transparent,