Open In App

Flutter - ShaderMask Widget

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

ShaderMask is a widget in Flutter that applies a shader to its child. It allows you to create various visual effects by manipulating the colors and gradients of the child widget. In this article, we are going to implement the ShaderMask widget and explore some properties of it.

Basic Syntax of ShaderMask Widget

Dart
ShaderMask(
  shaderCallback: (Rect bounds) {
    // Define the shader here
    return Shader;
  },
  // Optional, specifies how the 
  // shader blends with the child
  blendMode: BlendMode.modulate, 
  // The widget you want 
  // to apply the shader to
  child: Widget, 
)

Required Tools

To build this app, you need the following items installed on your machine:

  • Visual Studio Code / Android Studio
  • Android Emulator / iOS Simulator / Physical Device device.
  • Flutter Installed
  • Flutter plugin for VS Code / Android Studio.

Step By Step Implementations

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.

Step 2: Import the Package

First of all import material.dart file.

import 'package:flutter/material.dart';

Step 3: Execute the main Method

Here the execution of our app starts.

Dart
void main() {
  runApp(MyApp());
}

Step 4: Create MyApp Class

In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.

Dart
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.green, // Set the app's primary theme color
      ),
      debugShowCheckedModeBanner: false,
      home: ShaderMaskExampleScreen(),
    );
  }
}

Step 5: Create ShaderExampleScreen Class

In this class we are going to Implement the ShaderExampleScreen widget that help to add shader color effects to its child. Comments are added for better understanding.

ShaderMask(
shaderCallback: (Rect bounds) {
// Create a linear gradient shader for the mask
return LinearGradient(
colors: [Colors.red, Colors.blue],
stops: [0.5, 0.9],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
).createShader(bounds);
},
child: Image.network(
'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20220512131412/Student-Chapter-Article-Banner.png',
width: 300.0,
height: 300.0,
fit: BoxFit.cover,
),
),
Dart
class ShaderMaskExampleScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ShaderMask Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // Original Image
            Image.network(
              'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20220512131412/Student-Chapter-Article-Banner.png',
              width: 300.0,
              height: 300.0,
              fit: BoxFit.cover,
            ),
            Text("Original Image",
                style: TextStyle(fontWeight: FontWeight.bold)),

            SizedBox(height: 20.0), // Add some spacing between the images

            // Image with ShaderMask
            ShaderMask(
              shaderCallback: (Rect bounds) {
                // Create a linear gradient shader for the mask
                return LinearGradient(
                  colors: [Colors.red, Colors.blue],
                  stops: [0.5, 0.9],
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                ).createShader(bounds);
              },
              child: Image.network(
                'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20220512131412/Student-Chapter-Article-Banner.png',
                width: 300.0,
                height: 300.0,
                fit: BoxFit.cover,
              ),
            ),
            Text(
              "Shadered Image",
              style: TextStyle(fontWeight: FontWeight.bold),
            )
          ],
        ),
      ),
    );
  }
}

Here is the full Code of main.dart file

Dart
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.green, // Set the app's primary theme color
      ),
      debugShowCheckedModeBanner: false,
      home: ShaderMaskExampleScreen(),
    );
  }
}

class ShaderMaskExampleScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ShaderMask Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // Original Image
            Image.network(
              'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20220512131412/Student-Chapter-Article-Banner.png',
              width: 300.0,
              height: 300.0,
              fit: BoxFit.cover,
            ),
            Text("Original Image",
                style: TextStyle(fontWeight: FontWeight.bold)),

            SizedBox(height: 20.0), // Add some spacing between the images

            // Image with ShaderMask
            ShaderMask(
              shaderCallback: (Rect bounds) {
                // Create a linear gradient shader for the mask
                return LinearGradient(
                  colors: [Colors.red, Colors.blue],
                  stops: [0.5, 0.9],
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                ).createShader(bounds);
              },
              child: Image.network(
                'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20220512131412/Student-Chapter-Article-Banner.png',
                width: 300.0,
                height: 300.0,
                fit: BoxFit.cover,
              ),
            ),
            Text(
              "Shadered Image",
              style: TextStyle(fontWeight: FontWeight.bold),
            )
          ],
        ),
      ),
    );
  }
}

Output:

Screenshot_2023-10-04-20-04-13-779_comexampledismissable-min-(1)


Next Article

Similar Reads