Flutter:如何使用 CustomPaint 绘制心形
在Flutter中,要使用CustomPaint来绘制一个心形图案,你需要定义一个CustomPainter类,并在该类中实现paint
方法来绘制图案。以下是一个简单的例子:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
size: Size(200, 200),
painter: HeartPainter(),
),
),
),
);
}
}
class HeartPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.red
..style = PaintingStyle.fill;
final offset = Offset(size.width / 2, size.height / 2);
final scale = min(size.width, size.height) / 200;
canvas.translate(offset.dx, offset.dy);
canvas.scale(scale);
// 心形路径定义
Path path = Path()
..moveTo(85.873, 33.087)
..cubicTo(
48.503, 15.313, 29.893, 10.75, 38.613, 10.75)
..cubicTo(57.313, 10.75, 70.107, 22.479, 70.107, 34.113)
..cubicTo(70.107, 45.747, 57.313, 57.938, 43.827, 57.938)
..cubicTo(30.343, 57.938, 17.687, 45.747, 17.687, 34.113)
..cubicTo(17.687, 22.479, 30.343, 10.75, 43.827, 10.75)
..cubicTo(57.313, 10.75, 69.688, 22.479, 69.688, 34.113)
..cubicTo(69.688, 45.747, 57.313, 57.938, 43.827, 57.938)
..cubicTo(30.343, 57.938, 17.687, 45.747, 17.687, 34.113)
..cubicTo(17.687, 27.52, 30.343, 14.563, 43.827, 14.563)
..cubicTo(57.313, 14.563, 69.688, 27.52, 69.688, 34.113)
..cubicTo(69.688, 35.747, 57.313, 44.938, 43.827, 44.938)
..cubicTo(30.343, 44.938, 17.687, 35.747, 17.687, 34.113)
..cubicTo(17.687, 22.479,
评论已关闭