如何将渐变滤镜应用于图标?

时间:2019-11-25 19:11:59

标签: flutter dart

我需要在Icon上放置一个渐变。

我该如何实现?

2 个答案:

答案 0 :(得分:3)

执行此操作的一种方法是使用ShaderMask小部件。如果您用它来包装图标,则可以应用任何您喜欢的渐变:

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

给你这样的东西:

enter image description here

答案 1 :(得分:-1)

您也可以这样做:

          Container(
            height: 50,
            width: 50,
            child: Icon(Icons.error, color: Colors.white),
            decoration: BoxDecoration(gradient: LinearGradient(colors: [Colors.yellow, Colors.black])),
          ),

enter image description here