Skip to content

Commit

Permalink
feat: [MDS-431] Create Switch widget (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kypsis authored Mar 14, 2023
1 parent 93bad01 commit 434fb73
Show file tree
Hide file tree
Showing 18 changed files with 1,124 additions and 75 deletions.
137 changes: 137 additions & 0 deletions example/lib/src/storybook/stories/switch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import 'package:example/src/storybook/common/options.dart';
import 'package:example/src/storybook/common/widgets/text_divider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:moon_design/moon_design.dart';
import 'package:storybook_flutter/storybook_flutter.dart';

bool value = false;

class SwitchStory extends Story {
SwitchStory()
: super(
name: "Switch",
builder: (context) {
final switchSizesKnob = context.knobs.options(
label: "MoonSwitchSize",
description: "Switch size variants.",
initial: MoonSwitchSize.xs,
options: const [
Option(label: "x2s", value: MoonSwitchSize.x2s),
Option(label: "xs", value: MoonSwitchSize.xs),
Option(label: "sm", value: MoonSwitchSize.sm),
],
);

final thumbColorsKnob = context.knobs.options(
label: "thumbColor",
description: "MoonColors variants for the Switch thumb.",
initial: 7, // goten
options: colorOptions,
);

final thumbColor = colorTable(context)[thumbColorsKnob];

final activeTrackColorsKnob = context.knobs.options(
label: "activeTrackColor",
description: "MoonColors variants for the active Switch track.",
initial: 0, // piccolo
options: colorOptions,
);

final activeTrackColor = colorTable(context)[activeTrackColorsKnob];

final inactiveTrackColorsKnob = context.knobs.options(
label: "inactiveTrackColor",
description: "MoonColors variants for the active Switch track.",
initial: 2, // beerus
options: colorOptions,
);

final inactiveTrackColor = colorTable(context)[inactiveTrackColorsKnob];

final isDisabled = context.knobs.boolean(
label: "Disabled",
description: "onChanged() is null.",
);

final setRtlModeKnob = context.knobs.boolean(
label: "RTL mode",
description: "Switch between LTR and RTL modes.",
);

return Directionality(
textDirection: setRtlModeKnob ? TextDirection.rtl : TextDirection.ltr,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(height: 64),
const TextDivider(text: "Customisable Switch"),
const SizedBox(height: 32),
StatefulBuilder(
builder: (context, setState) {
return MoonSwitch(
switchSize: switchSizesKnob,
thumbColor: thumbColor,
activeTrackColor: activeTrackColor,
inactiveTrackColor: inactiveTrackColor,
value: value,
onChanged: isDisabled ? null : (newValue) => setState(() => value = newValue),
);
},
),
const SizedBox(height: 40),
const TextDivider(text: "Switches with custom children"),
const SizedBox(height: 32),
StatefulBuilder(
builder: (context, setState) {
return MoonSwitch(
activeThumbWidget: const Icon(
MoonIconsGeneric.check_alternative24,
size: 14,
),
inactiveThumbWidget: const Icon(
MoonIconsControls.close24,
size: 12,
),
activeTrackWidget: const Text(
"ON",
style: TextStyle(fontSize: 8),
textAlign: TextAlign.center,
),
inactiveTrackWidget: const Text(
"OFF",
style: TextStyle(fontSize: 8),
textAlign: TextAlign.center,
),
value: value,
onChanged: (newValue) => setState(() => value = newValue),
);
},
),
const SizedBox(height: 32),
StatefulBuilder(
builder: (context, setState) {
return MoonSwitch(
activeTrackWidget: const Icon(
MoonIconsGeneric.check_alternative24,
size: 14,
),
inactiveTrackWidget: const Icon(
MoonIconsControls.close24,
size: 12,
),
value: value,
onChanged: (newValue) => setState(() => value = newValue),
);
},
),
const SizedBox(height: 64),
],
),
),
);
},
);
}
2 changes: 2 additions & 0 deletions example/lib/src/storybook/storybook.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:example/src/storybook/stories/icons.dart';
import 'package:example/src/storybook/stories/linear_loader.dart';
import 'package:example/src/storybook/stories/linear_progress.dart';
import 'package:example/src/storybook/stories/popover.dart';
import 'package:example/src/storybook/stories/switch.dart';
import 'package:example/src/storybook/stories/tag.dart';
import 'package:example/src/storybook/stories/tooltip.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -73,6 +74,7 @@ class StorybookPage extends StatelessWidget {
LinearLoaderStory(),
LinearProgressStory(),
PopoverStory(),
SwitchStory(),
TagStory(),
TooltipStory(),
],
Expand Down
2 changes: 2 additions & 0 deletions lib/moon_design.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export 'package:moon_design/src/theme/progress/circular_progress/circular_progre
export 'package:moon_design/src/theme/progress/linear_progress/linear_progress_theme.dart';
export 'package:moon_design/src/theme/shadows.dart';
export 'package:moon_design/src/theme/sizes.dart';
export 'package:moon_design/src/theme/switch/switch_theme.dart';
export 'package:moon_design/src/theme/theme.dart';
export 'package:moon_design/src/theme/tooltip/tooltip_theme.dart';
export 'package:moon_design/src/theme/typography/text_colors.dart';
Expand Down Expand Up @@ -44,5 +45,6 @@ export 'package:moon_design/src/widgets/loaders/linear_loader.dart';
export 'package:moon_design/src/widgets/popover/popover.dart';
export 'package:moon_design/src/widgets/progress/circular_progress.dart';
export 'package:moon_design/src/widgets/progress/linear_progress.dart';
export 'package:moon_design/src/widgets/switch/switch.dart';
export 'package:moon_design/src/widgets/tag/tag.dart';
export 'package:moon_design/src/widgets/tooltip/tooltip.dart';
2 changes: 1 addition & 1 deletion lib/src/theme/opacity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter/material.dart';

