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 />");
  html.Element? ele = html.querySelector("#toast-content");
  String content = """
        var toastElement = Toastify({
          text: '$m',
          gravity: '$gravity',
          position: '$position',
          duration: $time,
          close: $showClose,
          backgroundColor: "$bgcolor",
        });
        toastElement.showToast();
      """;
  if (html.querySelector("#toast-content") != null) {
    ele!.remove();
  }
  final html.ScriptElement scriptText = html.ScriptElement()
    ..id = "toast-content"
    ..innerHtml = content;
  html.querySelector('head')!.children.add(scriptText);
  if (textColor != null) {
    html.Element toast = html.querySelector('.toastify')!;
    String tcRadix = textColor.toRadixString(16);
    final String tC = "${tcRadix.substring(2)}${tcRadix.substring(0, 2)}";
    toast.style.setProperty('color', "#$tC");
  }
}