Reputation: 53
Is such a thing possible?
example: "My favourite film is the matrix. And i think... "
similar questions have been asked before but they are forsimple capital the first letter of first word and not more
this codes just worked for first letter of first word and cant understand "."
extension CapExtension on String {
String get inCaps =>
this.length > 0 ? '${this[0].toUpperCase()}${this.substring(1)}' : '';
String get capitalizeFirstofEach => this
.replaceAll(RegExp(' +'), ' ')
.split(" ")
.map((str) => str.inCaps)
.join(" ");
}
class CapitalCaseTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: newValue.text.inCaps,
selection: newValue.selection,
);
}
}
TextFormField(
inputFormatters: [
CapitalCaseTextFormatter()
]
)
Upvotes: 1
Views: 3925
Reputation: 12575
Use this for capitalize each sentence first word
extension CapExtension on String {
String capitalizeSentence() {
// Each sentence becomes an array element
var sentences = this.split('.');
// Initialize string as empty string
var output = '';
// Loop through each sentence
for (var sen in sentences) {
// Trim leading and trailing whitespace
var trimmed = sen.trim();
// Capitalize first letter of current sentence
var capitalized = "${trimmed[0].toUpperCase() + trimmed.substring(1)}";
// Add current sentence to output with a period
output += capitalized + ". ";
}
return output;
}
}
// Text input formatter
class CapitalCaseTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: newValue.text.capitalizeSentence,
selection: newValue.selection,
);
}
}
Upvotes: 1
Reputation: 1710
Add textCapitalization
property
TextFormField(
textCapitalization: TextCapitalization.sentences,
inputFormatters: [
CapitalCaseTextFormatter() //no need of it then
]
)
Upvotes: 1