F

LutterAdda

Flutter capitalize first letter

Introduction

In Flutter, there are times when you want to make the first letter of a word or sentence capitalized. This is commonly done when displaying names, titles, or headings. Fortunately, Flutter provides simple ways to achieve this. In this article, we'll explore different methods to capitalize the first letter in Flutter using easy-to-understand examples and techniques.



Method 1: Using the capitalizeFirst letter method

The easiest method to capitalize the first letter in Flutter is by using the capitalizeFirst letter method. This method takes a string and converts its first letter to uppercase while keeping the rest of the text as it is.

Example

    String capitalizeFirstLetter(String input) {
        if (input.isEmpty) {
            return input;
        }
        return input[0].toUpperCase() + input.substring(1);
    }
        
    void main() {
        String name = "john doe";
        String capitalized = capitalizeFirstLetter(name);
        print(capitalized); // Output: "John doe"
    }       
                    

In the example, we define a function called capitalizeFirstLetter. It takes an input string, checks if it's empty, and then converts the first character to uppercase using the toUpperCase method. Finally, it combines the capitalized first character with the rest of the string using the substring method.



Method 2: Using the intl package

If your Flutter app supports different languages and localization, you can use the intl package for string manipulation and formatting. The intl package provides a method called capitalizeFirstof eachWord, which capitalizes the first letter of each word in a string.

To use the intl package:

  1. Add the intl package to your pubspec.yaml file.
  2. Import the package: import 'package:intl/intl.dart';
  3. Use the capitalizeFirstof eachWord method as shown in the example below:
    import 'package:intl/intl.dart';

    void main() {
        String name = "john doe";
        String capitalized = capitalizeFirstofEachWord(name);
        print(capitalized); // Output: "John Doe"
    }

    String capitalizeFirstofEachWord(String input) {
        final format = new DateFormat('');
        return format.capitalized(input);
    }

                    


Method 3: Using the text_capitalize package

  1. Add the text_capitalize package to your pubspec.yaml file.
  2. Import the package: import 'package:text_capitalize/text_capitalize.dart';
  3. Use the capitalize method as shown in the example below:
    import 'package:text_capitalize/text_capitalize.dart';

    void main() {
        String name = "john doe";
        String capitalized = capitalize(name);
        print(capitalized); // Output: "John doe"
    }
                    

In the example, the capitalize method from the text_capitalize package is used to capitalize the first letter of the string.



Conclusion

In this article, we explored simple methods to capitalize the first letter in Flutter. We covered using the capitalizeFirst letter method, the intl package, and the text_capitalize package. With these techniques, you can easily capitalize the first letter of a word or sentence in your Flutter applications.