Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Image fade out at bottom (Gradient)

Tags:

flutter

dart

Is there a way to make an Image fade out towards the bottom? So the Image has transperency 0 at the bottom.

I need the Image to be transparent, I can't use a Stack and overlay the Image at the bottom with a Color, because I don't know whats underneath the Image.

This is my Image

            Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.cover,
                  colorFilter: new ColorFilter.mode(Colors.black.withOpacity(1), BlendMode.dstATop),
                  image: NetworkImage(
                    routine.thumbnail
                  )
                )
              ),
            )

I can't do this:

        background: Stack(
          alignment: AlignmentDirectional.bottomCenter,                        
          children: <Widget>[
            Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.cover,
                  image: NetworkImage(
                    routine.thumbnail,
                  )
                )
              ),
            ),
            Container(
              width: 1000,
              height: 100,
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: FractionalOffset.topCenter,
                  end: FractionalOffset.bottomCenter,
                  colors: [
                    Colors.transparent,
                    someColor // I don't know what Color this will be, so I can't use this
                  ]
                )
              ),
            ),
          ]
        ),
like image 663
Jonas Avatar asked Mar 11 '19 13:03

Jonas


People also ask

How do you display an image with top and bottom gradient shadow in flutter?

Nested containers, which is to put your image inside Container(topShadow) => Container(bottomShadow)=> Image.

How do I change the opacity of an image in flutter?

Build the Appbar with the scaffold widget. Now use the Image. Network widget inside the body of the scaffold widget. Finally, set the opacity using colorBlendMode.

How do you give a gradient a color to an image in flutter?

withOpacity(0.0), Color(0xff0069ff). withOpacity(0.8), ], ), ), ), DecoratedBox( decoration: BoxDecoration( gradient: LinearGradient( begin: FractionalOffset. topLeft, end: FractionalOffset. bottomRight, colors: [ Color(0xff692eff).


1 Answers

you need a ShaderMask, something like this:

ShaderMask(
  shaderCallback: (rect) {
    return LinearGradient(
      begin: Alignment.topCenter,
      end: Alignment.bottomCenter,
      colors: [Colors.black, Colors.transparent],
    ).createShader(Rect.fromLTRB(0, 0, rect.width, rect.height));
  },
  blendMode: BlendMode.dstIn,
  child: Image.asset(
    'assets/chrome.png',
    height: 400,
    fit: BoxFit.contain,
  ),
),

here shaderCallback is used for returning a linear gradient that is used as a mask and with BlendMode.dstIn blend mode fades out your image from the top to the bottom

like image 181
pskink Avatar answered Sep 21 '22 02:09

pskink