How to replace multiple text in flutter?

Viewed 5138

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

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

Your code seems to be fine as far as I can tell, only thing you haven't done is to change the state of str variable;

So do this:

onPressed: () {
             setState(() { _textSelect();}); //add setState here. 

              return showDialog(
                context: context,
                builder: (context) {
                  return AlertDialog(

                    content: Text(
                    str,
                    style:TextStyle(
                      fontSize:50,
                      fontFamily:"Italy"),),
                  );
                },
              );
Related