toString method

  1. @override
String toString()
override

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Implementation

@override
String toString() {
  String val = '';

  int paperSize = 32;
  val += '\n' * 3;
  val += '${'#' * (paperSize + 4)}\n';
  for (var element in commands) {
    String line = '';
    switch (element['type']) {
      case 'text':
        int padLeft = 0;
        if (element['alignment'] == PrinterAlignment.center.index) {
          padLeft = (paperSize - element['value'].toString().length)~/2;
        } else if (element['alignment'] == PrinterAlignment.left.index) {
          padLeft = 0;
        } else if (element['alignment'] == PrinterAlignment.right.index) {
          padLeft = paperSize - element['value'].toString().length;
        }
        line += (' '*padLeft) + element['value'];
        break;
      case 'doubleText':
        line += '${element['leftValue']} ${element['rightValue'].toString().padLeft((paperSize-1)-element['leftValue'].toString().length)}';
        break;
      case 'image':
        if (element['value'] == cutLineImage) {
          line += '${'-' * (paperSize-3)} >8';
          break;
        }
        line += '${(' '*((paperSize - 7)~/2))}(Image)';
        break;
      case 'qrCode':
        line += '${' '*((paperSize - 4)~/2)}(QR)';
        break;
      case 'space':
        line += ' ' * paperSize;
        break;
    }
    line = '| $line${' ' * ((paperSize) - line.length)} |';
    val += '$line\n';
  }
  val += '${'#' * (paperSize + 4)}\n';
  val += '\n' * 3;
  return val;
}