Flutter 通过Clipper实现各种自定义形状的示例代码

 更新时间:2019年12月05日 10:40:52   作者:蓝色微笑ing  
这篇文章主要介绍了Flutter 通过Clipper实现各种自定义形状的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文介绍了Flutter 通过Clipper实现各种自定义形状的示例代码,分享给大家,具体如下:

ClipOval 圆形裁剪

ClipOval(
 child: SizedBox(
  width: 120.0,
  height: 120.0,
  child: Image.asset(
   Config.assets_avatar_1,
  ),
 ),
);

CircleAvatar 圆形头像

CircleAvatar(
 radius: 60.0,
 backgroundImage: AssetImage(
  Config.assets_avatar_1,
 ),
);

Container Decoration 装饰形状

 

通过BoxShape.circle实现圆形图片

Container(
 width: 120.0,
 height: 120.0,
 decoration: BoxDecoration(
  shape: BoxShape.circle,
  image: DecorationImage(
   image: AssetImage(
    Config.assets_avatar_1,
   ),
  ),
 )
);

通过BorderRadius实现圆形图片

Container(
 width: 120.0,
 height: 120.0,
 decoration: BoxDecoration(
  borderRadius: BorderRadius.all(Radius.circular(60.0)),
   image: DecorationImage(
    image: AssetImage(
     Config.assets_avatar_1,
    ),
   ),
 ),
)

ClipPath 路径剪裁

ClipPath(
 clipper: TriangleClipper(ClipperPosition.LeftTop),
 child: Container(
  width: 16.0,
  height: 16.0,
  decoration: BoxDecoration(
   color: Colors.blue,
  ),
 ),
);

enum ClipperPosition {
 LeftTop,
 RightTop,
}

class TriangleClipper extends CustomClipper<Path> {
 final ClipperPosition position;
 TriangleClipper(this.position);

 @override
 Path getClip(Size size) {
  final path = Path();
  path.lineTo(0.0, 0.0);
  if (position == ClipperPosition.LeftTop) {
   path.lineTo(size.width, 0.0);
   path.lineTo(size.width, size.height);
  } else if (position == ClipperPosition.RightTop) {
   path.lineTo(size.width, 0.0);
   path.lineTo(0.0, size.height);
  }
  path.close();
  return path;
 }

 @override
 bool shouldReclip(CustomClipper oldClipper) {
  return false;
 }
}

ClipRect 矩形剪裁

Container(
 alignment: Alignment.topCenter,
 color: Colors.transparent,
 child: Container(
  color: Colors.green,
  child: ClipRect(
   clipper: _RectClipper(20.0),
   child: Image.asset(
    Config.assets_avatar_1,
    width: 160.0,
    height: 160.0,
    fit: BoxFit.fill,
   ),
  ),
 ),
);

class _RectClipper extends CustomClipper<Rect> {
 /// Remove side of size
 final double removeSize;
 
 _RectClipper(this.removeSize);
 
 @override
 Rect getClip(Size size) {
  return new Rect.fromLTRB(
   removeSize,
   removeSize,
   size.width - removeSize,
   size.height - removeSize,
  );
 }
 
 @override
 bool shouldReclip(CustomClipper<Rect> oldClipper) {
  return false;
 }
}

ClipRRect 圆角矩形剪裁

ClipRRect(
 borderRadius: BorderRadius.all(Radius.circular(16.0)),
 child: Image.asset(
  Config.assets_avatar_1,
  fit: BoxFit.fill,
  width: 120.0,
  height: 120.0,
 ),
);

Star Rating(CustomPaint) 评分控件

评分控件 UI图

 

实现方案

使用CustomPaint结合ClipPath画出单个五角星;

  • 使用Stack渲染两层画面
  • 背景层,一排灰色五角星  前景层,一排亮色五角星,并使用ClipRect截取一定Width

实现代码

class StarRatingDemo extends StatefulWidget {
 @override
 _StarRatingDemoState createState() => _StarRatingDemoState();
}

