5

I want to disply a image in flutter in an transparent dialog. I've set opaque to false and used MaterialType.transparency. When I open the dialog the background is black.

class ShowProfileImage extends ModalRoute<void> {
  final String url;

  @override
  Duration get transitionDuration => Duration(milliseconds: 500);

  @override
  bool get opaque => false;

  @override
  bool get barrierDismissible => false;

  @override
  Color get barrierColor => Colors.black;

  @override
  String get barrierLabel => null;

  @override
  bool get maintainState => true;

  VoidCallback onDeleteImage;

  ShowProfileImage(String this.url);

  @override
  Widget buildPage(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
  ) {
    // This makes sure that text and other content follows the material style
    return Material(
      type: MaterialType.transparency,
      // make sure that the overlay content is not cut off
      child: SafeArea(
        child: InkWell(
            onTap: () => Navigator.of(context).pop(),
            child: Container(
                height: double.infinity,
                width: double.infinity,
                color: Colors.transparent,
                child: Center(
                    child: Container(
                        width: 300,
                        child: Image(image: NetworkImageWithRetry(url)))))),
      ),
    );
  }
}
MarcS82
  • 2,065
  • 7
  • 29
  • 46

5 Answers5

13

Try this, here I am showing an image from assets, you can add your code as well if you are fetching from network

     showDialog(
                context: context,
                builder: (_) => new Dialog(
                  backgroundColor: Colors.transparent,
                  child: new Container(
                    alignment: FractionalOffset.center,
                    height: 80.0,
                    padding: const EdgeInsets.all(20.0),
                    child:  new Image.asset(
                      'assets/images/image.jpg',
                      fit: BoxFit.cover,
                    )
                  ),
                ));
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
2

give showDialog a transparent barrierColor that not Colors.transparent,like Color(0x00ffffff)

TomVista
  • 31
  • 1
1

You can use:

Dialog(
    backgroundColor: Colors.transparent,
    elevation: 0,
    child: ...
)

You must set elevation to 0, otherwise a shadow will be canst beneath the dialog.

Mario Alem
  • 11
  • 1
0

Just call

_showTransparentDialog(context);

Implementation:

Future<T> _showTransparentDialog<T>(BuildContext context) async {
  return await showDialog<T>(
    context: context,
    builder: (_) {
      return AlertDialog(
        backgroundColor: Colors.transparent,
        content: Image.asset('your_image_asset'),
      );
    },
  );
}
iDecode
  • 22,623
  • 19
  • 99
  • 186
0

@TomaVista is right, if you set barrier color transparent, it will cause error, try Color(0x00ffffff) instead:

showDialog(context: context,
    //It doesnt work: barrierColor: Colors.transparent,
    barrierColor: Color(0x00ffffff), //this works
    builder: (_) {
      ////
    }
    );
Bensal
  • 3,316
  • 1
  • 23
  • 35