扑来 - >从单词列表中的RichText
我对颤振没有太多经验。
我正在使用 Dart 的 language_tool 库 (https://pub.dev/packages/language_tool) 和扑。
我想要错误列表中出现的单词,这些单词是由于 String text = 'Henlo i am Gabriele'; 中的 language_tool 发现的存在语法错误的单词。
为红色并带有下划线,(即使我决定更改字符串文本也会发生这种情况)。
- 这是我到目前为止的代码:
import 'package:flutter/material.dart';
import 'package:language_tool/language_tool.dart';
void main() => runApp(mainApp());
class mainApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Chat(),
);
}
}
class Chat extends StatefulWidget {
const Chat({Key? key}) : super(key: key);
@override
_ChatState createState() => _ChatState();
}
class _ChatState extends State<Chat> {
String text = 'Henlo i am Gabriele';
List<WritingMistake> mistakes = [];
List Error = [];
void tool(String text) async {
var tool = LanguageTool();
var result = tool.check(text);
var correction = await result;
for (var m in correction) {
WritingMistake mistake = WritingMistake(
message: m.message,
offset: m.offset,
length: m.length,
issueType: m.issueType,
issueDescription: m.issueDescription,
replacements: m.replacements,
);
mistakes.add(mistake);
}
for (var mistake in mistakes) {
var error =
text.substring(mistake.offset!, mistake.offset! + mistake.length!);
Error.add(error);
}
print(mistakes.length);
print(mistakes);
print(Error);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ListView(
children: [
Container(
color: Colors.red,
height: 150.0,
width: double.infinity,
child: Center(
child: Text(text, style: const TextStyle(fontSize: 20.0))),
),
Container(
color: Colors.white60,
height: 150.0,
width: double.infinity,
),
],
),
),
);
}
}
这是此代码的输出:
最后,这是我想要的输出:
我希望我说得很清楚,我希望有人可以帮助我。
谢谢 :)
I don't have much experience with flutter.
I am using the language_tool library (https://pub.dev/packages/language_tool) for Dart and Flutter.
I would like the words present in the Errors list, which are the words with grammatical errors found thanks to language_tool inside the String text = 'Henlo i am Gabriele';
are in red and underlined, (and that this happens even if I decide to change the string text).
-
this is the code I have so far:
import 'package:flutter/material.dart';
import 'package:language_tool/language_tool.dart';
void main() => runApp(mainApp());
class mainApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Chat(),
);
}
}
class Chat extends StatefulWidget {
const Chat({Key? key}) : super(key: key);
@override
_ChatState createState() => _ChatState();
}
class _ChatState extends State<Chat> {
String text = 'Henlo i am Gabriele';
List<WritingMistake> mistakes = [];
List Error = [];
void tool(String text) async {
var tool = LanguageTool();
var result = tool.check(text);
var correction = await result;
for (var m in correction) {
WritingMistake mistake = WritingMistake(
message: m.message,
offset: m.offset,
length: m.length,
issueType: m.issueType,
issueDescription: m.issueDescription,
replacements: m.replacements,
);
mistakes.add(mistake);
}
for (var mistake in mistakes) {
var error =
text.substring(mistake.offset!, mistake.offset! + mistake.length!);
Error.add(error);
}
print(mistakes.length);
print(mistakes);
print(Error);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ListView(
children: [
Container(
color: Colors.red,
height: 150.0,
width: double.infinity,
child: Center(
child: Text(text, style: const TextStyle(fontSize: 20.0))),
),
Container(
color: Colors.white60,
height: 150.0,
width: double.infinity,
),
],
),
),
);
}
}
this is the output of this code:
and finally, this is the output I would like to have:
I hope I was clear and I hope someone can help me.
Thank you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可行,但需要重构。
我还将您的功能从“tool”更新为“getErrors”
我实现 FutureBuilder 是因为我们需要使用异步进程
tool.check(text)
来检查字符串并返回错误修正。我找不到制作线性下划线的方法,您可以注意到下划线有一些上下。
编辑:
我更新了代码,现在可以工作了。
PS:单词之间的空格是使用 SizedBox 手动设置的。如果您找到并分享更舒适的解决方案,我将不胜感激。
当库在字符串中查找一些错误时,我还添加了一个带有加载文本的字符串。
This should works, but need to be refactored.
I also update your function from "tool" to "getErrors"
I implement the FutureBuilder because we need to work with an asynchronous process
tool.check(text)
that check the string and return the correction with errors.I can't find a way to make a linear underline, you can notice some up and down in the underline.
EDIT:
I updated the code now it's work.
PS: the space between words was made manually using a SizedBox. I would be grateful if you find and share a more comfortable solution.
I also added a string with loading text while the library is looking for some errors in the string.