5

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
  • 1
    shouldn't you be passing myController.text back to your _textSelectMethod? Commented Jun 8, 2020 at 7:38
  • I've used that..Code is corrected Commented Jun 8, 2020 at 7:41

3 Answers 3

3

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),
    ),
  );
}
Sign up to request clarification or add additional context in comments.

4 Comments

Can I add more lines like return str.replaceAll('eibv', 'éI*<');
It gives me data null error...Don't know what went wrong
Please update your code with my solution, so that I can run locally and tell you what mistake you're making.
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?
1

Single line answer

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.