Skip to main content

Command Palette

Search for a command to run...

ShaderMask widget and Attributes

Published
2 min read
ShaderMask widget and Attributes
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

The ShaderMask widget in Flutter is used to apply a shader-based mask to its child, creating various visual effects such as gradients, patterns, or custom effects. It's a powerful widget for adding complex visual effects to UI components.

Attributes:

  1. blendMode (BlendMode):

    • The blend mode used to combine the shader and the child. Options include BlendMode.srcIn, BlendMode.srcOut, BlendMode.dstIn, etc.
  2. shaderCallback (ShaderCallback):

    • A callback function that returns a Shader object defining the shader to be applied. It receives the size of the child widget as an argument.
  3. child (Widget):

    • The child widget to which the shader mask is applied.

Example:

import 'package:flutter/material.dart';

class CustomShaderMask extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ShaderMask Example'),
      ),
      body: Center(
        child: ShaderMask(
          blendMode: BlendMode.srcIn,
          shaderCallback: (Rect bounds) {
            return LinearGradient(
              colors: [Colors.red, Colors.blue],
            ).createShader(bounds);
          },
          child: Image.asset(
            'assets/flutter_logo.png',
            width: 200,
            height: 200,
          ),
        ),
      ),
    );
  }
}

void main() {
  runApp(
    MaterialApp(
      home: CustomShaderMask(),
    ),
  );
}

In this example, a ShaderMask is used to apply a gradient mask to an Image widget. The blendMode is set to BlendMode.srcIn, and the shaderCallback creates a linear gradient shader. This creates a visual effect where the image is masked with a gradient, resulting in a colorful overlay. The ShaderMask widget allows you to experiment with various shaders and blend modes to achieve unique visual effects in your Flutter app.

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"