Skip to content

Commit 3cec936

Browse files
authored
refactor: Update custom switch to have Cosmos specific gradient (#276)
1 parent e88f874 commit 3cec936

File tree

6 files changed

+60
-20
lines changed

6 files changed

+60
-20
lines changed
4.85 KB
Loading
9.42 KB
Loading
2.11 KB
Loading

packages/cosmos_ui_components/lib/components/cosmos_qr_image.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CosmosQrImage extends StatelessWidget {
1919
Widget build(BuildContext context) {
2020
final theme = CosmosTheme.of(context);
2121
return ClipRRect(
22-
borderRadius: borderRadius ?? BorderRadius.circular(theme.radiusL),
22+
borderRadius: borderRadius ?? theme.borderRadiusL,
2323
child: QrImage(
2424
padding: padding,
2525
data: data,
Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,66 @@
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';
23
import 'package:flutter/cupertino.dart';
34
import 'package:flutter/foundation.dart';
45
import 'package:flutter/material.dart';
56

67
class 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
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import 'package:flutter/material.dart';
2+
13
abstract class GlobalConstants {
24
static const double defaultButtonHeight = 50;
5+
6+
static const List<Color> cosmosGradientColors = [
7+
Color(0xFF64DBFC),
8+
Color(0xFF30FFDF),
9+
Color(0xFFFFFE39),
10+
];
311
}

0 commit comments

Comments
 (0)