Flutter:如何使用 CustomPaint 绘制心形

// Hearts with borders

CustomPaint(

size: const Size(200, 120),

painter: MyPainter(Colors.purple, Colors.black, 10),

),

CustomPaint(

size: const Size(200, 240),

painter: MyPainter(Colors.red, Colors.redAccent, 5),

),

CustomPaint(

size: const Size(50, 100),

painter: MyPainter(Colors.amber, Colors.indigo, 10),

),

最终代码

这是main.dart中的完整代码,它生成了上面屏幕截图中显示的很酷的心形:

// main.dart

import ‘package:flutter/material.dart’;

void main() {

runApp(const MyApp());

}

class MyApp extends StatelessWidget {

const MyApp({Key? key}) : super(key: key);

@override

Widget build(BuildContext context) {

return MaterialApp(

// Hide the debug banner

debugShowCheckedModeBanner: false,

title: ‘breeze’,

theme: ThemeData(

primarySwatch: Colors.indigo,

),

home: const HomeScreen(),

);

}

}

// Implementing our heart painter

class MyPainter extends CustomPainter {

// The color of the heart

final Color bodyColor;

// The color of the border of the heart

final Color borderColor;

// The thickness of the border

final double borderWith;

MyPainter(this.bodyColor, this.borderColor, this.borderWith);

@override

void paint(Canvas canvas, Size size) {

// The body of the heart

final Paint body = Paint();

body

…color = bodyColor

…style = PaintingStyle.fill

…strokeWidth = 0;

// The border of the heart

final Paint border = Paint();

border

…color = borderColor

…style = PaintingStyle.stroke

…strokeCap = StrokeCap.round

…strokeWidth = borderWith;

final double width = size.width;

final double height = size.height;

final Path path = Path();

path.moveTo(0.5 * width, height * 0.4);

path.cubicTo(0.2 * width, height * 0.1, -0.25 * width, height * 0.6,

0.5 * width, height);

path.moveTo(0.5 * width, height * 0.4);

path.cubicTo(0.8 * width, height * 0.1, 1.25 * width, height * 0.6,

0.5 * width, height);

canvas.drawPath(path, body);

canvas.drawPath(path, border);

}

@override

bool shouldRepaint(CustomPainter oldDelegate) {

return true;

}

}

class HomeScreen extends StatefulWidget {

const HomeScreen({Key? key}) : super(key: key);

@override

State createState() => _HomeScreenState();

}

class _HomeScreenState extends State {

@override

Widget build(BuildContext context) {

return Scaffold(

appBar: AppBar(

title: const Text(‘KindaCode.com’),

),

body: Center(

child: Column(

mainAxisAlignment: MainAxisAlignment.spaceAround,

mainAxisSize: MainAxisSize.min,

children: [

// Non-border heart

CustomPaint(

size: const Size(280, 260),

painter: MyPainter(Colors.pink, Colors.transparent, 0),

),

// Hearts with borders

CustomPaint(

size: const Size(200, 120),

painter: MyPainter(Colors.purple, Colors.black, 10),

),

CustomPaint(

size: const Size(200, 240),

painter: MyPainter(Colors.red, Colors.redAccent, 5),

),

CustomPaint(

size: const Size(50, 100),

painter: MyPainter(Colors.amber, Colors.indigo, 10),

),

],

)),

);

}

}

参考


您可以在官方文档中找到有关 CustomPaint 小部件和 CustomPainter 类的更多详细信息:

后记

尾声

面试成功其实都是必然发生的事情,因为在此之前我做足了充分的准备工作,不单单是纯粹的刷题,更多的还会去刷一些Android核心架构进阶知识点,比如:JVM、高并发、多线程、缓存、热修复设计、插件化框架解读、组件化框架设计、图片加载框架、网络、设计模式、设计思想与代码质量优化、程序性能优化、开发效率优化、设计模式、负载均衡、算法、数据结构、高级UI晋升、Framework内核解析、Android组件内核等。

不仅有学习文档,视频+笔记提高学习效率,还能稳固你的知识,形成良好的系统的知识体系。这里,笔者分享一份从架构哲学的层面来剖析的视频及资料分享给大家梳理了多年的架构经验,筹备近6个月最新录制的,相信这份视频能给你带来不一样的启发、收获。

Android进阶学习资料库

一共十个专题,包括了Android进阶所有学习资料,Android进阶视频,Flutter,java基础,kotlin,NDK模块,计算机网络,数据结构与算法,微信小程序,面试题解析,framework源码!

image

大厂面试真题

PS:之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

《2017-2021字节跳动Android面试历年真题解析》


《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
AT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

[外链图片转存中…(img-chyWO2gZ-1714847870990)]

《2017-2021字节跳动Android面试历年真题解析》

[外链图片转存中…(img-f5WBNZ4n-1714847870993)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 15
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值