Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create initial design for TextField #15

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion example/lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
import 'package:flutter/material.dart';
import 'package:language_tool/language_tool.dart';
import 'package:languagetool_textfield/domain/mistake.dart';
import 'package:languagetool_textfield/languagetool_textfield.dart';

/// A 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(LanguageTool());
final _textController = LanguageToolTextEditingController(
text: 'OKAYOKAYOKAYOKAYOKAY',
mistakes: [
const Mistake(
message: 'bad',
type: 'bad',
offset: 0,
length: 3,
),
const Mistake(
message: 'bad',
type: 'bad',
offset: 8,
length: 5,
),
],
);

@override
Widget build(BuildContext context) {
return const Placeholder();
return Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: LanguageToolTextField(
langService: _langToolService,
controller: _textController,
style: const TextStyle(),
),
),
),
);
}
}
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ packages:
source: hosted
version: "4.8.0"
language_tool:
dependency: transitive
dependency: "direct main"
description:
name: language_tool
sha256: "90ceb6f0a0b57fb3a5b88be82ffd676c90639cd06d622d25f76add30d5a2acd6"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
languagetool_textfield:
dependency: "direct dev"
dependency: "direct main"
description:
path: ".."
relative: true
Expand Down
5 changes: 3 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ environment:
dependencies:
flutter:
sdk: flutter
language_tool: ^2.1.1
languagetool_textfield:
path: ../

dev_dependencies:
flutter_test:
sdk: flutter
languagetool_textfield:
path: ../
solid_lints: ^0.0.14

flutter:
Expand Down
5 changes: 5 additions & 0 deletions lib/languagetool_textfield.dart
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
library languagetool_textfield;

export 'domain/language_check_service.dart';
export 'implementations/lang_tool_service.dart';
export 'presentation/language_tool_text_field.dart';
export 'presentation/widgets/language_tool_text_editing_controller.dart';
38 changes: 34 additions & 4 deletions lib/presentation/language_tool_text_field.dart
Original file line number Diff line number Diff line change
@@ -1,36 +1,66 @@
import 'package:flutter/material.dart';
import 'package:languagetool_textfield/domain/language_check_service.dart';
import 'package:languagetool_textfield/presentation/widgets/language_tool_text_editing_controller.dart';

/// A TextField widget that checks the grammar using the given [langService]
class LanguageToolTextField extends StatefulWidget {
/// A service for checking errors.
final LanguageCheckService langService;

/// A style to use for the text being edited.
final TextStyle style;
final TextStyle? style;

/// A decoration of this [TextField].
final InputDecoration decoration;
final InputDecoration? decoration;
Luxorum marked this conversation as resolved.
Show resolved Hide resolved

/// A builder function used to build errors.
final Widget Function()? mistakeBuilder;

/// A text controller used to highlight errors.
final LanguageToolTextEditingController? controller;

/// Creates a widget that checks grammar errors.
const LanguageToolTextField({
Key? key,
required this.langService,
required this.style,
required this.decoration,
this.decoration,
this.mistakeBuilder,
this.controller,
}) : super(key: key);

@override
State<LanguageToolTextField> createState() => _LanguageToolTextFieldState();
}

class _LanguageToolTextFieldState extends State<LanguageToolTextField> {
static const _borderRadius = 15.0;
static const _borderOpacity = 0.5;

final _textFieldController = LanguageToolTextEditingController();
final _textFieldBorder = OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey.withOpacity(_borderOpacity),
),
borderRadius: BorderRadius.circular(_borderRadius),
);
Luxorum marked this conversation as resolved.
Show resolved Hide resolved

@override
Widget build(BuildContext context) {
return const Placeholder();
return TextField(
autocorrect: false,
enableSuggestions: false,
textCapitalization: TextCapitalization.none,
keyboardType: TextInputType.multiline,
maxLines: null,
style: widget.style,
decoration: widget.decoration ??
InputDecoration(
focusedBorder: _textFieldBorder,
enabledBorder: _textFieldBorder,
border: _textFieldBorder,
),
controller: widget.controller ?? _textFieldController,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'package:flutter/material.dart';
import 'package:languagetool_textfield/domain/mistake.dart';

/// A custom controller for an editable text field, that supports
/// mistakes highlighting.
class LanguageToolTextEditingController extends TextEditingController {
/// A list of mistakes in the text.
final List<Mistake> mistakes;

/// Creates a controller for an editable text field.
LanguageToolTextEditingController({
String? text,
this.mistakes = const [],
}) : super(text: text);

@override
TextSpan buildTextSpan({
required BuildContext context,
TextStyle? style,
required bool withComposing,
}) {
final children = <InlineSpan>[];
const underlineThickness = 2.0;
const backgroundOpacity = 0.2;

if (mistakes.isEmpty) {
return TextSpan(text: text, style: style);
}

final lastMistakeIndex = mistakes.length - 1;

for (int i = 0; i < mistakes.length; i++) {
Luxorum marked this conversation as resolved.
Show resolved Hide resolved
final mistake = mistakes[i];
int previousMistakePosition = 0;
Luxorum marked this conversation as resolved.
Show resolved Hide resolved
if (i > 0) {
final previousMistake = mistakes[i - 1];
previousMistakePosition =
previousMistake.offset + previousMistake.length;
}
final mistakeStart = mistake.offset;
final mistakeEnd = mistakeStart + mistake.length;
Luxorum marked this conversation as resolved.
Show resolved Hide resolved
Luxorum marked this conversation as resolved.
Show resolved Hide resolved

if (mistakeEnd > text.length) {
children.add(
TextSpan(
text: text.substring(previousMistakePosition),
),
);
break;
}

children.add(
TextSpan(text: text.substring(previousMistakePosition, mistakeStart)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if mistakeStart is our of range?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be no errors anyways, cause if statement checks mistake.start + mistake.length

);

final textStyle = style ?? const TextStyle();
final mistakeText = text.substring(mistakeStart, mistakeEnd);

// WidgetSpans with mistake text characters are used here to calculate the correct caret position, which can be incorrectly positioned because of the WidgetSpan issue, described here: https://github.com/flutter/flutter/issues/107432.
// TextSpan recognizer to process clicks can't be used, because it requires the RichText widget instead of TextField, which we are using. Issue described here: https://github.com/flutter/flutter/issues/34931

Luxorum marked this conversation as resolved.
Show resolved Hide resolved
children.add(
TextSpan(
style: textStyle.copyWith(
decoration: TextDecoration.underline,
decorationColor: Colors.red,
decorationThickness: underlineThickness,
backgroundColor: Colors.red.withOpacity(backgroundOpacity),
),
children: [
for (final mistakeCharacter in mistakeText.characters)
WidgetSpan(
child: Text(
mistakeCharacter,
style: style,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need it here? I think the TextSpan sets the proper TextStyle for all of its children.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, seems that Text overrides style by it's own

),
),
],
),
);
if (i == lastMistakeIndex) {
Luxorum marked this conversation as resolved.
Show resolved Hide resolved
children.add(
TextSpan(
text: text.substring(mistakeEnd),
),
);
}
}

return TextSpan(children: children, style: style);
}
}