1- import 'package:cosmos_ui_components/cosmos_theme.dart' ;
1+ import 'package:cosmos_ui_components/cosmos_ui_components.dart' ;
2+ import 'package:cosmos_ui_components/utils/global_constants.dart' ;
23import 'package:flutter/cupertino.dart' ;
34import 'package:flutter/foundation.dart' ;
45import 'package:flutter/material.dart' ;
56
67class CosmosSwitch extends StatelessWidget {
78 const CosmosSwitch ({
89 required this .checked,
10+ this .toggleAsset,
11+ this .checkedGradientStops = const [0 , 0.5866 , 1 ],
12+ this .gradientColors = GlobalConstants .cosmosGradientColors,
13+ this .package,
914 this .onChanged,
1015 Key ? key,
1116 }) : super (key: key);
1217
1318 final bool checked;
1419 final ValueChanged <bool >? onChanged;
20+ final String ? toggleAsset;
21+ final List <double > checkedGradientStops;
22+ final List <Color > gradientColors;
23+ final String ? package;
1524
1625 @override
1726 Widget build (BuildContext context) {
18- final theme = CosmosTheme .of (context);
19- return Transform .scale (
20- scale: 0.9 , // so that the border appears closer to the switch
21- child: Container (
22- decoration: BoxDecoration (
23- borderRadius: BorderRadius .circular (1000 ),
24- border: Border .all (
25- color: checked ? Colors .transparent : theme.colors.text.withOpacity (0.1 ),
26- ),
27- ),
27+ return TweenAnimationBuilder <Alignment >(
28+ duration: const Duration (milliseconds: 200 ),
29+ tween: AlignmentTween (
30+ begin: checked ? Alignment .centerLeft : Alignment .centerRight,
31+ end: checked ? Alignment .centerRight : Alignment .centerLeft,
32+ ),
33+ builder: (context, value, child) => GestureDetector (
34+ onTap: () => onChanged !(! checked),
2835 child: Transform .scale (
29- scale: 1.1 , // so that the border appears closer to the switch
30- child: CupertinoSwitch (
31- value: checked,
32- onChanged: onChanged,
33- activeColor: theme.colors.link,
34- thumbColor: checked ? theme.colors.background : theme.colors.text,
35- trackColor: theme.colors.background,
36+ scale: 0.9 ,
37+ child: Container (
38+ width: 50 ,
39+ height: 28 ,
40+ decoration: BoxDecoration (
41+ borderRadius: BorderRadius .circular (30 ),
42+ gradient: checked
43+ ? LinearGradient (
44+ stops: checkedGradientStops,
45+ colors: gradientColors,
46+ )
47+ : null ,
48+ color: checked ? null : CosmosTheme .of (context).colors.text,
49+ ),
50+ child: Transform .scale (
51+ scale: 1.1 ,
52+ child: Align (
53+ alignment: value,
54+ child: SizedBox (
55+ width: 30 ,
56+ height: 30 ,
57+ child: Image .asset (
58+ toggleAsset ?? 'assets/images/toggle.png' ,
59+ package: package ?? (toggleAsset == null ? packageName : null ),
60+ ),
61+ ),
62+ ),
63+ ),
3664 ),
3765 ),
3866 ),
@@ -43,7 +71,11 @@ class CosmosSwitch extends StatelessWidget {
4371 void debugFillProperties (DiagnosticPropertiesBuilder properties) {
4472 super .debugFillProperties (properties);
4573 properties
74+ ..add (DiagnosticsProperty <bool >('checked' , checked))
4675 ..add (ObjectFlagProperty <ValueChanged <bool >?>.has ('onChanged' , onChanged))
47- ..add (DiagnosticsProperty <bool >('checked' , checked));
76+ ..add (StringProperty ('toggleAsset' , toggleAsset))
77+ ..add (IterableProperty <double >('checkedGradientStops' , checkedGradientStops))
78+ ..add (IterableProperty <Color >('gradientColors' , gradientColors))
79+ ..add (StringProperty ('package' , package));
4880 }
4981}
0 commit comments