cutAndAlign method

String cutAndAlign(
  1. int newLength, {
  2. String? paddingChar,
  3. bool? leftAlign,
})

Cuts the String and, if lesser then required, aligns it padding the exceeding chars to the right or to the left with the character selected.

Example: 'Cut This'.cutAndAlign(3) returns 'Cut' 'Cut This'.cutAndAlign(10) returns 'Cut This ' 'Cut This'.cutAndAlign(10, paddingChar: '') returns 'Cut This**' 'Cut This'.cutAndAlign(10, paddingChar: '', leftAlign: false) returns '**Cut This'

The method only works from the beginning of the string

Implementation

String cutAndAlign(int newLength, {String? paddingChar, bool? leftAlign}) {
  paddingChar ??= ' ';
  leftAlign ??= true;
  final ret = cut(newLength);
  if (leftAlign) {
    return ret.padRight(newLength, paddingChar);
  } else {
    return ret.padLeft(newLength, paddingChar);
  }
}