toast function
Display a snackbar
When the body of the snackbar is tapped, onTap
will be called with a callback that will hide the snackbar.
Call the function parameter passed on the callback to close the snackbar.
toast( title: 'title', message: 'message', onTap: (close) => close());
toast( title: 'error title', message: 'error message', error: true );
Implementation
ScaffoldFeatureController toast({
required BuildContext context,
String? title,
required String message,
Icon? icon,
Duration duration = const Duration(seconds: 8),
Function(Function)? onTap,
bool? error,
bool hideCloseButton = false,
Color? backgroundColor,
Color? foregroundColor,
double runSpacing = 12,
}) {
if (error == true) {
backgroundColor ??= Theme.of(context).colorScheme.error;
foregroundColor ??= Theme.of(context).colorScheme.onError;
}
return ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: duration,
backgroundColor: backgroundColor,
content: Row(
children: [
Expanded(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
if (onTap == null) return;
onTap(() {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
});
},
child: Row(children: [
if (icon != null) ...[
Theme(
data: Theme.of(context).copyWith(
iconTheme: IconThemeData(color: foregroundColor),
),
child: icon,
),
SizedBox(width: runSpacing),
],
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
if (title != null)
Text(
title,
style: TextStyle(
color: foregroundColor,
fontWeight: FontWeight.bold),
),
Text(message, style: TextStyle(color: foregroundColor)),
],
),
),
]),
),
),
if (hideCloseButton == false)
TextButton(
onPressed: () {
if (!context.mounted) return;
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
child: Text(
'dismiss'.t,
style: TextStyle(color: foregroundColor),
),
)
],
),
),
);
}