Open In App

Flutter – ColorFiltered Widget

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The ColorFiltered widget in Flutter allows you to apply color filter effects to its child widget. In this article, we are going to implement the ColorFiltered widget and explore some properties of it. A sample video is given below to get an idea about what we are going to do in this article.

Some Properties of the ColorFiltered Widget

  • colorFilter: This property takes a ColorFilter object that defines how the colors of the child widget should be modified. It is a required property.
  • child: This property is used to specify the child widget that you want to apply the color filter to. It is a required property.

Basic Syntax of ColorFiltered Widget

Dart




ColorFiltered(
  colorFilter: ColorFilter.mode(
    // Define the color
  ),
  child: // Your child widget here,
)


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: ColorFilterExample(),
    );
  }
}


Step 5: Create ColorFilterExample Class

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

  ColorFiltered(
colorFilter: ColorFilter.mode(
Colors.green
.withOpacity(0.5), // Apply a blue tint with 50% opacity
BlendMode.srcATop, // Use 'srcATop' blend mode
),
child: Image.network(
'https://via.placeholder.com/200x200',
width: 200,
height: 200,
fit: BoxFit.cover,
),
),
SizedBox(height: 20),
Text('Image with Green color'),
SizedBox(height: 40),
// Another ColorFiltered Image with a red tint
ColorFiltered(
colorFilter: ColorFilter.mode(
Colors.orange
.withOpacity(0.5), // Apply a red tint with 50% opacity
BlendMode.srcATop, // Use 'srcATop' blend mode
),
child: Image.network(
'https://via.placeholder.com/200x200',
width: 200,
height: 200,
fit: BoxFit.cover,
),
),

Dart




class ColorFilterExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ColorFiltered Example'),
      ),
      body: SingleChildScrollView(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // Original Image
              Image.network(
                'https://via.placeholder.com/200x200',
                width: 200,
                height: 200,
                fit: BoxFit.cover,
              ),
              SizedBox(height: 20),
              Text('Original Image'),
  
              SizedBox(height: 40),
  
              // ColorFiltered Image with a blue tint
              ColorFiltered(
                colorFilter: ColorFilter.mode(
                  Colors.green
                      .withOpacity(0.5), // Apply a blue tint with 50% opacity
                  BlendMode.srcATop, // Use 'srcATop' blend mode
                ),
                child: Image.network(
                  'https://via.placeholder.com/200x200',
                  width: 200,
                  height: 200,
                  fit: BoxFit.cover,
                ),
              ),
              SizedBox(height: 20),
              Text('Image with Green color'),
  
              SizedBox(height: 40),
  
              // Another ColorFiltered Image with a red tint
              ColorFiltered(
                colorFilter: ColorFilter.mode(
                  Colors.orange
                      .withOpacity(0.5), // Apply a red tint with 50% opacity
                  BlendMode.srcATop, // Use 'srcATop' blend mode
                ),
                child: Image.network(
                  'https://via.placeholder.com/200x200',
                  width: 200,
                  height: 200,
                  fit: BoxFit.cover,
                ),
              ),
              SizedBox(height: 20),
              Text('Image with Orange color'),
            ],
          ),
        ),
      ),
    );
  }
}


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: ColorFilterExample(),
    );
  }
}
  
class ColorFilterExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ColorFiltered Example'),
      ),
      body: SingleChildScrollView(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // Original Image
              Image.network(
                'https://via.placeholder.com/200x200',
                width: 200,
                height: 200,
                fit: BoxFit.cover,
              ),
              SizedBox(height: 20),
              Text('Original Image'),
  
              SizedBox(height: 40),
  
              // ColorFiltered Image with a blue tint
              ColorFiltered(
                colorFilter: ColorFilter.mode(
                  Colors.green
                      .withOpacity(0.5), // Apply a blue tint with 50% opacity
                  BlendMode.srcATop, // Use 'srcATop' blend mode
                ),
                child: Image.network(
                  'https://via.placeholder.com/200x200',
                  width: 200,
                  height: 200,
                  fit: BoxFit.cover,
                ),
              ),
              SizedBox(height: 20),
              Text('Image with Green color'),
  
              SizedBox(height: 40),
  
              // Another ColorFiltered Image with a red tint
              ColorFiltered(
                colorFilter: ColorFilter.mode(
                  Colors.orange
                      .withOpacity(0.5), // Apply a red tint with 50% opacity
                  BlendMode.srcATop, // Use 'srcATop' blend mode
                ),
                child: Image.network(
                  'https://via.placeholder.com/200x200',
                  width: 200,
                  height: 200,
                  fit: BoxFit.cover,
                ),
              ),
              SizedBox(height: 20),
              Text('Image with Orange color'),
            ],
          ),
        ),
      ),
    );
  }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads