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

Add support for custom barriers on ModalSheet #124

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions package/lib/src/modal/cupertino.dart
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ class CupertinoModalSheetPage<T> extends Page<T> {
this.transitionDuration = _cupertinoTransitionDuration,
this.transitionCurve = _cupertinoTransitionCurve,
required this.child,
this.barrier,
});

/// The content to be shown in the [Route] created by this page.
Expand All @@ -436,6 +437,8 @@ class CupertinoModalSheetPage<T> extends Page<T> {

final Curve transitionCurve;

final Widget? barrier;

@override
Route<T> createRoute(BuildContext context) {
return _PageBasedCupertinoModalSheetRoute(
Expand Down Expand Up @@ -478,6 +481,9 @@ class _PageBasedCupertinoModalSheetRoute<T>

@override
Widget buildContent(BuildContext context) => _page.child;

@override
Widget? get barrierWidget => _page.barrier;
}

class CupertinoModalSheetRoute<T> extends _BaseCupertinoModalSheetRoute<T> {
Expand All @@ -491,10 +497,13 @@ class CupertinoModalSheetRoute<T> extends _BaseCupertinoModalSheetRoute<T> {
this.barrierColor = _cupertinoBarrierColor,
this.transitionDuration = _cupertinoTransitionDuration,
this.transitionCurve = _cupertinoTransitionCurve,
this.barrier,
});

final WidgetBuilder builder;

final Widget? barrier;

@override
final Color? barrierColor;

Expand All @@ -517,4 +526,7 @@ class CupertinoModalSheetRoute<T> extends _BaseCupertinoModalSheetRoute<T> {
Widget buildContent(BuildContext context) {
return builder(context);
}

@override
Widget? get barrierWidget => barrier;
}
41 changes: 29 additions & 12 deletions package/lib/src/modal/modal_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ModalSheetPage<T> extends Page<T> {
this.barrierColor = Colors.black54,
this.transitionDuration = const Duration(milliseconds: 300),
this.transitionCurve = Curves.fastEaseInToSlowEaseOut,
this.barrier,
required this.child,
});

Expand All @@ -47,6 +48,8 @@ class ModalSheetPage<T> extends Page<T> {

final Curve transitionCurve;

final Widget? barrier;

@override
Route<T> createRoute(BuildContext context) {
return _PageBasedModalSheetRoute(
Expand Down Expand Up @@ -88,6 +91,9 @@ class _PageBasedModalSheetRoute<T> extends PageRoute<T>

@override
Widget buildContent(BuildContext context) => _page.child;

@override
Widget? get barrierWidget => _page.barrier;
}

class ModalSheetRoute<T> extends PageRoute<T> with ModalSheetRouteMixin<T> {
Expand All @@ -101,10 +107,13 @@ class ModalSheetRoute<T> extends PageRoute<T> with ModalSheetRouteMixin<T> {
this.barrierColor = Colors.black54,
this.transitionDuration = const Duration(milliseconds: 300),
this.transitionCurve = Curves.fastEaseInToSlowEaseOut,
this.barrier,
});

final WidgetBuilder builder;

final Widget? barrier;

@override
final Color? barrierColor;

Expand All @@ -127,6 +136,9 @@ class ModalSheetRoute<T> extends PageRoute<T> with ModalSheetRouteMixin<T> {
Widget buildContent(BuildContext context) {
return builder(context);
}

@override
Widget? get barrierWidget => barrier;
}

mixin ModalSheetRouteMixin<T> on ModalRoute<T> {
Expand Down Expand Up @@ -155,6 +167,8 @@ mixin ModalSheetRouteMixin<T> on ModalRoute<T> {

Widget buildContent(BuildContext context);

Widget? get barrierWidget;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a builder rather than a widget because the barrier is lazily built in buildModalBarrier. The signature could be something like ModalSheetBarrierBuilder? get barrierBuilder where ModalSheetBarrierBuilder is:

typedef ModalSheetBarrierBuilder = Widget Function(PageRoute route, VoidCallback onDismissCallback);

The route parameter is necessary to get barrierColor, barrierDismissible, etc...


@override
Widget buildPage(
BuildContext context,
Expand Down Expand Up @@ -199,18 +213,21 @@ mixin ModalSheetRouteMixin<T> on ModalRoute<T> {
final barrierColor = this.barrierColor;
if (barrierColor != null && barrierColor.alpha != 0 && !offstage) {
assert(barrierColor != barrierColor.withOpacity(0.0));
return AnimatedModalBarrier(
onDismiss: onDismiss,
dismissible: barrierDismissible,
semanticsLabel: barrierLabel,
barrierSemanticsDismissible: semanticsDismissible,
color: animation!.drive(
ColorTween(
begin: barrierColor.withOpacity(0.0),
end: barrierColor,
).chain(CurveTween(curve: barrierCurve)),
),
);

return barrierWidget != null
? GestureDetector(onTap: onDismiss, child: barrierWidget)
: AnimatedModalBarrier(
onDismiss: onDismiss,
dismissible: barrierDismissible,
semanticsLabel: barrierLabel,
barrierSemanticsDismissible: semanticsDismissible,
color: animation!.drive(
ColorTween(
begin: barrierColor.withOpacity(0.0),
end: barrierColor,
).chain(CurveTween(curve: barrierCurve)),
),
);
} else {
return ModalBarrier(
onDismiss: onDismiss,
Expand Down