Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [MDS-1331] Dispose animation controllers for tooltip and popover #441

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ linter:
rules:
depend_on_referenced_packages: false
use_setters_to_change_properties: false
avoid_classes_with_only_static_members: false
10 changes: 7 additions & 3 deletions example/lib/src/storybook/stories/primitives/modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class ModalStory extends StatelessWidget {
textDirection: Directionality.of(context),
child: MoonModal(
backgroundColor: backgroundColor,
borderRadius: borderRadiusKnob != null ? BorderRadius.circular(borderRadiusKnob.toDouble()) : null,
borderRadius: borderRadiusKnob != null
? BorderRadius.circular(borderRadiusKnob.toDouble())
: null,
child: SizedBox(
width: 300,
child: Column(
Expand All @@ -72,7 +74,8 @@ class ModalStory extends StatelessWidget {
padding: const EdgeInsets.fromLTRB(24, 20, 24, 16),
child: Text(
"MoonModal title",
style: context.moonTypography!.heading.text18.copyWith(color: textColor),
style: context.moonTypography!.heading.text18
.copyWith(color: textColor),
),
),
Divider(
Expand All @@ -83,7 +86,8 @@ class ModalStory extends StatelessWidget {
padding: const EdgeInsets.fromLTRB(24, 16, 24, 16),
child: Text(
"Re-open the MoonModal to view the updated knob value.",
style: context.moonTypography!.body.text14.copyWith(color: textColor),
style: context.moonTypography!.body.text14
.copyWith(color: textColor),
),
),
Padding(
Expand Down
4 changes: 3 additions & 1 deletion example/lib/src/storybook/stories/primitives/popover.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ class _PopoverStoryState extends State<PopoverStory> {
show: show,
borderColor: borderColor ?? Colors.transparent,
backgroundColor: backgroundColor,
borderRadius: borderRadiusKnob != null ? BorderRadius.circular(borderRadiusKnob.toDouble()) : null,
borderRadius: borderRadiusKnob != null
? BorderRadius.circular(borderRadiusKnob.toDouble())
: null,
distanceToTarget: distanceToTargetKnob,
popoverPosition: popoverPositionKnob ?? MoonPopoverPosition.top,
popoverShadows: showShadowKnob == true ? null : [],
Expand Down
20 changes: 15 additions & 5 deletions example/lib/src/storybook/stories/primitives/toast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,26 @@ class ToastStory extends StatelessWidget {

final widthKnob = context.knobs.nullable.slider(
label: "width",
description: "Width for MoonToast. If null, the toast will be as wide as its children.",
description:
"Width for MoonToast. If null, the toast will be as wide as its "
"children.",
enabled: false,
initial: 230,
max: MediaQuery.of(context).size.width,
);

final isPersistentKnob = context.knobs.boolean(
label: "isPersistent",
description: "Whether MoonToast is persistent across screens (will not behave as expected only in Storybook).",
description:
"Whether MoonToast is persistent across screens (will not behave as "
"expected only in Storybook).",
);

final useSafeAreaKnob = context.knobs.boolean(
label: "useSafeArea",
description: "Whether MoonToast respects the SafeArea (takes into account notches and native system bars).",
description:
"Whether MoonToast respects the SafeArea (takes into account notches "
"and native system bars).",
initial: true,
);

Expand All @@ -134,8 +140,12 @@ class ToastStory extends StatelessWidget {
width: widthKnob,
toastAlignment: toastAlignmentKnob ?? Alignment.bottomCenter,
variant: toastVariantKnob ?? MoonToastVariant.original,
displayDuration: displayDurationKnob != null ? Duration(seconds: displayDurationKnob) : null,
borderRadius: borderRadiusKnob != null ? BorderRadius.circular(borderRadiusKnob.toDouble()) : null,
displayDuration: displayDurationKnob != null
? Duration(seconds: displayDurationKnob)
: null,
borderRadius: borderRadiusKnob != null
? BorderRadius.circular(borderRadiusKnob.toDouble())
: null,
leading: Icon(
MoonIcons.generic_info_24_light,
color: iconColor,
Expand Down
7 changes: 5 additions & 2 deletions example/lib/src/storybook/stories/primitives/tooltip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ class _TooltipStoryState extends State<TooltipStory> {
initial: true,
);

final borderRadius = borderRadiusKnob != null ? BorderRadius.circular(borderRadiusKnob.toDouble()) : null;
final borderRadius = borderRadiusKnob != null
? BorderRadius.circular(borderRadiusKnob.toDouble())
: null;

return Center(
child: SingleChildScrollView(
Expand All @@ -149,7 +151,8 @@ class _TooltipStoryState extends State<TooltipStory> {
borderWidth: 1,
borderColor: borderColor ?? Colors.transparent,
borderRadius: borderRadius,
tooltipPosition: tooltipPositionKnob ?? MoonTooltipPosition.top,
tooltipPosition:
tooltipPositionKnob ?? MoonTooltipPosition.top,
hasArrow: showArrowKnob,
arrowBaseWidth: arrowBaseWidthKnob,
arrowLength: arrowLengthKnob,
Expand Down
66 changes: 42 additions & 24 deletions lib/src/widgets/modal/modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import 'package:moon_design/src/utils/shape_decoration_premul.dart';
import 'package:moon_design/src/utils/squircle/squircle_border.dart';
import 'package:moon_tokens/moon_tokens.dart';

/// Displays a modal overlay over the app's current content, incorporating entrance and exit animations,
/// modal barrier color, and modal barrier behavior, enabling dialog dismissal via barrier taps.
/// Intended for use in conjunction with [MoonModal].
/// Displays a modal overlay over the app's current content, incorporating
/// entrance and exit animations, modal barrier color, and modal barrier
/// behavior, enabling dialog dismissal via barrier taps. Intended for use in
/// conjunction with [MoonModal].
Future<T?> showMoonModal<T>({
bool barrierDismissible = true,
bool useRootNavigator = true,
Expand All @@ -33,8 +34,9 @@ Future<T?> showMoonModal<T>({
to: Navigator.of(context, rootNavigator: useRootNavigator).context,
);

final Color effectiveBarrierColor =
barrierColor ?? context.moonTheme?.modalTheme.colors.barrierColor ?? MoonColors.light.zeno;
final Color effectiveBarrierColor = barrierColor ??
context.moonTheme?.modalTheme.colors.barrierColor ??
MoonColors.light.zeno;

final Duration effectiveTransitionDuration = transitionDuration ??
context.moonTheme?.modalTheme.properties.transitionDuration ??
Expand Down Expand Up @@ -65,11 +67,15 @@ bool _debugIsActive(BuildContext context) {
if (context is Element && !context.debugIsActive) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('This BuildContext is no longer valid.'),
ErrorDescription('The showMoonModal function context parameter is a BuildContext that is no longer valid.'),
ErrorDescription(
'The showMoonModal function context parameter is a BuildContext that is '
'no longer valid.',
),
ErrorHint(
'This can commonly occur when the showMoonModal function is called after awaiting a Future. '
'In this situation the BuildContext might refer to a widget that has already been disposed during the await. '
'Consider using a parent context instead.',
'This can commonly occur when the showMoonModal function is called after '
'awaiting a Future. In this situation the BuildContext might refer to a '
'widget that has already been disposed during the await. Consider using '
'a parent context instead.',
),
]);
}
Expand All @@ -78,8 +84,9 @@ bool _debugIsActive(BuildContext context) {
}

class MoonModalRoute<T> extends RawDialogRoute<T> {
/// A Moon Design modal route with entrance and exit animations, modal barrier color, and modal barrier behavior
/// that allows dismissing the modal when tapped on the barrier.
/// A Moon Design modal route with entrance and exit animations, modal barrier
/// color, and modal barrier behavior that allows dismissing the modal when
/// tapped on the barrier.
MoonModalRoute({
super.anchorPoint,
required super.barrierColor,
Expand All @@ -93,15 +100,18 @@ class MoonModalRoute<T> extends RawDialogRoute<T> {
required BuildContext context,
required WidgetBuilder builder,
}) : super(
barrierLabel: barrierLabel ?? MaterialLocalizations.of(context).modalBarrierDismissLabel,
pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
barrierLabel: barrierLabel ??
MaterialLocalizations.of(context).modalBarrierDismissLabel,
pageBuilder: (
BuildContext buildContext,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
final Widget pageChild = Builder(builder: builder);

Widget modal = themes?.wrap(pageChild) ?? pageChild;

if (useSafeArea) {
modal = SafeArea(child: modal);
}
if (useSafeArea) modal = SafeArea(child: modal);

return modal;
},
Expand Down Expand Up @@ -152,18 +162,25 @@ class MoonModal extends StatelessWidget {

@override
Widget build(BuildContext context) {
final BorderRadiusGeometry effectiveBorderRadius =
borderRadius ?? context.moonTheme?.modalTheme.properties.borderRadius ?? MoonBorders.borders.surfaceSm;
final BorderRadiusGeometry effectiveBorderRadius = borderRadius ??
context.moonTheme?.modalTheme.properties.borderRadius ??
MoonBorders.borders.surfaceSm;

final Color effectiveBackgroundColor =
backgroundColor ?? context.moonTheme?.modalTheme.colors.backgroundColor ?? MoonColors.light.goku;
final Color effectiveBackgroundColor = backgroundColor ??
context.moonTheme?.modalTheme.colors.backgroundColor ??
MoonColors.light.goku;

final Color effectiveTextColor = context.moonTheme?.modalTheme.colors.textColor ?? MoonColors.light.textPrimary;
final Color effectiveTextColor =
context.moonTheme?.modalTheme.colors.textColor ??
MoonColors.light.textPrimary;

final Color effectiveIconColor = context.moonTheme?.modalTheme.colors.iconColor ?? MoonColors.light.iconPrimary;
final Color effectiveIconColor =
context.moonTheme?.modalTheme.colors.iconColor ??
MoonColors.light.iconPrimary;

final TextStyle effectiveTextStyle =
context.moonTheme?.modalTheme.properties.textStyle ?? MoonTypography.typography.body.textDefault;
context.moonTheme?.modalTheme.properties.textStyle ??
MoonTypography.typography.body.textDefault;

return Semantics(
label: semanticLabel,
Expand All @@ -177,7 +194,8 @@ class MoonModal extends StatelessWidget {
ShapeDecorationWithPremultipliedAlpha(
color: effectiveBackgroundColor,
shape: MoonSquircleBorder(
borderRadius: effectiveBorderRadius.squircleBorderRadius(context),
borderRadius:
effectiveBorderRadius.squircleBorderRadius(context),
),
),
child: child,
Expand Down
Loading