tasq/lib/utils/snackbar.dart

86 lines
2.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/material.dart';
import 'package:awesome_snackbar_content/awesome_snackbar_content.dart';
/// Helper wrappers around `awesome_snackbar_content` so that callers
/// can show snackbars with a consistent look and only specify a message and
/// semantic type.
/// Enumeration used by callers; the mapping to the packages
/// [ContentType] is internal.
enum SnackType { success, info, warning, error }
ContentType _mapSnackType(SnackType t) {
switch (t) {
case SnackType.success:
return ContentType.success;
case SnackType.info:
return ContentType.help;
case SnackType.warning:
return ContentType.warning;
case SnackType.error:
return ContentType.failure;
}
}
/// Core function used by all of the convenience helpers below.
void showAwesomeSnackBar(
BuildContext context, {
required String title,
required String message,
required SnackType snackType,
}) {
// Add margin and padding so even very short messages feel substantial.
final snackBar = SnackBar(
elevation: 0,
behavior: SnackBarBehavior.floating,
// give floating snackbar some breathing room
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
padding: EdgeInsets.zero,
backgroundColor: Colors.transparent,
content: Container(
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 8),
child: AwesomeSnackbarContent(
title: title,
message: message,
contentType: _mapSnackType(snackType),
),
),
);
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(snackBar);
}
void showSuccessSnackBar(BuildContext context, String message) =>
showAwesomeSnackBar(
context,
title: 'Success',
message: message,
snackType: SnackType.success,
);
void showErrorSnackBar(BuildContext context, String message) =>
showAwesomeSnackBar(
context,
title: 'Error',
message: message,
snackType: SnackType.error,
);
void showInfoSnackBar(BuildContext context, String message) =>
showAwesomeSnackBar(
context,
title: 'Info',
message: message,
snackType: SnackType.info,
);
void showWarningSnackBar(BuildContext context, String message) =>
showAwesomeSnackBar(
context,
title: 'Warning',
message: message,
snackType: SnackType.warning,
);