@immutable
class MoonOpacity extends ThemeExtension<MoonOpacity> with DiagnosticableTreeMixin {
static const opacities = MoonOpacity(disabled: 0.68);
static const opacities = MoonOpacity(disabled: 0.32);

/// Disabled opacity value.
final double disabled;
Expand Down
68 changes: 68 additions & 0 deletions lib/src/theme/switch/switch_colors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:moon_design/src/theme/colors.dart';

@immutable
class MoonSwitchColors extends ThemeExtension<MoonSwitchColors> with DiagnosticableTreeMixin {
static final light = MoonSwitchColors(
activeTrackColor: MoonColors.light.piccolo,
inactiveTrackColor: MoonColors.light.beerus,
thumbColor: MoonColors.light.goten,
);

static final dark = MoonSwitchColors(
activeTrackColor: MoonColors.dark.piccolo,
inactiveTrackColor: MoonColors.dark.beerus,
thumbColor: MoonColors.dark.goten,
);

/// Switch active track color.
final Color activeTrackColor;

/// Switch inactive track color.
final Color inactiveTrackColor;

/// Switch thumbcolor.
final Color thumbColor;

const MoonSwitchColors({
required this.activeTrackColor,
required this.inactiveTrackColor,
required this.thumbColor,
});

@override
MoonSwitchColors copyWith({
Color? activeTrackColor,
Color? inactiveTrackColor,
Color? thumbColor,
}) {
return MoonSwitchColors(
activeTrackColor: activeTrackColor ?? this.activeTrackColor,
inactiveTrackColor: inactiveTrackColor ?? this.inactiveTrackColor,
thumbColor: thumbColor ?? this.thumbColor,
);
}

@override
MoonSwitchColors lerp(ThemeExtension<MoonSwitchColors>? other, double t) {
if (other is! MoonSwitchColors) return this;

return MoonSwitchColors(
activeTrackColor: Color.lerp(activeTrackColor, other.activeTrackColor, t)!,
inactiveTrackColor: Color.lerp(inactiveTrackColor, other.inactiveTrackColor, t)!,
thumbColor: Color.lerp(thumbColor, other.thumbColor, t)!,
);
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty("type", "MoonSwitchColors"))
..add(ColorProperty("activeTrackColor", activeTrackColor))
..add(ColorProperty("inactiveTrackColor", inactiveTrackColor))
..add(ColorProperty("thumbColor", thumbColor));
}
}
51 changes: 51 additions & 0 deletions lib/src/theme/switch/switch_properties.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

@immutable
class MoonSwitchProperties extends ThemeExtension<MoonSwitchProperties> with DiagnosticableTreeMixin {
static const properties = MoonSwitchProperties(
transitionDuration: Duration(milliseconds: 200),
transitionCurve: Curves.easeInOutCubic,
);

/// Switch transition duration.
final Duration transitionDuration;

/// Switch transition curve.
final Curve transitionCurve;

const MoonSwitchProperties({
required this.transitionDuration,
required this.transitionCurve,
});

@override
MoonSwitchProperties copyWith({
Duration? transitionDuration,
Curve? transitionCurve,
}) {
return MoonSwitchProperties(
transitionDuration: transitionDuration ?? this.transitionDuration,
transitionCurve: transitionCurve ?? this.transitionCurve,
);
}

@override
MoonSwitchProperties lerp(ThemeExtension<MoonSwitchProperties>? other, double t) {
if (other is! MoonSwitchProperties) return this;

return MoonSwitchProperties(
transitionDuration: lerpDuration(transitionDuration, other.transitionDuration, t),
transitionCurve: other.transitionCurve,
);
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty("type", "MoonSwitchProperties"))
..add(DiagnosticsProperty<Duration>("transitionDuration", transitionDuration))
..add(DiagnosticsProperty<Curve>("transitionCurve", transitionCurve));
}
}
46 changes: 46 additions & 0 deletions lib/src/theme/switch/switch_shadows.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:moon_design/src/theme/shadows.dart';

@immutable
class MoonSwitchShadows extends ThemeExtension<MoonSwitchShadows> with DiagnosticableTreeMixin {
static final light = MoonSwitchShadows(
thumbShadows: MoonShadows.light.sm,
);

static final dark = MoonSwitchShadows(
thumbShadows: MoonShadows.dark.sm,
);

/// Switch thumb shadows.
final List<BoxShadow> thumbShadows;

const MoonSwitchShadows({
required this.thumbShadows,
});

@override
MoonSwitchShadows copyWith({List<BoxShadow>? thumbShadows}) {
return MoonSwitchShadows(
thumbShadows: thumbShadows ?? this.thumbShadows,
);
}

@override
MoonSwitchShadows lerp(ThemeExtension<MoonSwitchShadows>? other, double t) {
if (other is! MoonSwitchShadows) return this;

return MoonSwitchShadows(
thumbShadows: BoxShadow.lerpList(thumbShadows, other.thumbShadows, t)!,
);
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty("type", "MoonSwitchShadows"))
..add(DiagnosticsProperty<List<BoxShadow>>("shadows", thumbShadows));
}
}
Loading

0 comments on commit 434fb73

Please sign in to comment.