How to replace multiple text in flutter?

3,870

I am not good in RegExp, so maybe there is some better way of doing it.

String _textSelect(String str) {
  str = str.replaceAll('e', 'é');
  str = str.replaceAll('i', 'I');
  str = str.replaceAll('b', '*');
  str = str.replaceAll('v', '<');
  return str;
}


String output = _textSelect('English is blowing vhistle?'); 

Edit:

final myController = TextEditingController();
String str = '';

String _textSelect(String str) {
  str = str.replaceAll('e', 'é');
  str = str.replaceAll('i', 'I');
  str = str.replaceAll('b', '*');
  str = str.replaceAll('v', '<');
  return str;
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Retrieve Text Input'),
    ),
    body: Padding(
      padding: const EdgeInsets.all(16.0),
      child: TextField(
        controller: myController,
      ),
    ),
    floatingActionButton: FloatingActionButton(
      // When the user presses the button, show an alert dialog containing
      // the text that the user has entered into the text field.
      onPressed: () {
        str = _textSelect(myController.text); //update
        return showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              // Retrieve the text the that user has entered by using the
              // TextEditingController.
              content: Text(
                str,
                style: TextStyle(fontSize: 50, fontFamily: 'Italy'),
              ),
            );
          },
        );
      },
      tooltip: 'Show me the value!',
      child: Icon(Icons.text_fields),
    ),
  );
}
Share:
3,870
Axen_Rangs
Author by

Axen_Rangs

Updated on December 21, 2022

Comments

  • Axen_Rangs
    Axen_Rangs over 1 year

    I want to replace multiple characters which are getting from text field in flutter using Replaceall method.How can I implement that properly. Iv'e tried to do that as following but It won't replace the characters.

    _textSelect(String str){
        str=str.replaceAll('e', 'é');
        str=str.replaceAll('i', 'I');
        str=str.replaceAll('b', '*');
        str=str.replaceAll('v', '<');
    
        return str;
        }
    

    Context

    Widget build(BuildContext context) {
                return Scaffold(
                  appBar: AppBar(
                    title: Text('Retrieve Text Input'),
                  ),
                  body: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: TextField(
                      controller: myController,
                    ),
                  ),
                  floatingActionButton: FloatingActionButton(
                    // When the user presses the button, show an alert dialog containing
                    // the text that the user has entered into the text field.
                    onPressed: () {
                      _textSelect(myController.text);//update
                      return showDialog(
                        context: context,
                        builder: (context) {
                          return AlertDialog(
                            // Retrieve the text the that user has entered by using the
                            // TextEditingController.
                            content: Text(
                            str,
                            style:TextStyle(
                              fontSize:50,
                              fontFamily:"Italy"),),
                          );
                        },
                      );
                    },
                    tooltip: 'Show me the value!',
                    child: Icon(Icons.text_fields),
                  ),
                );
              }
    
    • Mohammad Assad Arshad
      Mohammad Assad Arshad almost 4 years
      shouldn't you be passing myController.text back to your _textSelectMethod?
    • Axen_Rangs
      Axen_Rangs almost 4 years
      I've used that..Code is corrected
  • Axen_Rangs
    Axen_Rangs almost 4 years
    Can I add more lines like return str.replaceAll('eibv', 'éI*<');
  • Axen_Rangs
    Axen_Rangs almost 4 years
    It gives me data null error...Don't know what went wrong
  • CopsOnRoad
    CopsOnRoad almost 4 years
    Please update your code with my solution, so that I can run locally and tell you what mistake you're making.
  • CopsOnRoad
    CopsOnRoad almost 4 years
    Ok, so what basically you want to do now with the string, do you want to update the TextField or you want to show that in Dialog?