Skip to content

Commit 318fea0

Browse files
author
Maksim Nikolaev
committed
added duration, curv and more frames to animation
1 parent 7ba5e21 commit 318fea0

1 file changed

Lines changed: 35 additions & 31 deletions

File tree

lib/flutter_switch.dart

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class FlutterSwitch extends StatefulWidget {
4141
this.inactiveToggleBorder,
4242
this.activeIcon,
4343
this.inactiveIcon,
44+
this.duration,
45+
this.curve,
4446
}) : assert(
4547
(toggleColor == null || activeToggleColor == null) &&
4648
(toggleColor == null || inactiveToggleColor == null),
@@ -242,6 +244,15 @@ class FlutterSwitch extends StatefulWidget {
242244
/// This property is optional.
243245
final Icon inactiveIcon;
244246

247+
/// The duration in milliseconds to change the state of the switch
248+
///
249+
final Duration duration;
250+
251+
/// An parametric animation easing curve, i.e. a mapping of the unit interval to the unit interval.
252+
///
253+
/// https://api.flutter.dev/flutter/animation/Curves-class.html
254+
final Curve curve;
255+
245256
@override
246257
_FlutterSwitchState createState() => _FlutterSwitchState();
247258
}
@@ -257,13 +268,16 @@ class _FlutterSwitchState extends State<FlutterSwitch>
257268
_animationController = AnimationController(
258269
vsync: this,
259270
value: widget.value ? 1.0 : 0.0,
260-
duration: Duration(milliseconds: 60),
271+
duration: widget.duration ?? Duration(milliseconds: 60),
261272
);
262273
_toggleAnimation = AlignmentTween(
263274
begin: Alignment.centerLeft,
264275
end: Alignment.centerRight,
265276
).animate(
266-
CurvedAnimation(parent: _animationController, curve: Curves.linear),
277+
CurvedAnimation(
278+
parent: _animationController,
279+
curve: widget.curve ?? Curves.linear,
280+
),
267281
);
268282
}
269283

@@ -329,39 +343,29 @@ class _FlutterSwitchState extends State<FlutterSwitch>
329343
color: _switchColor,
330344
border: _switchBorder,
331345
),
332-
child: Row(
333-
mainAxisAlignment: MainAxisAlignment.spaceBetween,
346+
child: Stack(
334347
children: <Widget>[
335-
_toggleAnimation.value == Alignment.centerRight
336-
? Expanded(
337-
child: Container(
338-
padding: EdgeInsets.symmetric(horizontal: 4.0),
339-
child: _activeText,
340-
),
341-
)
342-
: Container(),
343-
Align(
344-
alignment: _toggleAnimation.value,
345-
child: Container(
346-
width: widget.toggleSize,
347-
height: widget.toggleSize,
348-
decoration: BoxDecoration(
349-
shape: BoxShape.circle,
350-
color: _toggleColor ?? Colors.white,
351-
border: _toggleBorder,
348+
Container(
349+
alignment: Alignment.center,
350+
child: _toggleAnimation.value == Alignment.centerRight
351+
? _activeText
352+
: _inactiveText,
353+
),
354+
Container(
355+
child: Align(
356+
alignment: _toggleAnimation.value,
357+
child: Container(
358+
width: widget.toggleSize,
359+
height: widget.toggleSize,
360+
decoration: BoxDecoration(
361+
shape: BoxShape.circle,
362+
color: _toggleColor ?? Colors.white,
363+
border: _toggleBorder,
364+
),
365+
child: _icon,
352366
),
353-
child: _icon,
354367
),
355368
),
356-
_toggleAnimation.value == Alignment.centerLeft
357-
? Expanded(
358-
child: Container(
359-
padding: EdgeInsets.symmetric(horizontal: 4.0),
360-
alignment: Alignment.centerRight,
361-
child: _inactiveText,
362-
),
363-
)
364-
: Container(),
365369
],
366370
),
367371
),

0 commit comments

Comments
 (0)