TextAllCaps in Text() widget of Flutter?

旧时模样 提交于 2020-12-08 05:26: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?


回答1:


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



回答2:


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




回答3:


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



回答4:


My solution is like this:

Text(_capitalize("apple"))

and the custom method is

String _capitalize(String value) {
    return value.substring(0, 1).toUpperCase() +
        value.substring(1, value.length);
  }

Returns

Apple


来源:https://stackoverflow.com/questions/55654466/textallcaps-in-text-widget-of-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!