Flutter 通過(guò)Clipper實(shí)現(xiàn)各種自定義形狀的示例代碼
本文介紹了Flutter 通過(guò)Clipper實(shí)現(xiàn)各種自定義形狀的示例代碼,分享給大家,具體如下:
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 裝飾形狀
通過(guò)BoxShape.circle實(shí)現(xiàn)圓形圖片
Container(
width: 120.0,
height: 120.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage(
Config.assets_avatar_1,
),
),
)
);
通過(guò)BorderRadius實(shí)現(xiàn)圓形圖片
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) 評(píng)分控件
評(píng)分控件 UI圖
實(shí)現(xiàn)方案
使用CustomPaint結(jié)合ClipPath畫出單個(gè)五角星;
- 使用Stack渲染兩層畫面
- 背景層,一排灰色五角星 前景層,一排亮色五角星,并使用ClipRect截取一定Width
實(shí)現(xiàn)代碼
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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Flutter實(shí)現(xiàn)容器組件、圖片組件 的代碼
- Flutter實(shí)現(xiàn)文本組件、圖標(biāo)及按鈕組件的代碼
- flutter 輪播圖動(dòng)態(tài)加載網(wǎng)絡(luò)圖片的方法
- Flutter實(shí)現(xiàn)可循環(huán)輪播圖效果
- Flutter banner_view 輪播圖的使用及實(shí)現(xiàn)代碼
- Flutter 封裝一個(gè) Banner 輪播圖效果的實(shí)例代碼
- Flutter Image實(shí)現(xiàn)圖片加載
- flutter實(shí)現(xiàn)輪播圖效果
- flutter PageView實(shí)現(xiàn)左右滑動(dòng)切換視圖
- Android Studio下Flutter環(huán)境搭建圖文教程
相關(guān)文章
Java SSH 秘鑰連接mysql數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Java SSH 秘鑰連接mysql數(shù)據(jù)庫(kù)的方法,包括引入依賴的代碼和出現(xiàn)異常報(bào)錯(cuò)問題,需要的朋友可以參考下2021-06-06
Java之InputStreamReader類的實(shí)現(xiàn)
這篇文章主要介紹了Java之InputStreamReader類的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
springboot登陸頁(yè)面圖片驗(yàn)證碼簡(jiǎn)單的web項(xiàng)目實(shí)現(xiàn)
這篇文章主要介紹了springboot登陸頁(yè)面圖片驗(yàn)證碼簡(jiǎn)單的web項(xiàng)目實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
Java基礎(chǔ)之打印萬(wàn)年歷的簡(jiǎn)單實(shí)現(xiàn)(案例)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)之打印萬(wàn)年歷的簡(jiǎn)單實(shí)現(xiàn)(案例)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
深入探究Bean生命周期的擴(kuò)展點(diǎn)Bean Post Processor
在Spring框架中,Bean生命周期的管理是非常重要的一部分,在Bean的創(chuàng)建、初始化和銷毀過(guò)程中,Spring提供了一系列的擴(kuò)展點(diǎn),其中,Bean Post Processor(后處理器)是一個(gè)重要的擴(kuò)展點(diǎn),它能夠在Bean的初始化前后做一些額外的處理,本文就和大家一起深入探究2023-07-07
詳解用Kotlin寫一個(gè)基于Spring Boot的RESTful服務(wù)
這篇文章主要介紹了詳解用Kotlin寫一個(gè)基于Spring Boot的RESTful服務(wù) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
解決Java 部署Tomcat時(shí)使用jni和jna調(diào)用DLL文件的問題
這篇文章主要介紹了解決Java 部署Tomcat時(shí)使用jni和jna調(diào)用DLL文件的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Java實(shí)戰(zhàn)之仿天貓商城系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java制作一個(gè)基于SSM框架的迷你天貓商城系統(tǒng),文中采用的技術(shù)有JSP、Springboot、SpringMVC、Spring等,需要的可以參考一下2022-03-03

