From a3386e210ca5d9e640cc713e986ace828e92b2c6 Mon Sep 17 00:00:00 2001 From: Sujeet Date: Thu, 5 Mar 2020 13:35:56 +0530 Subject: [PATCH 1/2] the onChanged() function return true issue fixed and fixed some constructor field issue. --- lib/custom_switch.dart | 44 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/custom_switch.dart b/lib/custom_switch.dart index c9cf4bf..c54414f 100644 --- a/lib/custom_switch.dart +++ b/lib/custom_switch.dart @@ -3,25 +3,23 @@ library custom_switch; import 'package:flutter/material.dart'; class CustomSwitch extends StatefulWidget { - final bool value; final ValueChanged onChanged; final Color activeColor; - final Color inactiveColor = Colors.grey; - final String activeText = 'On'; - final String inactiveText = 'Off'; - final Color activeTextColor = Colors.white70; - final Color inactiveTextColor = Colors.white70; + final Color inactiveColor; + final String activeText; + final String inactiveText; + final Color activeTextColor; + final Color inactiveTextColor; - const CustomSwitch({ - Key key, - this.value, - this.onChanged, - this.activeColor, - this.inactiveColor, - this.activeText, - this.inactiveText, - this.activeTextColor, - this.inactiveTextColor}) + const CustomSwitch( + {Key key, + this.onChanged, + this.activeColor, + this.inactiveColor = Colors.grey, + this.activeText = 'On', + this.inactiveText = 'Off', + this.activeTextColor = Colors.white70, + this.inactiveTextColor = Colors.white70}) : super(key: key); @override @@ -32,15 +30,15 @@ class _CustomSwitchState extends State with SingleTickerProviderStateMixin { Animation _circleAnimation; AnimationController _animationController; - + bool isChecked = false; @override void initState() { super.initState(); _animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 60)); _circleAnimation = AlignmentTween( - begin: widget.value ? Alignment.centerRight : Alignment.centerLeft, - end: widget.value ? Alignment.centerLeft : Alignment.centerRight) + begin: isChecked ? Alignment.centerRight : Alignment.centerLeft, + end: isChecked ? Alignment.centerLeft : Alignment.centerRight) .animate(CurvedAnimation( parent: _animationController, curve: Curves.linear)); } @@ -57,9 +55,11 @@ class _CustomSwitchState extends State } else { _animationController.forward(); } - widget.value == false - ? widget.onChanged(true) - : widget.onChanged(false); + if (isChecked == false) { + widget.onChanged(isChecked); + } else { + widget.onChanged(isChecked); + } }, child: Container( width: 70.0, From a34013f21850a07484e04b5cb5b09a6ccdbb1903 Mon Sep 17 00:00:00 2001 From: Sujeet Date: Thu, 5 Mar 2020 13:38:24 +0530 Subject: [PATCH 2/2] little changes in example --- example/lib/main.dart | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 07c259a..611c583 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -8,22 +8,18 @@ class MyApp extends StatelessWidget { Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, - theme: ThemeData( - primarySwatch: Colors.deepOrange - ), + theme: ThemeData(primarySwatch: Colors.deepOrange), home: HomeScreen(), ); } } - class HomeScreen extends StatefulWidget { @override _HomeScreenState createState() => _HomeScreenState(); } class _HomeScreenState extends State { - bool status = false; @override @@ -38,7 +34,6 @@ class _HomeScreenState extends State { children: [ CustomSwitch( activeColor: Colors.pinkAccent, - value: status, onChanged: (value) { print("VALUE : $value"); setState(() { @@ -46,11 +41,13 @@ class _HomeScreenState extends State { }); }, ), - SizedBox(height: 12.0,), - Text('Value : $status', style: TextStyle( - color: Colors.black, - fontSize: 20.0 - ),) + SizedBox( + height: 12.0, + ), + Text( + 'Value : $status', + style: TextStyle(color: Colors.black, fontSize: 20.0), + ) ], ), ),