class _StarRatingDemoState extends State<StarRatingDemo> {
 /// ClipPath Star Rating
 _buildClipPathStarRating(double rate, int count) {
  return Container(
   padding: EdgeInsets.fromLTRB(24.0, 16.0, 24.0, 0.0),
   child: StaticRatingBar(
    size: 50.0,
    rate: rate,
    count: count,
   ),
  );
 }
 
 @override
 Widget build(BuildContext context) {
  return Scaffold(
   appBar: AppBar(
    centerTitle: true,
    title: Text('Star Rating'),
   ),
   body: ListView(
    physics: BouncingScrollPhysics(),
    children: <Widget>[
     // _buildClipPathStarRating(1.0, 1),
     _buildClipPathStarRating(0.5, 5),
     _buildClipPathStarRating(2.0, 5),
     _buildClipPathStarRating(3.0, 5),
     _buildClipPathStarRating(4.0, 5),
     _buildClipPathStarRating(5.0, 5),
     _buildClipPathStarRating(5.5, 6),
     SizedBox(height: 16.0),
    ],
   ),
  );
 }
}

class StaticRatingBar extends StatelessWidget {
 /// Number of stars
 final int count;
 
 /// Init rate
 final double rate;
 
 /// Size of the starts
 final double size;
 
 final Color colorLight;
 
 final Color colorDark;
 
 StaticRatingBar({
  this.rate = 5,
  this.colorLight = const Color(0xFF1E88E5),
  this.colorDark = const Color(0xFFEEEEEE),
  this.count = 5,
  this.size = 60,
 });
 
 Widget buildDarkStar() {
  return SizedBox(
   width: size * count,
   height: size,
   child: CustomPaint(
    painter: _PainterStars(
     count: count,
     color: colorDark,
     strokeWidth: 0.0,
     size: this.size / 2,
     style: PaintingStyle.fill,
    ),
   ),
  );
 }
 
 Widget buildLightStar() {
  return ClipRect(
   clipper: _RatingBarClipper(rate * size),
   child: SizedBox(
    height: size,
    width: size * count,
    child: CustomPaint(
     painter: _PainterStars(
      count: count,
      strokeWidth: 0.0,
      color: colorLight,
      size: this.size / 2,
      style: PaintingStyle.fill,
     ),
    ),
   ),
  );
 }
 
 @override
 Widget build(BuildContext context) {
  return Stack(
   children: <Widget>[
    buildDarkStar(),
    buildLightStar(),
   ],
  );
 }
}

class _RatingBarClipper extends CustomClipper<Rect> {
 final double width;
 
 _RatingBarClipper(this.width);
 
 @override
 Rect getClip(Size size) {
  return Rect.fromLTRB(0.0, 0.0, width, size.height);
 }
 
 @override
 bool shouldReclip(_RatingBarClipper oldClipper) {
  return false;
 }
}

class _PainterStars extends CustomPainter {
 final double size;
 final int count;
 final Color color;
 final PaintingStyle style;
 final double strokeWidth;
 
 _PainterStars({
  this.size,
  this.count,
  this.color,
  this.strokeWidth,
  this.style,
 });

 double degree2Radian(int degree) {
  return (pi * degree / 180);
 }

