Skip to content

Commit c1959da

Browse files
committed
added disabled parameter
1 parent 043aecf commit c1959da

1 file changed

Lines changed: 72 additions & 61 deletions

File tree

lib/flutter_switch.dart

Lines changed: 72 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class FlutterSwitch extends StatefulWidget {
4242
this.activeIcon,
4343
this.inactiveIcon,
4444
this.duration = const Duration(milliseconds: 200),
45+
this.disabled = false,
4546
}) : assert(
4647
(switchBorder == null || activeSwitchBorder == null) &&
4748
(switchBorder == null || inactiveSwitchBorder == null),
@@ -243,6 +244,11 @@ class FlutterSwitch extends StatefulWidget {
243244
/// Defaults to the value of 200 milliseconds.
244245
final Duration duration;
245246

247+
/// Determines whether the switch is disabled.
248+
///
249+
/// Defaults to the value of false.
250+
final bool disabled;
251+
246252
@override
247253
_FlutterSwitchState createState() => _FlutterSwitchState();
248254
}
@@ -320,82 +326,87 @@ class _FlutterSwitchState extends State<FlutterSwitch>
320326
return Align(
321327
child: GestureDetector(
322328
onTap: () {
323-
if (widget.value)
324-
_animationController.forward();
325-
else
326-
_animationController.reverse();
327-
328-
widget.onToggle(!widget.value);
329+
if (!widget.disabled) {
330+
if (widget.value)
331+
_animationController.forward();
332+
else
333+
_animationController.reverse();
334+
335+
widget.onToggle(!widget.value);
336+
}
329337
},
330-
child: Container(
331-
width: widget.width,
332-
height: widget.height,
333-
padding: EdgeInsets.all(widget.padding),
334-
decoration: BoxDecoration(
335-
borderRadius: BorderRadius.circular(widget.borderRadius),
336-
color: _switchColor,
337-
border: _switchBorder,
338-
),
339-
child: Stack(
340-
children: <Widget>[
341-
AnimatedOpacity(
342-
opacity: widget.value ? 1.0 : 0.0,
343-
duration: widget.duration,
344-
child: Container(
345-
width: _textSpace,
346-
padding: EdgeInsets.symmetric(horizontal: 4.0),
347-
alignment: Alignment.centerLeft,
348-
child: _activeText,
349-
),
350-
),
351-
Align(
352-
alignment: Alignment.centerRight,
353-
child: AnimatedOpacity(
354-
opacity: !widget.value ? 1.0 : 0.0,
338+
child: Opacity(
339+
opacity: widget.disabled ? 0.6 : 1,
340+
child: Container(
341+
width: widget.width,
342+
height: widget.height,
343+
padding: EdgeInsets.all(widget.padding),
344+
decoration: BoxDecoration(
345+
borderRadius: BorderRadius.circular(widget.borderRadius),
346+
color: _switchColor,
347+
border: _switchBorder,
348+
),
349+
child: Stack(
350+
children: <Widget>[
351+
AnimatedOpacity(
352+
opacity: widget.value ? 1.0 : 0.0,
355353
duration: widget.duration,
356354
child: Container(
357355
width: _textSpace,
358356
padding: EdgeInsets.symmetric(horizontal: 4.0),
359-
alignment: Alignment.centerRight,
360-
child: _inactiveText,
357+
alignment: Alignment.centerLeft,
358+
child: _activeText,
361359
),
362360
),
363-
),
364-
Container(
365-
child: Align(
366-
alignment: _toggleAnimation.value,
367-
child: Container(
368-
width: widget.toggleSize,
369-
height: widget.toggleSize,
370-
decoration: BoxDecoration(
371-
shape: BoxShape.circle,
372-
color: _toggleColor,
373-
border: _toggleBorder,
361+
Align(
362+
alignment: Alignment.centerRight,
363+
child: AnimatedOpacity(
364+
opacity: !widget.value ? 1.0 : 0.0,
365+
duration: widget.duration,
366+
child: Container(
367+
width: _textSpace,
368+
padding: EdgeInsets.symmetric(horizontal: 4.0),
369+
alignment: Alignment.centerRight,
370+
child: _inactiveText,
374371
),
372+
),
373+
),
374+
Container(
375+
child: Align(
376+
alignment: _toggleAnimation.value,
375377
child: Container(
376-
child: Stack(
377-
children: [
378-
Center(
379-
child: AnimatedOpacity(
380-
opacity: widget.value ? 1.0 : 0.0,
381-
duration: widget.duration,
382-
child: widget.activeIcon,
378+
width: widget.toggleSize,
379+
height: widget.toggleSize,
380+
decoration: BoxDecoration(
381+
shape: BoxShape.circle,
382+
color: _toggleColor,
383+
border: _toggleBorder,
384+
),
385+
child: Container(
386+
child: Stack(
387+
children: [
388+
Center(
389+
child: AnimatedOpacity(
390+
opacity: widget.value ? 1.0 : 0.0,
391+
duration: widget.duration,
392+
child: widget.activeIcon,
393+
),
383394
),
384-
),
385-
Center(
386-
child: AnimatedOpacity(
387-
opacity: !widget.value ? 1.0 : 0.0,
388-
duration: widget.duration,
389-
child: widget.inactiveIcon,
395+
Center(
396+
child: AnimatedOpacity(
397+
opacity: !widget.value ? 1.0 : 0.0,
398+
duration: widget.duration,
399+
child: widget.inactiveIcon,
400+
),
390401
),
391-
),
392-
],
402+
],
403+
),
393404
),
394405
),
395406
),
396407
),
397-
),
398-
],
408+
],
409+
),
399410
),
400411
),
401412
),

0 commit comments

Comments
 (0)