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),
),
);
}
Author by
Axen_Rangs
Updated on December 21, 2022Comments
-
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 almost 4 yearsshouldn't you be passing myController.text back to your _textSelectMethod?
-
Axen_Rangs almost 4 yearsI've used that..Code is corrected
-
-
Axen_Rangs almost 4 yearsCan I add more lines like
return str.replaceAll('eibv', 'éI*<');
-
Axen_Rangs almost 4 yearsIt gives me data null error...Don't know what went wrong
-
CopsOnRoad almost 4 yearsPlease update your code with my solution, so that I can run locally and tell you what mistake you're making.
-
CopsOnRoad almost 4 yearsOk, 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?