addHtmlToast method

dynamic addHtmlToast({
  1. String msg = "",
  2. String? gravity = "top",
  3. String position = "right",
  4. String bgcolor = "linear-gradient(to right, #00b09b, #96c93d)",
  5. int time = 3000,
  6. bool showClose = false,
  7. int? textColor,
})

injects Final Toastify code with all the parameters to make toast visible on web

Implementation

addHtmlToast(
    {String msg = "",
    String? gravity = "top",
    String position = "right",
    String bgcolor = "linear-gradient(to right, #00b09b, #96c93d)",
    int time = 3000,
    bool showClose = false,
    int? textColor}) {
  String m = msg.replaceAll("'", "\\'").replaceAll("\n", "<br />");
  web.Element? ele = web.document.querySelector("#toast-content");
  String content = """
        var toastElement = Toastify({
          text: '$m',
          gravity: '$gravity',
          position: '$position',
          duration: $time,
          close: $showClose,
          backgroundColor: "$bgcolor",
        });
        toastElement.showToast();
      """;
  if (web.document.querySelector("#toast-content") != null) {
    ele!.remove();
  }
  final web.HTMLScriptElement scriptText = web.HTMLScriptElement()
    ..id = "toast-content"
    ..innerHTML = content.toJS;
  web.document.body!.append(scriptText);
  if (textColor != null) {
    web.Element toast = web.document.querySelector('.toastify')!;
    String tcRadix = textColor.toRadixString(16);
    final String tC = "${tcRadix.substring(2)}${tcRadix.substring(0, 2)}";
    final style = toast.getAttribute('style') ?? '';
    toast.setAttribute('style', '$style color: #$tC;');
  }
}