Flutter 中渐变的高级用法

移动开发 作者: 2024-08-25 09:30:01
Flutter 中渐变有三种: LinearGradient:线性渐变 RadialGradient:放射状渐变 SweepGradient:扇形渐变 看下原图,下面的渐变都是在此图基础上完成。 Li
  • 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),)
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68321.html