-1

How to capitalize the first letter in text widget?

 Text("${_products[index]["product_name"]}",style: TextStyle(fontSize: 22.sp,),)

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mosfeq Anik
  • 89
  • 1
  • 4
  • Do you mean to say you want the first letter capital of that string – Sagar Acharya Aug 24 '21 at 07:54
  • 2
    Does this answer your question? [How to capitalize the first letter of a string in dart?](https://stackoverflow.com/questions/29628989/how-to-capitalize-the-first-letter-of-a-string-in-dart) – Nagual Aug 24 '21 at 07:55

2 Answers2

3

A function for capitalization of first letter

  String capitalizeOnlyFirstLater() {
    if(this.trim().isEmpty) return "";

    return "${this[0].toUpperCase()}${this.substring(1)}";
  }

Then call the function in text

Text("${_products[index]["product_name"].capitalizeOnlyFirstLater()}",style: TextStyle(fontSize: 22.sp,),)
  
Jahidul Islam
  • 11,435
  • 3
  • 17
  • 38
0

You can get the first letter and convert it to uppercase and concatenate the rest with it.

final String productName = _products[index]["product_name"];
final String productNameInSentenceCase = productName[0] + productName.substring(1);

Text(productNameInSentenceCase, style: TextStyle(fontSize: 22.sp,),)
Victor Eronmosele
  • 7,040
  • 2
  • 10
  • 33