Skip to content

Commit

Permalink
feat: [MDS-447] Create Toast component (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kypsis authored Apr 10, 2023
1 parent 3b558c9 commit 191fc13
Show file tree
Hide file tree
Showing 11 changed files with 648 additions and 10 deletions.
99 changes: 99 additions & 0 deletions example/lib/src/storybook/stories/toast.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import 'package:example/src/storybook/common/color_options.dart';
import 'package:flutter/material.dart';
import 'package:moon_design/moon_design.dart';
import 'package:storybook_flutter/storybook_flutter.dart';

class ToastStory extends Story {
ToastStory()
: super(
name: "Moon",
builder: (context) {
final customLabelTextKnob = context.knobs.text(
label: "Custom label text",
initial: "This is a custom MoonToast text",
);

final toastPositionKnob = context.knobs.options(
label: "MoonToastPosition",
description: "The position of the MoonToast.",
initial: MoonToastPosition.bottom,
options: const [
Option(label: "top", value: MoonToastPosition.top),
Option(label: "bottom", value: MoonToastPosition.bottom),
],
);

final toastVariantKnob = context.knobs.options(
label: "MoonToastVariant",
description: "The color variant of the MoonToast.",
initial: MoonToastVariant.original,
options: const [
Option(label: "original", value: MoonToastVariant.original),
Option(label: "inverted", value: MoonToastVariant.inverted),
],
);

final backgroundColorsKnob = context.knobs.options(
label: "backgroundColor",
description: "MoonColors variants for MoonToast background.",
initial: 40, // null
options: colorOptions,
);

final backgroundColor = colorTable(context)[backgroundColorsKnob];

final displayDurationKnob = context.knobs.sliderInt(
min: 1,
max: 10,
initial: 5,
label: "displayDuration",
description: "The duration to show the MoonToast.",
);

final borderRadiusKnob = context.knobs.sliderInt(
max: 20,
initial: 8,
label: "borderRadius",
description: "Border radius for MoonToast.",
);

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),
Builder(
builder: (context) {
return MoonFilledButton(
label: const Text("Tap me"),
onTap: () {
MoonToast().show(
context,
position: toastPositionKnob,
variant: toastVariantKnob,
backgroundColor: backgroundColor,
displayDuration: Duration(seconds: displayDurationKnob),
borderRadius: BorderRadius.circular(borderRadiusKnob.toDouble()),
leading: const Icon(MoonIcons.info_24),
title: Text(customLabelTextKnob),
trailing: const Icon(MoonIcons.star_24),
);
},
);
},
),
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 @@ -15,6 +15,7 @@ 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/toast.dart';
import 'package:example/src/storybook/stories/tooltip.dart';
import 'package:flutter/material.dart';
import 'package:moon_design/moon_design.dart';
Expand Down Expand Up @@ -86,6 +87,7 @@ class StorybookPage extends StatelessWidget {
RadioStory(),
SwitchStory(),
TagStory(),
ToastStory(),
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 @@ -21,6 +21,7 @@ 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/toast/toast_theme.dart';
export 'package:moon_design/src/theme/tooltip/tooltip_theme.dart';
export 'package:moon_design/src/theme/typography/text_colors.dart';
export 'package:moon_design/src/theme/typography/text_styles.dart';
Expand Down Expand Up @@ -58,4 +59,5 @@ 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/toast/toast.dart';
export 'package:moon_design/src/widgets/tooltip/tooltip.dart';
13 changes: 12 additions & 1 deletion lib/src/theme/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:moon_design/src/theme/shadows.dart';
import 'package:moon_design/src/theme/sizes.dart';
import 'package:moon_design/src/theme/switch/switch_theme.dart';
import 'package:moon_design/src/theme/tag/tag_theme.dart';
import 'package:moon_design/src/theme/toast/toast_theme.dart';
import 'package:moon_design/src/theme/tooltip/tooltip_theme.dart';
import 'package:moon_design/src/theme/typography/typography.dart';

Expand Down Expand Up @@ -49,6 +50,7 @@ class MoonTheme extends ThemeExtension<MoonTheme> with DiagnosticableTreeMixin {
sizes: MoonSizes.sizes,
switchTheme: MoonSwitchTheme.light,
tagTheme: MoonTagTheme.light,
toastTheme: MoonToastTheme.light,
tooltipTheme: MoonTooltipTheme.light,
typography: MoonTypography.light,
);
Expand All @@ -75,6 +77,7 @@ class MoonTheme extends ThemeExtension<MoonTheme> with DiagnosticableTreeMixin {
sizes: MoonSizes.sizes,
switchTheme: MoonSwitchTheme.dark,
tagTheme: MoonTagTheme.dark,
toastTheme: MoonToastTheme.dark,
tooltipTheme: MoonTooltipTheme.dark,
typography: MoonTypography.dark,
);
Expand Down Expand Up @@ -127,7 +130,7 @@ class MoonTheme extends ThemeExtension<MoonTheme> with DiagnosticableTreeMixin {
/// Moon Design System MoonPopover widget theming.
final MoonPopoverTheme popoverTheme;

/// Moon Design System Radio widget theming.
/// Moon Design System MoonRadio widget theming.
final MoonRadioTheme radioTheme;

/// Moon Design System shadows.
Expand All @@ -142,6 +145,9 @@ class MoonTheme extends ThemeExtension<MoonTheme> with DiagnosticableTreeMixin {
/// Moon Design System MoonTag widget theming.
final MoonTagTheme tagTheme;

/// Moon Design System MoonToast widget theming.
final MoonToastTheme toastTheme;

/// Moon Design System MoonTooltip widget theming.
final MoonTooltipTheme tooltipTheme;

Expand Down Expand Up @@ -170,6 +176,7 @@ class MoonTheme extends ThemeExtension<MoonTheme> with DiagnosticableTreeMixin {
required this.sizes,
required this.switchTheme,
required this.tagTheme,
required this.toastTheme,
required this.tooltipTheme,
required this.typography,
});
Expand Down Expand Up @@ -197,6 +204,7 @@ class MoonTheme extends ThemeExtension<MoonTheme> with DiagnosticableTreeMixin {
MoonSizes? sizes,
MoonSwitchTheme? switchTheme,
MoonTagTheme? tagTheme,
MoonToastTheme? toastTheme,
MoonTooltipTheme? tooltipTheme,
MoonTypography? typography,
}) {
Expand All @@ -222,6 +230,7 @@ class MoonTheme extends ThemeExtension<MoonTheme> with DiagnosticableTreeMixin {
sizes: sizes ?? this.sizes,
switchTheme: switchTheme ?? this.switchTheme,
tagTheme: tagTheme ?? this.tagTheme,
toastTheme: toastTheme ?? this.toastTheme,
tooltipTheme: tooltipTheme ?? this.tooltipTheme,
typography: typography ?? this.typography,
);
Expand Down Expand Up @@ -253,6 +262,7 @@ class MoonTheme extends ThemeExtension<MoonTheme> with DiagnosticableTreeMixin {
sizes: sizes.lerp(other.sizes, t),
switchTheme: switchTheme.lerp(other.switchTheme, t),
tagTheme: tagTheme.lerp(other.tagTheme, t),
toastTheme: toastTheme.lerp(other.toastTheme, t),
tooltipTheme: tooltipTheme.lerp(other.tooltipTheme, t),
typography: typography.lerp(other.typography, t),
);
Expand Down Expand Up @@ -284,6 +294,7 @@ class MoonTheme extends ThemeExtension<MoonTheme> with DiagnosticableTreeMixin {
..add(DiagnosticsProperty<MoonSizes>("MoonSizes", sizes))
..add(DiagnosticsProperty<MoonSwitchTheme>("MoonSwitchTheme", switchTheme))
..add(DiagnosticsProperty<MoonTagTheme>("MoonTagTheme", tagTheme))
..add(DiagnosticsProperty<MoonToastTheme>("MoonToastTheme", toastTheme))
..add(DiagnosticsProperty<MoonTooltipTheme>("MoonTooltipTheme", tooltipTheme))
..add(DiagnosticsProperty<MoonTypography>("MoonTypography", typography));
}
Expand Down
58 changes: 58 additions & 0 deletions lib/src/theme/toast/toast_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 MoonToastColors extends ThemeExtension<MoonToastColors> with DiagnosticableTreeMixin {
static final light = MoonToastColors(
lightVariantBackgroundColor: MoonColors.light.gohan,
darkVariantBackgroundColor: MoonColors.dark.gohan,
);

static final dark = MoonToastColors(
lightVariantBackgroundColor: MoonColors.dark.gohan,
darkVariantBackgroundColor: MoonColors.light.gohan,
);

/// Toast light variant background color.
final Color lightVariantBackgroundColor;

/// Toast dark variant background color.
final Color darkVariantBackgroundColor;

const MoonToastColors({
required this.lightVariantBackgroundColor,
required this.darkVariantBackgroundColor,
});

@override
MoonToastColors copyWith({
Color? lightVariantBackgroundColor,
Color? darkVariantBackgroundColor,
}) {
return MoonToastColors(
lightVariantBackgroundColor: lightVariantBackgroundColor ?? this.lightVariantBackgroundColor,
darkVariantBackgroundColor: darkVariantBackgroundColor ?? this.darkVariantBackgroundColor,
);
}

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

return MoonToastColors(
lightVariantBackgroundColor: Color.lerp(lightVariantBackgroundColor, other.lightVariantBackgroundColor, t)!,
darkVariantBackgroundColor: Color.lerp(darkVariantBackgroundColor, other.darkVariantBackgroundColor, t)!,
);
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty("type", "MoonToastColors"))
..add(ColorProperty("lightVariantBackgroundColor", lightVariantBackgroundColor))
..add(ColorProperty("darkVariantBackgroundColor", darkVariantBackgroundColor));
}
}
93 changes: 93 additions & 0 deletions lib/src/theme/toast/toast_properties.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:moon_design/src/theme/borders.dart';
import 'package:moon_design/src/theme/sizes.dart';

@immutable
class MoonToastProperties extends ThemeExtension<MoonToastProperties> with DiagnosticableTreeMixin {
static final properties = MoonToastProperties(
borderRadius: MoonBorders.borders.surfaceSm,
contentPadding: EdgeInsets.all(MoonSizes.sizes.x2s),
gap: MoonSizes.sizes.x2s,
displayDuration: const Duration(seconds: 5),
transitionDuration: const Duration(milliseconds: 200),
transitionCurve: Curves.easeInOutCubic,
);

/// Toast border radius.
final BorderRadius borderRadius;

/// Padding around toast content.
final EdgeInsets contentPadding;

/// Space between toast children.
final double gap;

/// Toast display duration.
final Duration displayDuration;

/// Toast transition duration.
final Duration transitionDuration;

/// Toast transition curve.
final Curve transitionCurve;

const MoonToastProperties({
required this.borderRadius,
required this.contentPadding,
required this.gap,
required this.displayDuration,
required this.transitionDuration,
required this.transitionCurve,
});

@override
MoonToastProperties copyWith({
BorderRadius? borderRadius,
EdgeInsets? contentPadding,
double? gap,
Duration? displayDuration,
Duration? transitionDuration,
Curve? transitionCurve,
}) {
return MoonToastProperties(
borderRadius: borderRadius ?? this.borderRadius,
contentPadding: contentPadding ?? this.contentPadding,
gap: gap ?? this.gap,
displayDuration: displayDuration ?? this.displayDuration,
transitionDuration: transitionDuration ?? this.transitionDuration,
transitionCurve: transitionCurve ?? this.transitionCurve,
);
}

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

return MoonToastProperties(
borderRadius: BorderRadius.lerp(borderRadius, other.borderRadius, t)!,
contentPadding: EdgeInsets.lerp(contentPadding, other.contentPadding, t)!,
gap: lerpDouble(gap, other.gap, t)!,
displayDuration: lerpDuration(displayDuration, other.displayDuration, t),
transitionDuration: lerpDuration(transitionDuration, other.transitionDuration, t),
transitionCurve: other.transitionCurve,
);
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty("type", "MoonToastProperties"))
..add(DiagnosticsProperty<BorderRadius>("borderRadius", borderRadius))
..add(DiagnosticsProperty<EdgeInsets>("contentPadding", contentPadding))
..add(DoubleProperty("gap", gap))
..add(DiagnosticsProperty<Duration>("displayDuration", displayDuration))
..add(DiagnosticsProperty<Duration>("transitionDuration", transitionDuration))
..add(DiagnosticsProperty<Curve>("transitionCurve", transitionCurve));
}
}
Loading

0 comments on commit 191fc13

Please sign in to comment.