-
Notifications
You must be signed in to change notification settings - Fork 0
/
Street light on and off animation
214 lines (197 loc) · 6.3 KB
/
Street light on and off animation
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
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(
debugShowCheckedModeBanner: false,
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
bool islighton = true;
Offset initialPosition = const Offset(250, 0);
Offset switchPosition = const Offset(350, 350);
Offset containerPosition = const Offset(350, 350);
Offset finalPosition = const Offset(350, 350);
double lightlength = 0;
@override
void didChangeDependencies() {
final size = MediaQuery.of(context).size;
initialPosition = Offset(size.width * .9, 0);
containerPosition = Offset(size.width * .9, size.height * .4);
finalPosition = Offset(size.width * .9, size.height * .5 - size.width * .1);
if (islighton) {
switchPosition = containerPosition;
} else {
switchPosition = finalPosition;
}
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return Scaffold(
backgroundColor: const Color(0xff374151),
body: Stack(
fit: StackFit.expand,
children: [
Container(
color: Colors.transparent,
alignment: Alignment.centerLeft,
child: Stack(
children: [
AspectRatio(
aspectRatio: 1,
child: Image.asset("assets/images/lamp.png")),
Positioned(
top: 80,
right: 85,
child: ClipPath(
clipper: MySpotLightClipper(),
child: AnimatedContainer(
duration: const Duration(milliseconds: 100),
decoration: BoxDecoration(
color: Colors.amber,
borderRadius: BorderRadius.circular(10),
),
height: lightlength,
width: 150,
),
),
)
],
),
),
Positioned(
top: containerPosition.dy - size.width * .1 / 2 - 1,
left: containerPosition.dx - size.width * .1 / 2 - 5,
child: Container(
height: size.height * .13 + 10,
width: size.width * .1 + 10,
decoration: BoxDecoration(
color: Colors.grey,
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.7),
offset: const Offset(5.0, 5.0),
blurRadius: 10.0,
spreadRadius: 0,
),
BoxShadow(
color: Colors.grey.shade900.withOpacity(0.5),
offset: const Offset(-3.0, -3.0),
blurRadius: 5,
spreadRadius: 0,
),
],
borderRadius: BorderRadius.circular(30)),
)),
Wire(
initialPosition: initialPosition,
toOffset: switchPosition,
),
AnimatedPositioned(
duration: const Duration(microseconds: 0),
top: switchPosition.dy - size.width * 0.1 / 2,
left: switchPosition.dx - size.width * 0.1 / 2,
child: Draggable(
feedback: Container(
height: size.height * .1,
width: size.width * .1,
decoration: const BoxDecoration(
shape: BoxShape.circle,
)),
onDragEnd: (details) {
if (islighton) {
islighton = !islighton;
lightlength = 260;
switchPosition = containerPosition;
} else {
islighton = !islighton;
lightlength = 0;
switchPosition = finalPosition;
}
},
onDragUpdate: (details) {
setState(() {
switchPosition = details.localPosition;
});
},
child: Container(
height: size.height * .1,
width: size.width * .1,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: Colors.white, width: 3),
color: Colors.amber,
),
),
),
)
],
),
);
}
}
class Wire extends StatefulWidget {
final Offset toOffset;
final Offset initialPosition;
const Wire({Key? key, required this.toOffset, required this.initialPosition})
: super(key: key);
@override
State<StatefulWidget> createState() => _WireState();
}
class _WireState extends State<Wire> with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: LinePainter(
toOffset: widget.toOffset,
initialPosition: widget.initialPosition,
));
}
}
class LinePainter extends CustomPainter {
Paint? _paint;
final Offset initialPosition;
final Offset toOffset;
LinePainter({required this.toOffset, required this.initialPosition}) {
_paint = Paint()
..color = Colors.white
..strokeWidth = 10;
}
@override
void paint(Canvas canvas, Size size) {
canvas.drawLine(initialPosition, toOffset, _paint!);
}
@override
bool shouldRepaint(LinePainter oldDelegate) {
return false;
}
}
class MySpotLightClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
Path path = Path();
path.moveTo(size.width / 2 - 20, 0);
path.lineTo(0, size.height);
path.lineTo(size.width, size.height);
path.lineTo(size.width / 2 + 25, 0);
path.close();
return path;
}
/// this must be true
@override
bool shouldReclip(covariant CustomClipper<Path> oldClipper) => true;
// TODO: implement shouldReclip
}