item method

Future<Datum> item(
  1. Widget i, {
  2. bool? wrap,
  3. int flex = 1,
})

Implementation

Future<Datum> item(Widget i, {bool? wrap, int flex = 1}) async {
  Datum datum;
  if (i is Image) {
    if (i.image is AssetImage) {
      String assetName = (i.image as AssetImage).assetName;
      final ByteData assetByteData = await rootBundle.load(assetName);
      final Uint8List imagebytes = assetByteData.buffer.asUint8List();
      datum = Datum(
        image: base64.encode(imagebytes),
        align: "center",
        imagewidth: 550,
        imageheight: 70,
      );
    } else if (i.image is NetworkImage) {
      String result;
      String imageUrl = (i.image as NetworkImage).url;
      var response = await http.get(Uri.parse(imageUrl));
      if (response.statusCode == HttpStatus.ok) {
        var bytes = response.bodyBytes;
        result = base64.encode(bytes);
      } else {
        result = "";
        throw Exception('Failed to load image: ${response.statusCode}');
      }
      datum = Datum(
        image: result,
        align: "center",
        imagewidth: 550,
        imageheight: 70,
      );
    } else if (i.image is MemoryImage) {
      Uint8List imageUrl = (i.image as MemoryImage).bytes;
      datum = Datum(
        image: base64.encode(imageUrl),
        align: "center",
        imagewidth: 550,
        imageheight: 70,
      );
    } else {
      String imageUrl = (i.image as FileImage).file.path;
      File imagefile = File(imageUrl); //convert Path to File
      Uint8List imagebytes = await imagefile.readAsBytes(); //convert to bytes
      datum = Datum(
        image: base64.encode(imagebytes),
        align: "center",
        imagewidth: 550,
        imageheight: 70,
      );
    }
  } else if (i is Text) {
    String? textalign;
    bool bold = false;

    if (i.textAlign != null) {
      textalign = i.textAlign.toString().split(".")[1];
    }
    if (i.style != null) {
      int weight = int.parse(
        i.style!.fontWeight.toString().split(".")[1].split("w")[1],
      );
      if (weight >= 700) {
        bold = true;
      }
    }
    bool? rap;
    if (wrap != null) {
      rap = wrap;
    } else if (i.softWrap != null) {
      rap = i.softWrap;
    } else {
      rap = false;
    }
    datum = Datum(
      text: i.data.toString(),
      textsize: "normal",
      align: textalign,
      textwrap: rap,
      bold: bold,
      flex: flex,
    );
  } else if (i is Divider) {
    datum = Datum(
      text:
          "-------------------------------------------------------------------------",
      textsize: "normal",
      align: "center",
      textwrap: false,
      bold: true,
    );
  } else {
    datum = Datum(text: "", textsize: "normal", align: "center");
  }
  return datum;
}