-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathflutter_switch.dart
462 lines (412 loc) · 14.8 KB
/
flutter_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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
library flutter_switch;
import 'package:flutter/material.dart';
class FlutterSwitch extends StatefulWidget {
/// Creates a material design switch.
///
/// The following arguments are required:
///
/// * [value] determines whether this switch is on or off.
/// * [onToggle] is called when the user toggles the switch on or off.
///
const FlutterSwitch({
Key? key,
required this.value,
required this.onToggle,
this.activeColor = Colors.blue,
this.inactiveColor = Colors.grey,
this.activeTextColor = Colors.white70,
this.inactiveTextColor = Colors.white70,
this.toggleColor = Colors.white,
this.activeToggleColor,
this.inactiveToggleColor,
this.width = 70.0,
this.height = 35.0,
this.toggleSize = 25.0,
this.valueFontSize = 16.0,
this.borderRadius = 20.0,
this.padding = const EdgeInsets.all(4.0),
this.showOnOff = false,
this.activeText,
this.inactiveText,
this.activeTextFontWeight,
this.inactiveTextFontWeight,
this.switchBorder,
this.activeSwitchBorder,
this.inactiveSwitchBorder,
this.toggleBorder,
this.activeToggleBorder,
this.inactiveToggleBorder,
this.activeIcon,
this.inactiveIcon,
this.duration = const Duration(milliseconds: 200),
this.disabled = false,
}) : assert(
(switchBorder == null || activeSwitchBorder == null) &&
(switchBorder == null || inactiveSwitchBorder == null),
'Cannot provide switchBorder when an activeSwitchBorder or inactiveSwitchBorder was given\n'
'To give the switch a border, use "activeSwitchBorder: border" or "inactiveSwitchBorder: border".'),
assert(
(toggleBorder == null || activeToggleBorder == null) &&
(toggleBorder == null || inactiveToggleBorder == null),
'Cannot provide toggleBorder when an activeToggleBorder or inactiveToggleBorder was given\n'
'To give the toggle a border, use "activeToggleBorder: color" or "inactiveToggleBorder: color".'),
super(key: key);
/// Determines if the switch is on or off.
///
/// This property is required.
final bool value;
/// Called when the user toggles the switch.
///
/// This property is required.
///
/// [onToggle] should update the state of the parent [StatefulWidget]
/// using the [setState] method, so that the parent gets rebuilt; for example:
///
/// ```dart
/// FlutterSwitch(
/// value: _status,
/// width: 110,
/// borderRadius: 30.0,
/// onToggle: (val) {
/// setState(() {
/// _status = val;
/// });
/// },
/// ),
/// ```
final ValueChanged<bool> onToggle;
/// Displays an on or off text.
///
/// Text value can be override by the [activeText] and
/// [inactiveText] properties.
///
/// Defaults to 'false' if no value was given.
final bool showOnOff;
/// The text to display when the switch is on.
/// This parameter is only necessary when [showOnOff] property is true.
///
/// Defaults to 'On' if no value was given.
///
/// To change value style, the following properties are available
///
/// [activeTextColor] - The color to use on the text value when the switch is on.
/// [activeTextFontWeight] - The font weight to use on the text value when the switch is on.
final String? activeText;
/// The text to display when the switch is off.
/// This parameter is only necessary when [showOnOff] property is true.
///
/// Defaults to 'Off' if no value was given.
///
/// To change value style, the following properties are available
///
/// [inactiveTextColor] - The color to use on the text value when the switch is off.
/// [inactiveTextFontWeight] - The font weight to use on the text value when the switch is off.
final String? inactiveText;
/// The color to use on the switch when the switch is on.
///
/// Defaults to [Colors.blue].
final Color activeColor;
/// The color to use on the switch when the switch is off.
///
/// Defaults to [Colors.grey].
final Color inactiveColor;
/// The color to use on the text value when the switch is on.
/// This parameter is only necessary when [showOnOff] property is true.
///
/// Defaults to [Colors.white70].
final Color activeTextColor;
/// The color to use on the text value when the switch is off.
/// This parameter is only necessary when [showOnOff] property is true.
///
/// Defaults to [Colors.white70].
final Color inactiveTextColor;
/// The font weight to use on the text value when the switch is on.
/// This parameter is only necessary when [showOnOff] property is true.
///
/// Defaults to [FontWeight.w900].
final FontWeight? activeTextFontWeight;
/// The font weight to use on the text value when the switch is off.
/// This parameter is only necessary when [showOnOff] property is true.
///
/// Defaults to [FontWeight.w900].
final FontWeight? inactiveTextFontWeight;
/// The color to use on the toggle of the switch.
///
/// Defaults to [Colors.white].
///
/// If the [activeSwitchBorder] or [inactiveSwitchBorder] is used, this property must be null.
final Color toggleColor;
/// The color to use on the toggle of the switch when the given value is true.
///
/// If [inactiveToggleColor] is used and this property is null. the value of
/// [Colors.white] will be used.
final Color? activeToggleColor;
/// The color to use on the toggle of the switch when the given value is false.
///
/// If [activeToggleColor] is used and this property is null. the value of
/// [Colors.white] will be used.
final Color? inactiveToggleColor;
/// The given width of the switch.
///
/// Defaults to a width of 70.0.
final double width;
/// The given height of the switch.
///
/// Defaults to a height of 35.0.
final double height;
/// The size of the toggle of the switch.
///
/// Defaults to a size of 25.0.
final double toggleSize;
/// The font size of the values of the switch.
/// This parameter is only necessary when [showOnOff] property is true.
///
/// Defaults to a size of 16.0.
final double valueFontSize;
/// The border radius of the switch.
///
/// Defaults to the value of 20.0.
final double borderRadius;
/// The padding of the switch.
///
/// Defaults to the value of 4.0.
final EdgeInsets padding;
/// The border of the switch.
///
/// This property will give a uniform border to both states of the toggle
///
/// If the [activeSwitchBorder] or [inactiveSwitchBorder] is used, this property must be null.
final BoxBorder? switchBorder;
/// The border of the switch when the given value is true.
///
/// This property is optional.
final BoxBorder? activeSwitchBorder;
/// The border of the switch when the given value is false.
///
/// This property is optional.
final BoxBorder? inactiveSwitchBorder;
/// The border of the toggle.
///
/// This property will give a uniform border to both states of the toggle
///
/// If the [activeToggleBorder] or [inactiveToggleBorder] is used, this property must be null.
final BoxBorder? toggleBorder;
/// The border of the toggle when the given value is true.
///
/// This property is optional.
final BoxBorder? activeToggleBorder;
/// The border of the toggle when the given value is false.
///
/// This property is optional.
final BoxBorder? inactiveToggleBorder;
/// The icon inside the toggle when the given value is true.
/// activeIcon can be an Icon Widget, an Image or Fontawesome Icons.
///
/// This property is optional.
final Widget? activeIcon;
/// The icon inside the toggle when the given value is false.
/// inactiveIcon can be an Icon Widget, an Image or Fontawesome Icons.
///
/// This property is optional.
final Widget? inactiveIcon;
/// The duration in milliseconds to change the state of the switch
///
/// Defaults to the value of 200 milliseconds.
final Duration duration;
/// Determines whether the switch is disabled.
///
/// Defaults to the value of false.
final bool disabled;
@override
_FlutterSwitchState createState() => _FlutterSwitchState();
}
class _FlutterSwitchState extends State<FlutterSwitch>
with SingleTickerProviderStateMixin {
late final Animation _toggleAnimation;
late final AnimationController _animationController;
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this,
value: widget.value ? 1.0 : 0.0,
duration: widget.duration,
);
_toggleAnimation = AlignmentTween(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
).animate(
CurvedAnimation(
parent: _animationController,
curve: Curves.linear,
),
);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
void didUpdateWidget(FlutterSwitch oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.value == widget.value) return;
if (widget.value)
_animationController.forward();
else
_animationController.reverse();
}
@override
Widget build(BuildContext context) {
Color _toggleColor = Colors.white;
Color _switchColor = Colors.white;
Border? _switchBorder;
Border? _toggleBorder;
if (widget.value) {
_toggleColor = widget.activeToggleColor ?? widget.toggleColor;
_switchColor = widget.activeColor;
_switchBorder = widget.activeSwitchBorder as Border? ??
widget.switchBorder as Border?;
_toggleBorder = widget.activeToggleBorder as Border? ??
widget.toggleBorder as Border?;
} else {
_toggleColor = widget.inactiveToggleColor ?? widget.toggleColor;
_switchColor = widget.inactiveColor;
_switchBorder = widget.inactiveSwitchBorder as Border? ??
widget.switchBorder as Border?;
_toggleBorder = widget.inactiveToggleBorder as Border? ??
widget.toggleBorder as Border?;
}
double _textSpace = widget.width - widget.toggleSize;
return AnimatedBuilder(
animation: _animationController,
builder: (context, child) {
return Container(
width: widget.width,
child: Align(
child: GestureDetector(
onTap: () {
if (!widget.disabled) {
if (widget.value)
_animationController.forward();
else
_animationController.reverse();
widget.onToggle(!widget.value);
}
},
child: Opacity(
opacity: widget.disabled ? 0.6 : 1,
child: Container(
width: widget.width,
height: widget.height,
padding: widget.padding,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(widget.borderRadius),
color: _switchColor,
border: _switchBorder,
),
child: Stack(
children: <Widget>[
AnimatedOpacity(
opacity: widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: Container(
width: _textSpace,
padding: EdgeInsets.symmetric(horizontal: 4.0),
alignment: Alignment.centerLeft,
child: _activeText,
),
),
Align(
alignment: Alignment.centerRight,
child: AnimatedOpacity(
opacity: !widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: Container(
width: _textSpace,
padding: EdgeInsets.symmetric(horizontal: 4.0),
alignment: Alignment.centerRight,
child: _inactiveText,
),
),
),
Container(
child: Align(
alignment: _toggleAnimation.value,
child: Container(
width: widget.toggleSize,
height: widget.toggleSize,
padding: EdgeInsets.all(4.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _toggleColor,
border: _toggleBorder,
),
child: FittedBox(
fit: BoxFit.contain,
child: Container(
child: Stack(
children: [
Center(
child: AnimatedOpacity(
opacity: widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.activeIcon,
),
),
Center(
child: AnimatedOpacity(
opacity: !widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.inactiveIcon,
),
),
],
),
),
),
),
),
),
],
),
),
),
),
),
);
},
);
}
FontWeight get _activeTextFontWeight =>
widget.activeTextFontWeight ?? FontWeight.w900;
FontWeight get _inactiveTextFontWeight =>
widget.inactiveTextFontWeight ?? FontWeight.w900;
Widget get _activeText {
if (widget.showOnOff) {
return Text(
widget.activeText ?? "On",
style: TextStyle(
color: widget.activeTextColor,
fontWeight: _activeTextFontWeight,
fontSize: widget.valueFontSize,
),
);
}
return Text("");
}
Widget get _inactiveText {
if (widget.showOnOff) {
return Text(
widget.inactiveText ?? "Off",
style: TextStyle(
color: widget.inactiveTextColor,
fontWeight: _inactiveTextFontWeight,
fontSize: widget.valueFontSize,
),
textAlign: TextAlign.right,
);
}
return Text("");
}
}