12

I have a CupertinoAlertDialog and AlertDialog in my Flutter app. every time the dialog pops up, everything behind it becomes darker. I would like to remove the background. how do I do that?

CupertinoDialogAction(
        child: Text('Delete',
                style: TextStyle(color: Colors.red),
              ),
              onPressed: () async {
                await CommentActivity.delete(postData[index]['id'])
                  .then((response) {
                  if (response) {
                    setState(() {
                      postData.removeAt(index);
                      createPageActivity();
                      renderPageActivity();
                    });
                    Navigator.of(context).pop();
                  }
                });
              }
            )
          ],
        )
Sharhad Bashar
  • 123
  • 1
  • 1
  • 4

4 Answers4

20

An alternative solution that partially solves the problem is using an almost transparent color for the barrier:

showDialog<void>(
      barrierColor: Color(0x01000000),
)
IAmJulianAcosta
  • 1,082
  • 2
  • 14
  • 30
8

Just launch the dialog with de navigator instead of using the showDialog() and use a PageRouteBuilder

Navigator.of(context).push(
                  PageRouteBuilder(
                      pageBuilder: (context, _, __) => AlertDialog(),
                      opaque: false),
);
Sergio Bernal
  • 2,126
  • 9
  • 20
6

I think you're talking about the black fader in the background of the dialog... Is part of the material/cupertino implementations, in Material has a fixed value of Colors.black54.

You will have to copy the showDialog() code, and modify it.

Demo:

// common Dialog widget shown in both implementation. 
  Widget buildDialog(BuildContext context) {
    return CupertinoDialogAction(
      child: Text(
        'Delete',
        style: TextStyle(color: Colors.red),
      ),
      onPressed: () async {
        Navigator.of(context).pop();
      },
    );
  }

  void openDialog(BuildContext context) {
    // open custom dialog.
    openCustomDialog(context);

    // open default dialog.
//    openFlutterDialog(context);

  }

  // regular Fluter showDialog()
  void openFlutterDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (ctx) {
        return buildDialog(ctx);
      },
    );
  }

  void openCustomDialog(BuildContext context) {
    showCustomDialog(
      context: context,
      builder: (ctx) {
        return buildDialog(ctx);
      },
    );
  }

  // custom implementation of showDialog()...
  Future<T> showCustomDialog<T>({
    @required BuildContext context,
    bool barrierDismissible = true,
    WidgetBuilder builder,
  }) {
    assert(debugCheckHasMaterialLocalizations(context));
    final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
    return showGeneralDialog(
      context: context,
      pageBuilder: (BuildContext buildContext, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        final Widget pageChild = Builder(builder: builder);
        return SafeArea(
          child: Builder(builder: (BuildContext context) {
            return theme != null
                ? Theme(data: theme, child: pageChild)
                : pageChild;
          }),
        );
      },
      barrierDismissible: barrierDismissible,
      barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
      // KEY PART TO MODIFY, Flutter doesn't allow a transparent Color,
      // values under opacity .01 are considered transparent and will throw an error.
      // But this value is transparent enough.
      barrierColor: Colors.black.withOpacity(0.01),

            // you can modify the default FadeTransition duration here.
      transitionDuration: const Duration(milliseconds: 2000),
    );
  }

Is this what you were looking for?

roipeker
  • 1,183
  • 9
  • 9
5

Simple solution with barrierColor property in showDialog method which I set white color with opacity value zero and barrier shadow is vanished

AlertDialog alert = AlertDialog(
    backgroundColor: Colors.transparent,
    elevation: 0,
    content: new Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Loader(),
      ],
    ),
  );
  showDialog(
    barrierColor: Colors.white.withOpacity(0),
    barrierDismissible: false,
    context: context,
    builder: (BuildContext context) {
      return WillPopScope(
            onWillPop: (){},
          child: alert);
    },
  );
Sachin Tanpure
  • 1,068
  • 11
  • 12