Skip to content

Commit

Permalink
feat: [MDS-442] Create Radio widget (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kypsis committed Mar 20, 2023
1 parent 756e0de commit 1c4f674
Show file tree
Hide file tree
Showing 12 changed files with 550 additions and 50 deletions.
24 changes: 17 additions & 7 deletions example/lib/src/storybook/stories/checkbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ class CheckboxStory extends Story {

final activeColor = colorTable(context)[activeColorsKnob];

final fillColorsKnob = context.knobs.options(
label: "fillColor",
final inactiveColorsKnob = context.knobs.options(
label: "inactiveColor",
description: "MoonColors variants for when Checkbox is unchecked.",
initial: 39, // transparent
options: colorOptions,
);

final fillColor = colorTable(context)[fillColorsKnob];
final inactiveColor = colorTable(context)[inactiveColorsKnob];

final borderColorsKnob = context.knobs.options(
label: "borderColor",
description: "MoonColors variants for Checkbox border.",
initial: 6, // trunks
options: colorOptions,
);

final borderColor = colorTable(context)[borderColorsKnob];

final isDisabled = context.knobs.boolean(
label: "Disabled",
Expand All @@ -46,7 +55,7 @@ class CheckboxStory extends Story {

final isTristate = context.knobs.boolean(
label: "tristate",
description: "Whether the checkbox uses tristate.",
description: "Whether the Checkbox uses tristate.",
);

final setRtlModeKnob = context.knobs.boolean(
Expand All @@ -66,9 +75,10 @@ class CheckboxStory extends Story {
StatefulBuilder(
builder: (context, setState) {
return MoonCheckbox(
checkColor: checkColor,
activeColor: activeColor,
fillColor: fillColor,
borderColor: borderColor,
checkColor: checkColor,
inactiveColor: inactiveColor,
tristate: isTristate,
value: value,
onChanged: isDisabled ? null : (newValue) => setState(() => value = newValue),
Expand All @@ -84,7 +94,7 @@ class CheckboxStory extends Story {
context,
checkColor: checkColor,
activeColor: activeColor,
fillColor: fillColor,
inactiveColor: inactiveColor,
tristate: isTristate,
value: value,
onChanged: isDisabled ? null : (newValue) => setState(() => value = newValue),
Expand Down
108 changes: 108 additions & 0 deletions example/lib/src/storybook/stories/radio.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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';

enum ChoiceCustom { first, second }

enum ChoiceLabel { first, second }

ChoiceCustom? valueCustom = ChoiceCustom.first;
ChoiceLabel? valueLabel = ChoiceLabel.first;

class RadioStory extends Story {
RadioStory()
: super(
name: "Radio",
builder: (context) {
final activeColorsKnob = context.knobs.options(
label: "activeColor",
description: "MoonColors variants for when Radio is checked.",
initial: 0, // piccolo
options: colorOptions,
);

final activeColor = colorTable(context)[activeColorsKnob];

final inactiveColorsKnob = context.knobs.options(
label: "inactiveColor",
description: "MoonColors variants for when Radio is unchecked.",
initial: 6, // trunks
options: colorOptions,
);

final inactiveColor = colorTable(context)[inactiveColorsKnob];

final isToggleable = context.knobs.boolean(
label: "toggleable",
description: "Whether the selected Radio can be unselected.",
);

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: StatefulBuilder(
builder: (context, setState) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(height: 64),
const TextDivider(text: "Customisable Radio buttons"),
const SizedBox(height: 32),
MoonRadio(
value: ChoiceCustom.first,
groupValue: valueCustom,
onChanged: isDisabled ? null : (ChoiceCustom? choice) => setState(() => valueCustom = choice),
activeColor: activeColor,
inactiveColor: inactiveColor,
toggleable: isToggleable,
),
const SizedBox(height: 8),
MoonRadio(
value: ChoiceCustom.second,
groupValue: valueCustom,
onChanged: isDisabled ? null : (ChoiceCustom? choice) => setState(() => valueCustom = choice),
activeColor: activeColor,
inactiveColor: inactiveColor,
toggleable: isToggleable,
),
const SizedBox(height: 40),
const TextDivider(text: "Radios with clickable text"),
const SizedBox(height: 32),
MoonRadio.withLabel(
context,
value: ChoiceLabel.first,
groupValue: valueLabel,
label: "With label #1",
onChanged: isDisabled ? null : (ChoiceLabel? choice) => setState(() => valueLabel = choice),
),
const SizedBox(height: 8),
MoonRadio.withLabel(
context,
value: ChoiceLabel.second,
groupValue: valueLabel,
label: "With label #2",
onChanged: isDisabled ? null : (ChoiceLabel? choice) => setState(() => valueLabel = choice),
),
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 @@ -10,6 +10,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/radio.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';
Expand Down Expand Up @@ -78,6 +79,7 @@ class StorybookPage extends StatelessWidget {
LinearLoaderStory(),
LinearProgressStory(),
PopoverStory(),
RadioStory(),
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/opacity.dart';
export 'package:moon_design/src/theme/popover/popover_theme.dart';
export 'package:moon_design/src/theme/progress/circular_progress/circular_progress_theme.dart';
export 'package:moon_design/src/theme/progress/linear_progress/linear_progress_theme.dart';
export 'package:moon_design/src/theme/radio/radio_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';
Expand Down Expand Up @@ -49,6 +50,7 @@ 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/radio/radio.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';
18 changes: 9 additions & 9 deletions lib/src/theme/checkbox/checkbox_colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class MoonCheckboxColors extends ThemeExtension<MoonCheckboxColors> with Diagnos
static final light = MoonCheckboxColors(
activeColor: MoonColors.light.piccolo,
checkColor: MoonColors.light.goten,
fillColor: Colors.transparent,
inactiveColor: Colors.transparent,
borderColor: MoonColors.light.trunks,
);

static final dark = MoonCheckboxColors(
activeColor: MoonColors.dark.piccolo,
checkColor: MoonColors.dark.goten,
fillColor: Colors.transparent,
inactiveColor: Colors.transparent,
borderColor: MoonColors.dark.trunks,
);

Expand All @@ -28,28 +28,28 @@ class MoonCheckboxColors extends ThemeExtension<MoonCheckboxColors> with Diagnos
/// Checkbox icon color.
final Color checkColor;

/// Checkbox fill (inactive) color.
final Color fillColor;
/// Checkbox inactive color.
final Color inactiveColor;

const MoonCheckboxColors({
required this.activeColor,
required this.borderColor,
required this.checkColor,
required this.fillColor,
required this.inactiveColor,
});

@override
MoonCheckboxColors copyWith({
Color? activeColor,
Color? borderColor,
Color? checkColor,
Color? fillColor,
Color? inactiveColor,
}) {
return MoonCheckboxColors(
activeColor: activeColor ?? this.activeColor,
borderColor: borderColor ?? this.borderColor,
checkColor: checkColor ?? this.checkColor,
fillColor: fillColor ?? this.fillColor,
inactiveColor: inactiveColor ?? this.inactiveColor,
);
}

Expand All @@ -61,7 +61,7 @@ class MoonCheckboxColors extends ThemeExtension<MoonCheckboxColors> with Diagnos
activeColor: Color.lerp(activeColor, other.activeColor, t)!,
borderColor: Color.lerp(borderColor, other.borderColor, t)!,
checkColor: Color.lerp(checkColor, other.checkColor, t)!,
fillColor: Color.lerp(fillColor, other.fillColor, t)!,
inactiveColor: Color.lerp(inactiveColor, other.inactiveColor, t)!,
);
}

Expand All @@ -73,6 +73,6 @@ class MoonCheckboxColors extends ThemeExtension<MoonCheckboxColors> with Diagnos
..add(ColorProperty("activeColor", activeColor))
..add(ColorProperty("borderColor", borderColor))
..add(ColorProperty("checkColor", checkColor))
..add(ColorProperty("fillColor", fillColor));
..add(ColorProperty("inactiveColor", inactiveColor));
}
}
58 changes: 58 additions & 0 deletions lib/src/theme/radio/radio_colors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

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

@immutable
class MoonRadioColors extends ThemeExtension<MoonRadioColors> with DiagnosticableTreeMixin {
static final light = MoonRadioColors(
activeColor: MoonColors.light.piccolo,
inactiveColor: MoonColors.light.trunks,
);

static final dark = MoonRadioColors(
activeColor: MoonColors.dark.piccolo,
inactiveColor: MoonColors.dark.trunks,
);

/// Radio active color.
final Color activeColor;

/// Radio inactive color.
final Color inactiveColor;

const MoonRadioColors({
required this.activeColor,
required this.inactiveColor,
});

@override
MoonRadioColors copyWith({
Color? activeColor,
Color? inactiveColor,
}) {
return MoonRadioColors(
activeColor: activeColor ?? this.activeColor,
inactiveColor: inactiveColor ?? this.inactiveColor,
);
}

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

return MoonRadioColors(
activeColor: Color.lerp(activeColor, other.activeColor, t)!,
inactiveColor: Color.lerp(inactiveColor, other.inactiveColor, t)!,
);
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty("type", "MoonRadioColors"))
..add(ColorProperty("activeColor", activeColor))
..add(ColorProperty("inactiveColor", inactiveColor));
}
}
48 changes: 48 additions & 0 deletions lib/src/theme/radio/radio_theme.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:moon_design/src/theme/radio/radio_colors.dart';

@immutable
class MoonRadioTheme extends ThemeExtension<MoonRadioTheme> with DiagnosticableTreeMixin {
static final light = MoonRadioTheme(
colors: MoonRadioColors.light,
);

static final dark = MoonRadioTheme(
colors: MoonRadioColors.dark,
);

/// Radio colors.
final MoonRadioColors colors;

const MoonRadioTheme({
required this.colors,
});

@override
MoonRadioTheme copyWith({
MoonRadioColors? colors,
}) {
return MoonRadioTheme(
colors: colors ?? this.colors,
);
}

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

return MoonRadioTheme(
colors: colors.lerp(other.colors, t),
);
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder diagnosticProperties) {
super.debugFillProperties(diagnosticProperties);
diagnosticProperties
..add(DiagnosticsProperty("type", "MoonRadioTheme"))
..add(DiagnosticsProperty<MoonRadioColors>("colors", colors));
}
}
Loading

0 comments on commit 1c4f674

Please sign in to comment.