wrapTextSplit static method

  1. @Deprecated('Name will change to wrapText next major version but still return List<String>')
List<String> wrapTextSplit(
  1. String text,
  2. int maxWidth,
  3. int charSpacing
)

Implementation

@Deprecated('Name will change to wrapText next major version but still return List<String>')
static List<String> wrapTextSplit(String text, int maxWidth, int charSpacing) {
  List<String> lines = text.split("\n");
  List<String> output = List.empty(growable: true);

  for (String line in lines) {
    String trimmedLine = line.trim();
    if (trimmedLine.isEmpty) {
      continue;
    }
    else if (getTextWidth(trimmedLine, charSpacing) <= maxWidth) {
      output.add(trimmedLine);
    }
    else {
      String thisLine = "";
      List<String> words = trimmedLine.split(" ");
      for (String word in words) {
        if (getTextWidth("$thisLine $word", charSpacing) > maxWidth) {
          // TODO handle case in which first word > maxWidth
          // TODO shouldn't really be measuring length with the leading space char
          output.add(thisLine);
          thisLine = word;
        } else if (thisLine.isEmpty) {
          thisLine = word;
        } else {
          thisLine += " $word";
        }
      }
      if (thisLine.isNotEmpty) {
        output.add(thisLine.trim());
      }
    }
  }
  return output;
}