showCustomSnackbar static method

void showCustomSnackbar(
  1. BuildContext context,
  2. String message, {
  3. int durationInSeconds = 2,
  4. Color? backgroundColor,
})

Implementation

static void showCustomSnackbar(
  BuildContext context,
  String message, {
  int durationInSeconds = 2,
  Color? backgroundColor,
}) {
  if (_isSnackbarVisible) {
    return;
  }

  backgroundColor ??= Theme.of(context).colorScheme.inverseSurface;
  _isSnackbarVisible = true;

  final maxWidth = WidgetMaxWidthCalculator.getMaxWidth(context);
  final horizontalMargin = (Get.width - maxWidth) / 2;
  var safeHorizontalMargin = horizontalMargin > 0 ? horizontalMargin : 0.0;

  final defaultMargin = Get.width * 0.1;
  if (safeHorizontalMargin < defaultMargin) {
    safeHorizontalMargin = defaultMargin;
  }

  Get.showSnackbar(
    GetSnackBar(
      messageText: Center(
        child: Text(
          message,
          textAlign: TextAlign.center,
          style: TextStyle(
            color: Theme.of(context).colorScheme.onInverseSurface,
          ),
        ),
      ),
      duration: Duration(seconds: durationInSeconds),
      snackPosition: SnackPosition.BOTTOM,
      backgroundColor: backgroundColor,
      margin: EdgeInsets.symmetric(
        horizontal: safeHorizontalMargin,
        vertical: 20,
      ),
      borderRadius: 10,
      isDismissible: true,
      forwardAnimationCurve: Curves.easeOutCirc,
      onTap: (_) {
        Get.closeCurrentSnackbar();
      },
    ),
  ).future.then((_) {
    _isSnackbarVisible = false;
  });
}