Line data Source code
1 : import 'package:flutter/cupertino.dart'; 2 : import 'package:flutter/foundation.dart'; 3 : import 'package:flutter/material.dart'; 4 : 5 : /// Decorate the provided [Image] or [IconData]. 6 : class BlendImageIcon<T> extends StatelessWidget { 7 1 : const BlendImageIcon(this.image, {Key key, this.color, this.size}) 8 2 : : assert(image is Widget || image is IconData, 9 : 'image must be IconData or Widget'), 10 1 : super(key: key); 11 : 12 : /// Color used for Icon and gradient. 13 : final Color color; 14 : 15 : /// Child image. 16 : final T image; 17 : 18 : /// Size of icon. 19 : final double size; 20 : 21 1 : @override 22 : Widget build(BuildContext context) { 23 2 : if (image is Widget) { 24 3 : var s = size ?? IconTheme.of(context).size; 25 : // flutter web do not support shader mask. (flutter v1.12.x) 26 1 : var showRawImage = kIsWeb || color == null; 27 : if (showRawImage) { 28 1 : return SizedBox( 29 : width: s, 30 : height: s, 31 1 : child: image as Widget, 32 : ); 33 : } 34 0 : return SizedBox( 35 : width: s, 36 : height: s, 37 0 : child: ShaderMask( 38 0 : child: image as Widget, 39 0 : shaderCallback: (Rect bounds) { 40 0 : return LinearGradient(colors: [color, color]).createShader(bounds); 41 : }, 42 : blendMode: BlendMode.srcIn, 43 : ), 44 : ); 45 : } 46 4 : return Icon(image as IconData, size: size, color: color); 47 : } 48 : }