1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| import 'dart:math'; import 'dart:ui';
import 'package:flutter/material.dart'; import 'package:like_button/like_button.dart';
void main() { runApp(MaterialApp( home: CustomPaintExample(), )); }
class CustomPaintExample extends StatefulWidget { const CustomPaintExample({super.key});
@override State<CustomPaintExample> createState() => _CustomPaintExampleState(); }
class _CustomPaintExampleState extends State<CustomPaintExample> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('CustomPaint Example'), ), body: buildBody(), ); }
Future<ui.Image> load(String asset) async { ByteData data = await rootBundle.load(asset); ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List(), targetHeight: 100, targetWidth: 100); ui.FrameInfo fi = await codec.getNextFrame(); return fi.image; }
buildBody() { return Container( color: Colors.grey[300], width: 400, height: double.infinity, child: CustomPaint( foregroundPainter: CirclePainter( outerCircleRadiusProgress: 0.5, innerCircleRadiusProgress: 0.3, ), ), ); } }
class CirclePainter extends CustomPainter { CirclePainter( {required this.outerCircleRadiusProgress, required this.innerCircleRadiusProgress, this.circleColor = const CircleColor(start: Color(0xFFFF5722), end: Color(0xFFFFC107))}) { _circlePaint.style = PaintingStyle.stroke; }
final Paint _circlePaint = Paint();
final double outerCircleRadiusProgress; final double innerCircleRadiusProgress; final CircleColor circleColor;
drawPath(Canvas canvas, Size size) { Paint newPaint(Color color) { Paint paint = Paint() ..color = color ..style = PaintingStyle.stroke ..strokeWidth = 10; return paint; }
Path generatePath(double x, double y) { Path path = Path(); path.moveTo(x, y); path.lineTo(x + 100, y + 100); path.lineTo(x + 150, y + 80); path.lineTo(x + 100, y + 200); path.lineTo(x, y + 100); return path; }
canvas.drawPath(generatePath(100, 100), newPaint(Colors.red)); }
drawPoints(Canvas canvas, Size size) { Paint paint = Paint() ..color = Colors.red ..strokeWidth = 20;
canvas.drawPoints( PointMode.points, [ Offset(100, 100), Offset(250, 180), Offset(200, 300), ], paint); paint.strokeCap = StrokeCap.round; canvas.drawPoints(PointMode.points, [Offset(100, 200)], paint); }
drawLine(Canvas canvas, Size size) { Paint paint = Paint() ..color = Colors.black12 ..strokeWidth = 20; canvas.drawLine(Offset(100, 100), Offset(250, 180), paint); }
drawRect(Canvas canvas, Size size) { Paint paint = Paint() ..color = Colors.pink ..strokeWidth = 20;
canvas.drawRect(Rect.fromPoints(Offset(100, 120), Offset(130, 140)), paint); }
drawOval(Canvas canvas, Size size) { Paint paint = Paint() ..color = Colors.pink ..strokeWidth = 20; Rect pRect = Rect.fromLTRB(50, 150, 400, 350);
canvas.drawRect(pRect, paint); paint.color = Colors.yellow;
canvas.drawOval(pRect, paint); }
drawArc(Canvas canvas, Size size) { Paint paint = Paint()..color = Colors.pink; Rect rect = Rect.fromCircle(center: Offset(size.width / 2, size.height / 2), radius: 100); canvas.save(); canvas.drawRect(rect, Paint()..color = Colors.blue); canvas.restore(); canvas.drawArc(rect, 90 * (pi / 180), 90 * (pi / 180), false, paint); }
@override void paint(Canvas canvas, Size size) { drawPoints(canvas, size); drawLine(canvas, size); drawRect(canvas, size); drawOval(canvas, size); drawArc(canvas, size); drawPath(canvas, size); drawCircle(canvas, size); }
drawCircle(Canvas canvas, Size size) {
final double center = size.width * 0.5; _updateCircleColor(); final double strokeWidth = outerCircleRadiusProgress * center - (innerCircleRadiusProgress * center); if (strokeWidth > 0.0) { _circlePaint.strokeWidth = strokeWidth; canvas.drawCircle(Offset(center, center), outerCircleRadiusProgress * center, _circlePaint); } }
double clamp(double value, double low, double high) { return min(max(value, low), high); }
double mapValueFromRangeToRange(double value, double fromLow, double fromHigh, double toLow, double toHigh) { return toLow + ((value - fromLow) / (fromHigh - fromLow) * (toHigh - toLow)); }
void _updateCircleColor() { double colorProgress = clamp(outerCircleRadiusProgress, 0.5, 1.0); colorProgress = mapValueFromRangeToRange(colorProgress, 0.5, 1.0, 0.0, 1.0); _circlePaint.color = Color.lerp(circleColor.start, circleColor.end, colorProgress)!; }
@override bool shouldRepaint(CustomPainter oldDelegate) { if (oldDelegate.runtimeType != runtimeType) { return true; }
return oldDelegate is CirclePainter && (oldDelegate.outerCircleRadiusProgress != outerCircleRadiusProgress || oldDelegate.innerCircleRadiusProgress != innerCircleRadiusProgress || oldDelegate.circleColor.start != circleColor.start || oldDelegate.circleColor.end != circleColor.end); } }
|