 Path createStarPath(double radius, Path path) {
  double radian = degree2Radian(36);
  double radiusIn = (radius * sin(radian / 2) / cos(radian)) * 1.1;
  path.moveTo((radius * cos(radian / 2)), 0.0);
  path.lineTo(
   (radius * cos(radian / 2) + radiusIn * sin(radian)),
   (radius - radius * sin(radian / 2)),
  );
  path.lineTo(
   (radius * cos(radian / 2) * 2),
   (radius - radius * sin(radian / 2)),
  );
  path.lineTo(
   (radius * cos(radian / 2) + radiusIn * cos(radian / 2)),
   (radius + radiusIn * sin(radian / 2)),
  );
  path.lineTo(
   (radius * cos(radian / 2) + radius * sin(radian)),
   (radius + radius * cos(radian)),
  );
  path.lineTo((radius * cos(radian / 2)), (radius + radiusIn));
  path.lineTo(
   (radius * cos(radian / 2) - radius * sin(radian)),
   (radius + radius * cos(radian)),
  );
  path.lineTo(
   (radius * cos(radian / 2) - radiusIn * cos(radian / 2)),
   (radius + radiusIn * sin(radian / 2)),
  );
  path.lineTo(0.0, (radius - radius * sin(radian / 2)));
  path.lineTo(
   (radius * cos(radian / 2) - radiusIn * sin(radian)),
   (radius - radius * sin(radian / 2)),
  );
  path.lineTo((radius * cos(radian / 2)), 0.0);
  return path;
 }

 @override
 void paint(Canvas canvas, Size size) {
  Paint paint = Paint();
  paint.strokeWidth = strokeWidth;
  paint.color = color;
  paint.style = style;
  Path path = Path();
  double offset = strokeWidth > 0 ? strokeWidth + 2 : 0.0;
  
  path = createStarPath(this.size - offset, path);
  for (int i = 0; i < count - 1; i++) {
   path = path.shift(Offset(this.size * 2, 0.0));
   path = createStarPath(this.size - offset, path);
  }
 
  if (offset > 0) {
   path = path.shift(Offset(offset, offset));
  }
  path.close();
  canvas.drawPath(path, paint);
 }
 
 @override
 bool shouldRepaint(_PainterStars oldDelegate) {
  return oldDelegate.size != this.size;
 }
}

代码地址

https://github.com/smiling1990/FlutterClipper

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java数据结构之数组(动力节点之Java学院整理)

    Java数据结构之数组(动力节点之Java学院整理)

    这篇文章主要介绍了Java数据结构之数组(动力节点之Java学院整理)的相关资料,包括创建和内存分配,数组封装后的使用等,需要的朋友参考下吧
    2017-04-04
  • 全面理解java中的异常处理机制

    全面理解java中的异常处理机制

    下面小编就为大家带来一篇全面理解java中的异常处理机制。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • 浅谈StringBuilder类的capacity()方法和length()方法的一些小坑

    浅谈StringBuilder类的capacity()方法和length()方法的一些小坑

    这篇文章主要介绍了StringBuilder类的capacity()方法和length()方法的一些小坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • java实现拼图小游戏

    java实现拼图小游戏

    这篇文章主要为大家详细介绍了java实现拼图小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • 带你了解Java数据结构和算法之无权无向图

    带你了解Java数据结构和算法之无权无向图

    这篇文章主要为大家介绍了Java数据结构和算法之无权无向图 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • java面试题——详解HashMap和Hashtable 的区别

    java面试题——详解HashMap和Hashtable 的区别

    本篇文章主要介绍了java中HashMap和Hashtable的区别,具有一定的参考价值,有需要的可以了解一下。
    2016-11-11
  • vue+springboot+shiro+jwt实现登录功能

    vue+springboot+shiro+jwt实现登录功能

    这篇文章主要介绍了vue+springboot+shiro+jwt实现登录功能,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • Java集合中contains方法的效率对比分析

    Java集合中contains方法的效率对比分析

    这篇文章主要介绍了Java集合中contains方法的效率对比分析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • Java中的 stop the world是什么呢

    Java中的 stop the world是什么呢

    这篇文章主要介绍了Java中的stop the world是什么呢,从字面上讲,就是停止这个世界,看到这个字眼,就觉得这是可怕的事情,那到底什么是stop-the-world,本文给大家详细讲解,感兴趣的朋友跟随小编一起看看吧
    2023-05-05
  • 详解Java深拷贝,浅拷贝和Cloneable接口

    详解Java深拷贝,浅拷贝和Cloneable接口

    这篇文章主要为大家详细介绍了Java中Cloneable接口以及深拷贝与浅拷贝的相关知识,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-08-08

最新评论