Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Flutter's ShaderMask

I am trying to understand how ShaderMask works. If I follow the example on the ShaderMask docs here:

ShaderMask(
  shaderCallback: (Rect bounds) {
    return RadialGradient(
      center: Alignment.topLeft,
      radius: 1.0,
      colors: <Color>[Colors.yellow, Colors.deepOrange.shade900],
      tileMode: TileMode.mirror,
    ).createShader(bounds);
  },
  child: const Text('I’m burning the memories'),
)

I get this:

ShaderMask example output

(the double lines below Text are apparently an indication of a lack of a Theme)

Then If I surround this same ShaderMask in a Scaffold I get this:

Scaffold(
      body: Center(
        child: ShaderMask(
          shaderCallback: (Rect bounds) {
            return RadialGradient(
              center: Alignment.topLeft,
              radius: 1.0,
              colors: <Color>[Colors.yellow, Colors.deepOrange.shade900],
              tileMode: TileMode.mirror,
            ).createShader(bounds);
          },
          child: const Text('I’m burning the memories'),
        ),
      ),
    );

ShaderMask with Scaffold

It's all gone! the ShaderMask appears to have been just ignored, and there's no mention on the docs about this behavior, why does the Scaffold ""disables"" the ShaderMask?

like image 837
Michel Feinstein Avatar asked Jan 21 '26 10:01

Michel Feinstein


1 Answers

You are missing - blendMode property

More info on - blendMode property

working code:

    Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ShaderMask(
          blendMode: BlendMode.srcATop,  // Add this
          shaderCallback: (Rect bounds) {
            return RadialGradient(
              center: Alignment.topLeft,
              radius: 1.0,
              colors: <Color>[Colors.yellow, Colors.deepOrange.shade900],
              tileMode: TileMode.mirror,
            ).createShader(bounds);
          },
          child: const Text('I’m burning the memories'),
        ),
      ),
    );
  }
like image 51
anmol.majhail Avatar answered Jan 25 '26 19:01

anmol.majhail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!