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 onTap and closeOnTap methods #105

Merged
merged 1 commit into from
Feb 21, 2024
Merged
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
145 changes: 93 additions & 52 deletions lib/elegant_notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class ElegantNotification extends StatefulWidget {
this.progressBarPadding,
this.onDismiss,
this.progressIndicatorBackground = greyColor,
this.onTap,
this.closeOnTap = false,
}) : super(key: key) {
notificationType = NotificationType.custom;
checkAssertions();
Expand Down Expand Up @@ -72,6 +74,8 @@ class ElegantNotification extends StatefulWidget {
this.progressBarPadding,
this.onDismiss,
this.progressIndicatorBackground = greyColor,
this.onTap,
this.closeOnTap = false,
}) : super(key: key) {
notificationType = NotificationType.success;
progressIndicatorColor = notificationType.color();
Expand Down Expand Up @@ -105,6 +109,8 @@ class ElegantNotification extends StatefulWidget {
this.progressBarPadding,
this.onDismiss,
this.progressIndicatorBackground = greyColor,
this.onTap,
this.closeOnTap = false,
}) : super(key: key) {
notificationType = NotificationType.error;
progressIndicatorColor = notificationType.color();
Expand Down Expand Up @@ -138,6 +144,8 @@ class ElegantNotification extends StatefulWidget {
this.progressBarPadding,
this.onDismiss,
this.progressIndicatorBackground = greyColor,
this.onTap,
this.closeOnTap = false,
}) : super(key: key) {
notificationType = NotificationType.info;
progressIndicatorColor = notificationType.color();
Expand All @@ -153,6 +161,12 @@ class ElegantNotification extends StatefulWidget {
if (action != null) {
assert(onActionPressed != null);
}
if (onTap != null) {
assert(
action == null && onActionPressed == null,
'You can not set both an action and an onTap method',
);
}

if (position == Alignment.centerRight) {
assert(
Expand Down Expand Up @@ -286,11 +300,18 @@ class ElegantNotification extends StatefulWidget {
final Widget Function(void Function() dismissNotification)? closeButton;

///Function invoked when user press on the close button
final Function()? onCloseButtonPressed;
final void Function()? onCloseButtonPressed;

///Function invoked when the notification is closed after the finish of the progress indicator
///
final Function()? onProgressFinished;
final void Function()? onProgressFinished;

///Function invoked when the user taps on the notification
final void Function()? onTap;

///Whether to close the notification when the tap on an action or on the
///notification itself
final bool closeOnTap;

///The type of the notification, will be set automatically on every constructor
///possible values
Expand Down Expand Up @@ -474,63 +495,83 @@ class ElegantNotificationState extends State<ElegantNotification>
);
}

void closeNotification() {
widget.onCloseButtonPressed?.call();
closeTimer.cancel();
slideController.reverse();
widget.onDismiss?.call();
widget.closeOverlay();
}

@override
Widget build(BuildContext context) {
return SlideTransition(
position: offsetAnimation,
child: Container(
width: widget.width ?? MediaQuery.of(context).size.width * 0.7,
height: widget.height ?? MediaQuery.of(context).size.height * 0.12,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(widget.radius),
color: widget.background,
boxShadow: widget.enableShadow
? [
BoxShadow(
color: widget.shadowColor.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 1,
offset: const Offset(0, 1), // changes position of shadow
),
]
: null,
),
child: Column(
children: [
Expanded(
child: ToastContent(
title: widget.title,
description: widget.description,
notificationType: widget.notificationType,
icon: widget.icon,
displayCloseButton: widget.displayCloseButton,
closeButton: widget.closeButton,
onCloseButtonPressed: () {
widget.onCloseButtonPressed?.call();
closeTimer.cancel();
slideController.reverse();
widget.onDismiss?.call();
widget.closeOverlay();
},
iconSize: widget.iconSize,
action: widget.action,
onActionPressed: widget.onActionPressed,
child: InkWell(
onTap: widget.onTap == null
? null
: () {
widget.onTap!();
if (widget.closeOnTap) {
closeNotification();
}
},
child: Container(
width: widget.width ?? MediaQuery.of(context).size.width * 0.7,
height: widget.height ?? MediaQuery.of(context).size.height * 0.12,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(widget.radius),
color: widget.background,
boxShadow: widget.enableShadow
? [
BoxShadow(
color: widget.shadowColor.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 1,
offset: const Offset(0, 1), // changes position of shadow
),
]
: null,
),
child: Column(
children: [
Expanded(
child: ToastContent(
title: widget.title,
description: widget.description,
notificationType: widget.notificationType,
icon: widget.icon,
displayCloseButton:
widget.onTap == null ? widget.displayCloseButton : false,
closeButton: widget.closeButton,
onCloseButtonPressed: closeNotification,
iconSize: widget.iconSize,
action: widget.action,
onActionPressed: widget.onActionPressed == null
? null
: () {
widget.onActionPressed!();
if (widget.closeOnTap) {
closeNotification();
}
},
),
),
),
if (widget.showProgressIndicator)
Padding(
padding: widget.progressBarPadding ?? const EdgeInsets.all(0),
child: SizedBox(
width: widget.progressBarWidth,
height: widget.progressBarHeight,
child: AnimatedProgressBar(
foregroundColor: widget.progressIndicatorColor,
duration: widget.toastDuration,
backgroundColor: widget.progressIndicatorBackground,
if (widget.showProgressIndicator)
Padding(
padding: widget.progressBarPadding ?? const EdgeInsets.all(0),
child: SizedBox(
width: widget.progressBarWidth,
height: widget.progressBarHeight,
child: AnimatedProgressBar(
foregroundColor: widget.progressIndicatorColor,
duration: widget.toastDuration,
backgroundColor: widget.progressIndicatorBackground,
),
),
),
),
],
],
),
),
),
);
Expand Down