How To Convert Text To Upper Case & Lower Case In Flutter

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

there are two inbuilt functions available to convert any type of string text to lower case and upper case using String.toLowerCase() and String.toUpperCase() function. These functions would directly apply on String and convert them to upper case and Lower case. In this tutorial we would create 2 functions to convert string to lower case and upper case. We would call these functions on Button onPress event.

How To Convert Text To Upper Case & Lower Case In Flutter

Convert String Text To Upper Case Lower Case Android iOS Example In Flutter

Complete Code FOr  Convert Text To Upper Case & Lower Case In Flutter
main.dart
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            backgroundColor: Colors.lightGreen,
            title: Text("Convert Text To Upper Case Lower Case"),
          ),
          body: SafeArea(
              child : Container(
                alignment: Alignment.center,
                child:MyHomePage(),
              )
          )
      ),
    );
  }
}
class MyHomePage extends StatefulWidget {
  @override
  MyHomePageState createState() => MyHomePageState();
}

class MyHomePageState extends State {

  String output = 'BajarangiSoft.com';

  void convertToLowerCase(){
    setState(() {
      output = output.toLowerCase() ;
    });
  }

  void convertToUpperCase(){
    setState(() {
      output = output.toUpperCase() ;
    });
  }

  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        SizedBox(height: 30.0,),
        Container(
            child: Text('$output',
                textAlign: TextAlign.center,
                style: TextStyle(fontSize: 20
                )
            )
        ),
        RaisedButton(
          child: Text('Upper Case'),
          onPressed: convertToUpperCase,
          color: Colors.lightGreen,
          textColor: Colors.white,
        ),
        RaisedButton(
          child: Text('Lower Case'),
          onPressed: convertToLowerCase,
          color: Colors.redAccent,
          textColor: Colors.white,
        ),

      ],
    );
  }
}

Related Post