showWidget function

void showWidget(
  1. String type,
  2. DataModel data,
  3. Config config,
  4. BuildContext context, {
  5. bool showNextAndPreviousButtons = false,
  6. Map keys = const {},
})

Implementation

void showWidget(
  String type,
  DataModel data,
  Config config,
  BuildContext context, {
  bool showNextAndPreviousButtons = false,
  Map<dynamic, dynamic> keys = const {},
}) async {
  String? shape,
      title,
      body,
      height,
      width,
      url,
      position,
      color,
      background,
      textColor,
      selector;
  int? scale;
  GlobalKey? key;
  bool showConfetti = false, isDraggable = false;
  if (type != "tour") {
    shape = data.steps[0].shape ?? "rect";

    title = data.steps[0].title;
    body = data.steps[0].content;
    height = data.steps[0].height;
    width = data.steps[0].width;
    background = data.steps[0].background ?? "#ffffff";
    textColor = data.steps[0].textColor ?? "#000000";
    showConfetti = data.steps[0].showConfetti ?? false;

    url = data.steps[0].url;
    //TODO adjust scale from frontend(cs) first
    scale = null;
    // scale =
    //     int.tryParse(jsonResponse["content"]["bodyHtmlScale"].toString());
    isDraggable = data.steps[0].draggable ?? false;
    position = data.steps[0].position;
    color = data.steps[0].color;
    selector = data.steps[0].selector;
    key = keys[selector.toString()];
  }

  PagePilot.showConfetti = showConfetti;

  if (((body != null) || url != null) || type == "tour") {
    switch (type) {
      case "tooltip":
      case "i":
      case "?":
        if (key == null) {
          throw Exception(
            "PagePilotPluginError: Key not found for ${selector.toString()}",
          );
        }
        PagePilot.showTooltip(context, key: key, data: data);
        await acknowledge(data.id, Config.userId, type);
        break;
      case "tour":
      case "walktrough":
        PagePilot.showTour(
          context,
          config,
          data: data,
          scrollController: config.scrollController,
          showNextAndPreviousButtons: showNextAndPreviousButtons,
          gkeys: keys,
        );

        await acknowledge(data.id, Config.userId, type);
        break;

      /*case "dialog":
        PagePilot.showOkDialog(
          context,
          shape: shape ?? "rect",
          title: title,
          body: body,
          background: background,
          textColor: textColor,
          url: url,
          scale: scale,
          onOkPressed: () async {
            await client.get(
              Uri.parse(
                "$baseUrl/acknowledge?id=${jsonResponse["_id"]}",
              ),
            );
          },
        );
        break;
      case "snackbar":
      case "snack":
      case "toast":
        PagePilot.showSnackbar(
          context,
          title: title,
          body: body,
          background: background,
          textColor: textColor,
          url: url,
          scale: scale,
          duration: int.tryParse(jsonResponse["timeout"].toString()) ?? 3000,
        );
        //acknowledge
        await client.get(
          Uri.parse(
            "$baseUrl/acknowledge?id=${jsonResponse["_id"]}",
          ),
        );
        break;
      case "bottomsheet":
        PagePilot.showBottomSheet(
          context,
          title: title,
          body: body,
          background: background,
          textColor: textColor,
          url: url,
          scale: scale,
          onOkPressed: () async {
            await client.get(
              Uri.parse(
                "$baseUrl/acknowledge?id=${jsonResponse["_id"]}",
              ),
            );
          },
        );
        break;
      // case "spotlight":
      //   break;
      case "pip":
      case "floatingwidget":
        PagePilot.showFloatingWidget(
          context,
          title: title,
          body: body,
          background: background,
          textColor: textColor,
          url: url,
          position: position,
          scale: scale,
          isDraggable: isDraggable,
          isVisible: true,
        );
        await client.get(
          Uri.parse(
            "$baseUrl/acknowledge?id=${jsonResponse["_id"]}",
          ),
        );
        break;
      case "beacon":
        if (key == null) {
          throw Exception(
            "PagePilotPluginError: Key not found for ${jsonResponse["content"]["element"].toString()}",
          );
        }
        PagePilot.showBeacon(
          context,
          shape: shape ?? "rect",
          key: key,
          beaconPosition: position == null ? "center" : position.toLowerCase(),
          title: title,
          body: body ?? "",
          background: background,
          textColor: textColor,
          color: color == null
              ? Colors.blue.withOpacity(0.5)
              : PagePilot.hexToColor(color),
          onBeaconClicked: () async {
            //acknowledge
            await client.get(
              Uri.parse(
                "$baseUrl/acknowledge?id=${jsonResponse["_id"]}",
              ),
            );
          },
          // title: jsonResponse["content"]["tour"][0]["title"],
          // description: jsonResponse["content"]["tour"][0]["description"],
        );

        break;*/
    }
  } else {
    throw Exception(
      "PagePilotPluginError: Either provide title & body or html for $type  and key: ${data.steps[0].selector.toString()}",
    );
  }
}