Open In App

Flutter – ColorFiltered Widget

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

Basic Syntax of ColorFiltered Widget




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:



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.




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.




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,
),
),




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




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:


Article Tags :