-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathlight_effect_demo.dart
260 lines (232 loc) · 7.29 KB
/
light_effect_demo.dart
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import 'dart:math';
import 'dart:ui';
import 'package:flutter/material.dart';
class LightEffectWidgetDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: GradientControlPage(),
);
}
}
class GradientControlPage extends StatefulWidget {
const GradientControlPage({super.key});
@override
State<GradientControlPage> createState() => _GradientControlPageState();
}
class _GradientControlPageState extends State<GradientControlPage> {
double startAngle = 33.7 * pi / 180; // -45 degrees
double sweepAngle = pi / 2; // 90 degrees
double gradientRadius = 0.8;
bool showDebugElements = true;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Column(
children: [
Expanded(
child: Center(
child: GradientDemo(
startAngle: startAngle,
sweepAngle: sweepAngle,
gradientRadius: gradientRadius,
showDebugElements: showDebugElements,
),
),
),
Container(
padding: const EdgeInsets.all(16),
color: Colors.black87,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
if (showDebugElements) ...[
_buildControlLabel(
'Angle: ${(startAngle * 180 / pi).toStringAsFixed(1)}°'),
Slider(
value: startAngle,
min: -pi,
max: pi,
onChanged: (value) => setState(() => startAngle = value),
),
],
CheckboxListTile(
title: const Text('Debug'),
value: showDebugElements,
onChanged: (value) =>
setState(() => showDebugElements = value ?? true),
),
],
),
),
],
),
);
}
Widget _buildControlLabel(String text) {
return Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
text,
style: const TextStyle(color: Colors.white70),
),
);
}
}
class GradientDemo extends StatelessWidget {
final double startAngle;
final double sweepAngle;
final double gradientRadius;
final bool showDebugElements;
const GradientDemo({
super.key,
required this.startAngle,
required this.sweepAngle,
required this.gradientRadius,
required this.showDebugElements,
});
@override
Widget build(BuildContext context) {
return SizedBox(
width: 300,
height: 250, // Increased height to accommodate the blur rectangle
child: Stack(
clipBehavior: Clip.none,
children: [
CustomPaint(
size: const Size(300, 250),
painter: GradientPainter(
startAngle: startAngle,
sweepAngle: sweepAngle,
gradientRadius: gradientRadius,
showDebugElements: showDebugElements,
),
),
Positioned(
bottom: -50, // Position it half outside
left: 0,
right: 0,
height: 100, // Total height of blur rectangle
child: ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: 0,
sigmaY: 10,
),
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: 0,
sigmaY: 10,
),
child: Container(
color: Colors.white.withOpacity(0.00001),
),
),
),
),
),
],
),
);
}
}
class GradientPainter extends CustomPainter {
final double startAngle;
final double sweepAngle;
final double gradientRadius;
final bool showDebugElements;
GradientPainter({
required this.startAngle,
required this.sweepAngle,
required this.gradientRadius,
required this.showDebugElements,
});
@override
void paint(Canvas canvas, Size size) {
final rect = Offset.zero & size;
// Draw white rectangle
final backgroundPaint = Paint()..color = Colors.white;
canvas.drawRect(rect, backgroundPaint);
// Calculate centers for both gradients
final leftCenter = Offset(size.width / 4, 0);
final rightCenter = Offset(3 * size.width / 4, 0);
final radius = size.height * gradientRadius;
// Left gradient
final leftGradientPaint = Paint()
..shader = SweepGradient(
center: Alignment(-0.5, 0), // Left center
startAngle: startAngle,
endAngle: startAngle + sweepAngle,
colors: [
Colors.white.withOpacity(0),
Colors.black,
],
stops: const [0.0, 1.0],
).createShader(rect)
..style = PaintingStyle.fill;
// Right gradient (mirrored)
final rightGradientPaint = Paint()
..shader = SweepGradient(
center: Alignment(0.5, 0), // Right center
startAngle: pi - startAngle - sweepAngle,
endAngle: pi - startAngle,
colors: [
Colors.black,
Colors.white.withOpacity(0),
],
stops: const [0.0, 1.0],
).createShader(rect)
..style = PaintingStyle.fill;
// Draw both gradients
canvas.drawRect(rect, leftGradientPaint);
canvas.drawRect(rect, rightGradientPaint);
if (!showDebugElements) return;
// Debug elements for both centers
final centerPointPaint = Paint()
..color = Colors.blue
..style = PaintingStyle.fill;
canvas.drawCircle(leftCenter, 4, centerPointPaint);
canvas.drawCircle(rightCenter, 4, centerPointPaint);
// Debug circles
final debugCirclePaint = Paint()
..color = Colors.blue.withOpacity(0.3)
..style = PaintingStyle.stroke
..strokeWidth = 1;
// Draw dashed circles for both gradients
_drawDashedCircle(canvas, leftCenter, radius, debugCirclePaint);
_drawDashedCircle(canvas, rightCenter, radius, debugCirclePaint);
// Corner points
final cornerPointPaint = Paint()
..color = Colors.red
..style = PaintingStyle.fill;
canvas.drawCircle(Offset.zero, 3, cornerPointPaint);
canvas.drawCircle(Offset(size.width, 0), 3, cornerPointPaint);
canvas.drawCircle(Offset(0, size.height), 3, cornerPointPaint);
canvas.drawCircle(Offset(size.width, size.height), 3, cornerPointPaint);
}
void _drawDashedCircle(
Canvas canvas, Offset center, double radius, Paint paint) {
const dashLength = 5;
const dashSpace = 5;
double currentAngle = 0;
while (currentAngle < 2 * pi) {
canvas.drawArc(
Rect.fromCircle(center: center, radius: radius),
currentAngle,
dashLength / radius,
false,
paint,
);
currentAngle += (dashLength + dashSpace) / radius;
}
}
@override
bool shouldRepaint(GradientPainter oldDelegate) =>
oldDelegate.startAngle != startAngle ||
oldDelegate.sweepAngle != sweepAngle ||
oldDelegate.gradientRadius != gradientRadius ||
oldDelegate.showDebugElements != showDebugElements;
}