toTitleCase function

String toTitleCase(
  1. String text
)

Returns a given String with all the first chars of each word capitalised and other chars lowercase

Implementation

String toTitleCase(String text) {
  if (text.length <= 1) return text.toUpperCase();
  final List<String> words = text.split(' ');
  final capitalizedWords = words.map((word) {
    if (word.trim().isNotEmpty) {
      final String firstLetter = word.trim().substring(0, 1).toUpperCase();
      final String remainingLetters = word.trim().substring(1).toLowerCase();
      return '$firstLetter$remainingLetters';
    }
    return '';
  });
  return capitalizedWords.join(' ');
}