-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathlite_rolling_switch.dart
242 lines (226 loc) · 7.01 KB
/
lite_rolling_switch.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
library lite_rolling_switch;
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
/// Customable and nice Switch button :).
///
/// Currently, you can change the widget
/// width but not the height property.
///
/// The following arguments are required:
///
/// * [value] determines switch state (on/off).
/// * [onChanged] is called when user toggles switch on/off.
/// * [onTap] event called on tap
/// * [onDoubleTap] event called on double tap
/// * [onSwipe] event called on swipe (left/right)
///
class LiteRollingSwitch extends StatefulWidget {
@required
final bool value;
final double width;
@required
final Function(bool) onChanged;
final String textOff;
final Color textOffColor;
final String textOn;
final Color textOnColor;
final Color colorOn;
final Color colorOff;
final double textSize;
final Duration animationDuration;
final IconData iconOn;
final IconData iconOff;
final Function onTap;
final Function onDoubleTap;
final Function onSwipe;
LiteRollingSwitch({
this.value = false,
this.width = 130,
this.textOff = "Off",
this.textOn = "On",
this.textSize = 14.0,
this.colorOn = Colors.green,
this.colorOff = Colors.red,
this.iconOff = Icons.flag,
this.iconOn = Icons.check,
this.animationDuration = const Duration(milliseconds: 600),
this.textOffColor = Colors.white,
this.textOnColor = Colors.black,
required this.onTap,
required this.onDoubleTap,
required this.onSwipe,
required this.onChanged,
});
@override
_RollingSwitchState createState() => _RollingSwitchState();
}
class _RollingSwitchState extends State<LiteRollingSwitch>
with SingleTickerProviderStateMixin {
/// Late declarations
late AnimationController animationController;
late Animation<double> animation;
late bool turnState;
double value = 0.0;
@override
void dispose() {
//Ensure to dispose animation controller
animationController.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this,
lowerBound: 0.0,
upperBound: 1.0,
duration: widget.animationDuration);
animation =
CurvedAnimation(parent: animationController, curve: Curves.easeInOut);
animationController.addListener(() {
setState(() {
value = animation.value;
});
});
turnState = widget.value;
// Executes a function only one time after the layout is completed.
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
if (turnState) {
animationController.forward();
}
});
});
}
@override
Widget build(BuildContext context) {
//Color transition animation
Color? transitionColor = Color.lerp(widget.colorOff, widget.colorOn, value);
return GestureDetector(
onDoubleTap: () {
_action();
widget.onDoubleTap();
},
onTap: () {
_action();
widget.onTap();
},
onPanEnd: (details) {
_action();
widget.onSwipe();
},
child: Container(
padding: EdgeInsets.all(5),
width: widget.width,
decoration: BoxDecoration(
color: transitionColor, borderRadius: BorderRadius.circular(50)),
child: Stack(
children: <Widget>[
Transform.translate(
offset: isRTL(context)
? Offset(-10 * value, 0)
: Offset(10 * value, 0), //original
child: Opacity(
opacity: (1 - value).clamp(0.0, 1.0),
child: Container(
padding: isRTL(context)
? EdgeInsets.only(left: 10)
: EdgeInsets.only(right: 10),
alignment: isRTL(context)
? Alignment.centerLeft
: Alignment.centerRight,
height: 40,
child: Text(
widget.textOff,
style: TextStyle(
color: widget.textOffColor,
fontWeight: FontWeight.bold,
fontSize: widget.textSize),
),
),
),
),
Transform.translate(
offset: isRTL(context)
? Offset(-10 * (1 - value), 0)
: Offset(10 * (1 - value), 0), //original
child: Opacity(
opacity: value.clamp(0.0, 1.0),
child: Container(
padding: isRTL(context)
? EdgeInsets.only(right: 5)
: EdgeInsets.only(left: 5),
alignment: isRTL(context)
? Alignment.centerRight
: Alignment.centerLeft,
height: 40,
child: Text(
widget.textOn,
style: TextStyle(
color: widget.textOnColor,
fontWeight: FontWeight.bold,
fontSize: widget.textSize),
),
),
),
),
Transform.translate(
offset: isRTL(context)
? Offset((-widget.width + 50) * value, 0)
: Offset((widget.width - 50) * value, 0),
child: Transform.rotate(
angle: 0,
child: Container(
height: 40,
width: 40,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle, color: Colors.white),
child: Stack(
children: <Widget>[
Center(
child: Opacity(
opacity: (1 - value).clamp(0.0, 1.0),
child: Icon(
widget.iconOff,
size: 25,
color: transitionColor,
),
),
),
Center(
child: Opacity(
opacity: value.clamp(0.0, 1.0),
child: Icon(
widget.iconOn,
size: 21,
color: transitionColor,
),
),
),
],
),
),
),
)
],
),
),
);
}
_action() {
_determine(changeState: true);
}
_determine({bool changeState = false}) {
setState(() {
if (changeState) turnState = !turnState;
(turnState)
? animationController.forward()
: animationController.reverse();
widget.onChanged(turnState);
});
}
}
bool isRTL(BuildContext context) {
return Bidi.isRtlLanguage(Localizations.localeOf(context).languageCode);
}