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
| import 'dart:math' as math;
import 'package:flutter/material.dart';
void main() { runApp(const MyApp()); }
class MyApp extends StatelessWidget { const MyApp({super.key});
@override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple)), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } }
class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title;
@override State<MyHomePage> createState() => _MyHomePageState(); }
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin { late final AnimationController _controller; late final Animation<double> _progress;
@override void initState() { super.initState(); _controller = AnimationController(vsync: this, duration: const Duration(seconds: 4))..repeat(); _progress = CurvedAnimation(parent: _controller, curve: Curves.easeInOut); }
@override void dispose() { _controller.dispose(); super.dispose(); }
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title)), body: Center( child: ClipRRect( borderRadius: BorderRadius.circular(16), child: SizedBox( width: 320, height: 220, child: CustomPaint( isComplex: true, willChange: true, painter: MyPainter( progress: _progress, baseColor: Colors.indigo, accentColor: Colors.cyanAccent, showGrid: true, ), foregroundPainter: MyForegroundPainter(progress: _progress), child: const Padding( padding: EdgeInsets.all(24), child: Align( alignment: Alignment.bottomLeft, child: Text( 'CustomPaint Demo\n背景 + 子组件 + 前景', style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w600, height: 1.4), ), ), ), ), ), ), ), ); } }
class MyPainter extends CustomPainter { MyPainter({ required this.progress, this.baseColor = Colors.indigo, this.accentColor = Colors.cyanAccent, this.showGrid = true, }) : super(repaint: progress);
final Listenable progress; final Color baseColor; final Color accentColor; final bool showGrid;
double get _t => progress is Animation<double> ? (progress as Animation<double>).value : 0;
@override void paint(Canvas canvas, Size size) { final t = _t; final bgRect = Offset.zero & size;
final bgPaint = Paint() ..shader = LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [ Color.lerp(baseColor, accentColor, t * 0.6)!, Color.lerp(baseColor.withValues(alpha: 0.4), accentColor, t)!, ], ).createShader(bgRect); canvas.drawRect(bgRect, bgPaint);
if (showGrid) { _drawGrid(canvas, size, t); } _drawWave(canvas, size, t); _drawRings(canvas, size, t); }
void _drawGrid(Canvas canvas, Size size, double t) { const step = 24.0; final gridPaint = Paint() ..color = Colors.white.withValues(alpha: 0.08) ..strokeWidth = 1;
final offset = (t * step) % step; for (double x = -offset; x <= size.width; x += step) { canvas.drawLine(Offset(x, 0), Offset(x, size.height), gridPaint); } for (double y = -offset; y <= size.height; y += step) { canvas.drawLine(Offset(0, y), Offset(size.width, y), gridPaint); } }
void _drawWave(Canvas canvas, Size size, double t) { final path = Path(); final waveHeight = size.height * 0.12; final baseY = size.height * 0.65;
path.moveTo(0, baseY); for (double x = 0; x <= size.width; x += 2) { final y = baseY + math.sin((x / size.width * 2 * math.pi) + (t * 2 * math.pi)) * waveHeight; path.lineTo(x, y); } path ..lineTo(size.width, size.height) ..lineTo(0, size.height) ..close();
final wavePaint = Paint() ..shader = LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [accentColor.withValues(alpha: 0.35), accentColor.withValues(alpha: 0.05)], ).createShader(Offset.zero & size);
canvas.drawPath(path, wavePaint); }
void _drawRings(Canvas canvas, Size size, double t) { final center = Offset(size.width * 0.75, size.height * 0.28); for (int i = 0; i < 3; i++) { final radius = 30.0 + i * 18 + t * 12; final ringPaint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 2.5 - i * 0.5 ..color = Colors.white.withValues(alpha: 0.25 - i * 0.05); canvas.drawCircle(center, radius, ringPaint); } }
@override bool shouldRepaint(covariant MyPainter oldDelegate) { return oldDelegate.baseColor != baseColor || oldDelegate.accentColor != accentColor || oldDelegate.showGrid != showGrid || oldDelegate.progress != progress; } }
class MyForegroundPainter extends CustomPainter { MyForegroundPainter({required this.progress}) : super(repaint: progress);
final Listenable progress;
double get _t => progress is Animation<double> ? (progress as Animation<double>).value : 0;
@override void paint(Canvas canvas, Size size) { final scanY = size.height * _t; final scanPaint = Paint() ..shader = LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [Colors.transparent, Colors.white.withValues(alpha: 0.15), Colors.transparent], stops: const [0.0, 0.5, 1.0], ).createShader(Rect.fromLTWH(0, scanY - 20, size.width, 40));
canvas.drawRect(Rect.fromLTWH(0, scanY - 20, size.width, 40), scanPaint);
final borderPaint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 2 ..color = Colors.white.withValues(alpha: 0.35); canvas.drawRRect( RRect.fromRectAndRadius(Rect.fromLTWH(1, 1, size.width - 2, size.height - 2), const Radius.circular(16)), borderPaint, ); }
@override bool shouldRepaint(covariant MyForegroundPainter oldDelegate) => oldDelegate.progress != progress; }
|