- LinearGradient:线性渐变
- RadialGradient:放射状渐变
- SweepGradient:扇形渐变
LinearGradient
ShaderMask(
shaderCallback: (Rect bounds) {
return LinearGradient(
begin: Alignment.topCenter,end: Alignment.bottomCenter,colors: [Colors.red,Colors.blue,Colors.green],).createShader(bounds);
},blendMode: BlendMode.color,child: Image.asset(
'assets/images/b.jpg',fit: BoxFit.cover,),)
Color color = Colors.orange;
return ShaderMask(
shaderCallback: (Rect bounds) {
return LinearGradient(
begin: Alignment.topCenter,colors: [color,color,Colors.transparent,color],stops: [0,.4,.41,.6,.61,1]
).createShader(bounds);
},);
RadialGradient
ShaderMask(
shaderCallback: (Rect bounds) {
return RadialGradient(
radius: .5,colors: <Color>[Colors.red,Colors.blue],).createShader(bounds);
},child: Image.asset(
'assets/images/b.jpg',)
ShaderMask(
shaderCallback: (Rect bounds) {
return RadialGradient(
radius: .6,colors: <Color>[
Colors.transparent,Colors.grey.withOpacity(.7),Colors.grey.withOpacity(.7)
],.5,1],blendMode: BlendMode.srcATop,)
SweepGradient
ShaderMask(
shaderCallback: (Rect bounds) {
return SweepGradient(
colors: <Color>[
Colors.red,Colors.blue
],)
Container(
width: 200,height: 200,child: CustomPaint(
painter: _CircleProgressPaint(.5),)
class _CircleProgressPaint extends CustomPainter {
final double progress;
_CircleProgressPaint(this.progress);
Paint _paint = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 20;
@override
void paint(Canvas canvas,Size size) {
_paint.shader = ui.Gradient.sweep(
Offset(size.width / 2,size.height / 2),[Colors.red,Colors.yellow]);
canvas.drawArc(
Rect.fromLTWH(0,size.width,size.height),pi*2,false,_paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
ShaderMask(
shaderCallback: (Rect bounds) {
return LinearGradient(
colors: <Color>[Colors.blue,Colors.red],tileMode: TileMode.mirror,child: Center(
child: Text(
'老孟,一枚有态度的程序员',style: TextStyle(fontSize: 24),)