import 'dart:async';

import 'package:flutter/material.dart';


ScaffoldFeatureController<SnackBar, SnackBarClosedReason> notifyUserX(
  ScaffoldMessengerState sm,
  String message, {
  int delaySeconds = 3,
}) {
  final durationSeconds = _normalizeDelaySeconds(delaySeconds);
  sm.clearSnackBars();
  return sm.showSnackBar(
    SnackBar(
      content: Text(message),
      duration: Duration(seconds: durationSeconds),
    ),
  );
}

ScaffoldFeatureController<SnackBar, SnackBarClosedReason> notifyUser(BuildContext context, String message, { int delaySeconds = 3 }) {
  return notifyUserX(ScaffoldMessenger.of(context), message, delaySeconds: delaySeconds);
}

Future<ScaffoldFeatureController<SnackBar, SnackBarClosedReason>> postNotifyUser(
  BuildContext context,
  String message, {
  int delaySeconds = 3,
}) {
  final completer = Completer<ScaffoldFeatureController<SnackBar, SnackBarClosedReason>>();

  WidgetsBinding.instance.addPostFrameCallback((_) {
    final controller = notifyUser(context, message, delaySeconds: delaySeconds);
    completer.complete(controller);
  });

  return completer.future;
}

int _normalizeDelaySeconds(int delaySeconds) =>
    delaySeconds <= 0 ? 3 : delaySeconds;
