Skip to main content

Flutter Capitalize Text

In flutter there are no predefine property available to create Capitalize text. To make Capitalize text in flutter we will use a string tweak where first we would convert the first letter of sentence to capital with string upper case function. Then convert rest of string to lowercase.

Flutter Capitalize Text

1. Creating String variable and assign some random text to it.
final String testSentence = "lorem Ipsum is Simply Dummy tEXT.";
2. Creating text widget and defining above string with string function tweak to make capitalize text.
Text(
        testSentence[0].toUpperCase() + testSentence.substring(1).toLowerCase(),
        style: const TextStyle(fontSize: 21),
        textAlign: TextAlign.center,
      ),
Screenshot:
Flutter Capitalize Text
Source code for main.dart file:
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  final String testSentence = "lorem Ipsum is Simply Dummy tEXT.";

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Center(
      child: Text(
        testSentence[0].toUpperCase() + testSentence.substring(1).toLowerCase(),
        style: const TextStyle(fontSize: 21),
        textAlign: TextAlign.center,
      ),
    )));
  }
}

Comments