showSnackBar method

void showSnackBar(
  1. String msg, {
  2. Color? foreground,
  3. Color? background,
  4. SnackBarBehavior? behavior = SnackBarBehavior.floating,
  5. int durationMilliseconds = 2000,
  6. bool closeIcon = false,
  7. SnackBarAction? action,
  8. bool hideCurrent = true,
  9. TextStyle? textStyle,
  10. EdgeInsets? margin,
})

Shows a customizable snackbar. msg is the text content of the snackbar. foreground is the color of the text and close icon. background is the background color of the snackbar. behavior controls how the snackbar is displayed (floating or fixed). duration is the duration the snackbar is visible (in milliseconds). closeIcon shows or hides the close icon. action is an optional action button for the snackbar. hideCurrent determines if the current snackbar should be hidden before showing a new one.

Implementation

void showSnackBar(
  String msg, {
  Color? foreground,
  Color? background,
  SnackBarBehavior? behavior = SnackBarBehavior.floating,
  int durationMilliseconds = 2000,
  bool closeIcon = false,
  SnackBarAction? action,
  bool hideCurrent = true,
  TextStyle? textStyle,
  EdgeInsets? margin,
}) {
  final scaffoldMessenger = ScaffoldMessenger.of(this);

  if (hideCurrent) scaffoldMessenger.hideCurrentSnackBar();

  scaffoldMessenger.showSnackBar(SnackBar(
    behavior: behavior,
    closeIconColor: foreground,
    action: action,
    showCloseIcon: closeIcon,
    backgroundColor: background,
    duration: Duration(milliseconds: durationMilliseconds),
    margin: margin,
    content: Text(msg,
        style: textStyle ?? styles.labelLarge?.copyWith(color: foreground)),
  ));
}