28

As we are having android:textAllCaps="true" feature in Android's Textview, how can we give this same feature in Text() Widget of Flutter?

I know Text('Abc'.toUpperCase()), is one way but I don't want to do it manually. Is there any property of Text() widget that converts it automatically or any widget that has similar property?

Gaurang Goda
  • 3,394
  • 4
  • 20
  • 29

4 Answers4

20

(EDITED) My solution is like this:

Text("apple".toUpperCase())
Returns:

APPLE
Guvanch
  • 838
  • 7
  • 10
2

Use following function for the First word as Caps

String getCapitalizeString({String str}) {
    if (str.length <= 1) { return str.toUpperCase(); }
    return '${str[0].toUpperCase()}${str.substring(1)}';
}

Use :

Text(this.getCapitalizeString(str: listObj[position]);
Dharmesh Mansata
  • 4,422
  • 1
  • 27
  • 33
0

To capitalize the text like this: "this is only a example" to this "This Is Only A Example",

use this function:

firstCharacterUpper(String text) {
    List arrayPieces = List();

    String outPut = '';

    text = 'this is only a example'; // This is not necessary, is only for the example. The text here is that one is passed in parameter.

    text.split(' ').forEach((sepparetedWord) {
      arrayPieces.add(sepparetedWord);
    });

    arrayPieces.forEach((word) {
      word =
          "${word[0].toString().toUpperCase()}${word.toString().substring(1)} ";
      outPut += word;
    });

    return outPut;
}

OutPut: 'This Is Only A Example'.

-1

just to simplify the function of the answers before

String getCapitalizeString(String str) {
  String cRet = '';
  str.split(' ').forEach((word) {
    cRet += "${word[0].toUpperCase()}${word.substring(1).toLowerCase()} ";
  });
  return cRet.trim();
}
Martin
  • 343
  • 2
  • 11