stringWidth function

int stringWidth(
  1. String text
)

Returns the visual width of a string (simple approximation). For full CJK/emoji awareness, integrate a proper stringWidth package.

Implementation

int stringWidth(String text) {
  // Simple: count characters. Replace with a proper grapheme-aware
  // width calculation when a suitable Dart package is available.
  var width = 0;
  for (final rune in text.runes) {
    if (rune > 0xFFFF) {
      width += 2; // Emoji / surrogate pair
    } else if (_isCJK(rune)) {
      width += 2;
    } else {
      width += 1;
    }
  }
  return width;
}