Search code examples
flutterdarttext

How to replace multiple text in flutter?


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),
              ),
            );
          }

Solution

  • 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),
        ),
      );
    }