Skip to content

Commit 5c8a494

Browse files
Merge pull request #7 from boringdeveloper/0.1.x
reacting with state
2 parents 465d01c + 2aca269 commit 5c8a494

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

lib/flutter_switch.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,35 @@ class _FlutterSwitchState extends State<FlutterSwitch>
5656
CurvedAnimation(parent: _animationController, curve: Curves.linear),
5757
);
5858
}
59-
59+
6060
@override
6161
void dispose() {
6262
_animationController.dispose();
6363
super.dispose();
6464
}
6565

66+
@override
67+
void didUpdateWidget(FlutterSwitch oldWidget) {
68+
super.didUpdateWidget(oldWidget);
69+
if (widget.value)
70+
_animationController.forward();
71+
else
72+
_animationController.reverse();
73+
}
74+
6675
@override
6776
Widget build(BuildContext context) {
6877
return AnimatedBuilder(
6978
animation: _animationController,
7079
builder: (context, child) {
7180
return GestureDetector(
7281
onTap: () {
73-
if (_animationController.isCompleted) {
82+
if (widget.value)
7483
_animationController.reverse();
75-
} else {
84+
else
7685
_animationController.forward();
77-
}
78-
widget.value == false
79-
? widget.onToggle(true)
80-
: widget.onToggle(false);
86+
87+
widget.onToggle(!widget.value);
8188
},
8289
child: Container(
8390
width: widget.width,

0 commit comments

Comments
 (0)