Gaurang Goda
Gaurang Goda

Reputation: 3855

TextAllCaps in Text() widget of Flutter?

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?

Upvotes: 29

Views: 24760

Answers (4)

Guvanch
Guvanch

Reputation: 1288

(EDITED) My solution is like this:

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

APPLE

Upvotes: 21

Martin
Martin

Reputation: 363

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();
}

Upvotes: -1

André Felipe
André Felipe

Reputation: 11

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'.

Upvotes: 0

Dharmesh Mansata
Dharmesh Mansata

Reputation: 4738

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]);

Upvotes: 2

Related Questions