gpt4 book ai didi

flutter - 如何在 flutter 中替换多个文本?

转载 作者:行者123 更新时间:2023-12-03 04:46:10 27 4
gpt4 key购买 nike

我想使用 Replaceall 方法替换从 flutter 中的文本字段获取的多个字符。我该如何正确实现它。我试过如下那样做,但它不会替换字符。

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

return str;
}

上下文

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

最佳答案

我不擅长 RegExp,所以也许有更好的方法。

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?');

编辑:

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

关于flutter - 如何在 flutter 中替换多个文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62257141/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com