Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply Gradient filter to an Icon?

Tags:

flutter

dart

I need to put a gradient on my Icon.

How do I achieve this?

like image 717
Mateus99 Avatar asked Dec 22 '22 20:12

Mateus99


1 Answers

One way to do this is to use a ShaderMask widget. If you wrap an icon with this, you can apply any gradient you like:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: LinearGradientMask(
            child: Icon(
              Icons.book,
              size: 250,
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }
}

class LinearGradientMask extends StatelessWidget {
  LinearGradientMask({this.child});
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return ShaderMask(
      shaderCallback: (bounds) {
        return RadialGradient(
          center: Alignment.topLeft,
          radius: 1,
          colors: [Colors.blue, Colors.red],
          tileMode: TileMode.mirror,
        ).createShader(bounds);
      },
      child: child,
    );
  }
}

giving you something like looks like this:

enter image description here

like image 174
Matt S. Avatar answered Jan 21 '23 05:01

Matt S.