-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
149 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,36 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:language_tool/language_tool.dart'; | ||
import 'package:languagetool_textfield/languagetool_textfield.dart'; | ||
|
||
/// Main screen widget demonstrating library usage example | ||
class App extends StatefulWidget { | ||
/// Creates a new instance of main screen widget | ||
const App({super.key}); | ||
|
||
@override | ||
State<App> createState() => _AppState(); | ||
} | ||
|
||
class _AppState extends State<App> { | ||
final LangToolService _langToolService = LangToolService(LanguageTool()); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const Placeholder(); | ||
return Scaffold( | ||
body: Center( | ||
child: Padding( | ||
padding: const EdgeInsets.all(20.0), | ||
child: LanguageToolTextField( | ||
langService: _langToolService, | ||
style: const TextStyle(), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
library languagetool_textfield; | ||
|
||
export 'domain/language_check_service.dart'; | ||
export 'implementations/lang_tool_service.dart'; | ||
export 'presentation/language_tool_text_field.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
lib/presentation/widgets/custom_text_field_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:languagetool_textfield/domain/mistake.dart'; | ||
|
||
/// Custom controller for an editable text field, that supports | ||
/// mistakes highlighting. | ||
class CustomTextFieldController extends TextEditingController { | ||
/// List of mistakes in text. | ||
final List<Mistake> mistakes; | ||
|
||
/// Creates a controller for an editable text field. | ||
CustomTextFieldController({ | ||
String? text, | ||
this.mistakes = const [], | ||
}) : super(text: text); | ||
|
||
@override | ||
TextSpan buildTextSpan({ | ||
required BuildContext context, | ||
TextStyle? style, | ||
required bool withComposing, | ||
}) { | ||
final List<TextSpan> children = []; | ||
const underlineThickness = 2.0; | ||
const backgroundOpacity = 0.2; | ||
|
||
if (mistakes.isEmpty) { | ||
return TextSpan(text: text, style: style); | ||
} | ||
|
||
for (int i = 0; i < mistakes.length; i++) { | ||
final mistake = mistakes[i]; | ||
final previousMistakePosition = | ||
i > 0 ? mistakes[i - 1].offset + mistakes[i - 1].length : 0; | ||
final mistakeStart = mistake.offset; | ||
final mistakeEnd = mistakeStart + mistake.length; | ||
|
||
children.add( | ||
TextSpan(text: text.substring(previousMistakePosition, mistakeStart)), | ||
); | ||
|
||
children.add( | ||
TextSpan( | ||
text: text.substring(mistakeStart, mistakeEnd), | ||
style: TextStyle( | ||
decoration: TextDecoration.underline, | ||
decorationColor: Colors.red, | ||
decorationThickness: underlineThickness, | ||
backgroundColor: Colors.red.withOpacity(backgroundOpacity), | ||
), | ||
), | ||
); | ||
|
||
if (mistake == mistakes.last) { | ||
children.add( | ||
TextSpan( | ||
text: text.substring( | ||
mistakeEnd, | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
return TextSpan(children: children, style: style); | ||
} | ||
} |