Skip to content

Commit

Permalink
Create initial design for TextField
Browse files Browse the repository at this point in the history
  • Loading branch information
Luxorum committed Apr 24, 2023
1 parent 135195e commit 6bb0e55
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 8 deletions.
23 changes: 22 additions & 1 deletion example/lib/app.dart
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(),
),
),
),
);
}
}
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
4 changes: 4 additions & 0 deletions lib/languagetool_textfield.dart
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';
55 changes: 52 additions & 3 deletions lib/presentation/language_tool_text_field.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import 'package:flutter/material.dart';
import 'package:languagetool_textfield/domain/language_check_service.dart';
import 'package:languagetool_textfield/domain/mistake.dart';
import 'package:languagetool_textfield/presentation/widgets/custom_text_field_controller.dart';

class LanguageToolTextField extends StatefulWidget {
final LanguageCheckService langService;
final TextStyle style;
final InputDecoration decoration;
final InputDecoration? decoration;
final Widget Function()? mistakeBuilder;

const LanguageToolTextField({
Key? key,
required this.langService,
required this.style,
required this.decoration,
this.decoration,
this.mistakeBuilder,
}) : super(key: key);

Expand All @@ -20,9 +22,56 @@ class LanguageToolTextField extends StatefulWidget {
}

class _LanguageToolTextFieldState extends State<LanguageToolTextField> {
final _textFieldController = CustomTextFieldController(
text: 'OkayOkayOkay',
mistakes: [
const Mistake(
message: 'bad',
type: 'bad',
offset: 1,
length: 2,
replacements: ['1'],
),
const Mistake(
message: 'bad',
type: 'bad',
offset: 4,
length: 2,
replacements: ['1'],
),
const Mistake(
message: 'bad',
type: 'bad',
offset: 8,
length: 2,
replacements: ['1'],
),
],
);
static const _borderRadius = 15.0;
static const _borderOpacity = 0.5;
final _textFieldBorder = OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey.withOpacity(_borderOpacity),
),
borderRadius: BorderRadius.circular(_borderRadius),
);

@override
Widget build(BuildContext context) {
return const Placeholder();
return TextField(
autocorrect: false,
enableSuggestions: false,
textCapitalization: TextCapitalization.none,
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: widget.decoration ??
InputDecoration(
focusedBorder: _textFieldBorder,
enabledBorder: _textFieldBorder,
border: _textFieldBorder,
),
controller: _textFieldController,
);
}
}
66 changes: 66 additions & 0 deletions lib/presentation/widgets/custom_text_field_controller.dart
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);
}
}

0 comments on commit 6bb0e55

Please sign in to comment.