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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto direction the text of a text field according to user input #20

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 16 additions & 3 deletions lib/tag_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:super_tag_editor/suggestions_box_controller.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';
import 'package:super_tag_editor/utils/direction_helper.dart';
import 'package:super_tag_editor/widgets/validation_suggestion_item.dart';

import './tag_editor_layout_delegate.dart';
Expand Down Expand Up @@ -183,6 +184,7 @@ class TagEditor<T> extends StatefulWidget {
class TagsEditorState<T> extends State<TagEditor<T>> {
/// A controller to keep value of the [TextField].
late TextEditingController _textFieldController;
late TextDirection _textDirection;

/// A state variable for checking if new text is enter.
var _previousText = '';
Expand Down Expand Up @@ -211,6 +213,7 @@ class TagsEditorState<T> extends State<TagEditor<T>> {
void initState() {
super.initState();
_textFieldController = (widget.controller ?? TextEditingController());
_textDirection = widget.textDirection ?? TextDirection.ltr;
_focusNodeKeyboard = FocusNode()..addListener(_onFocusKeyboardChanged);
_focusNode = (widget.focusNode ?? FocusNode())
..addListener(_onFocusChanged);
Expand Down Expand Up @@ -603,7 +606,7 @@ class TagsEditorState<T> extends State<TagEditor<T>> {

@override
Widget build(BuildContext context) {
final decoration = widget.hasAddButton
final decoration = widget.hasAddButton && widget.icon == null
? widget.inputDecoration.copyWith(
suffixIcon: CupertinoButton(
padding: EdgeInsets.zero,
Expand Down Expand Up @@ -667,13 +670,23 @@ class TagsEditorState<T> extends State<TagEditor<T>> {
cursorColor: widget.cursorColor,
autocorrect: widget.autocorrect,
textAlign: widget.textAlign,
textDirection: widget.textDirection,
textDirection: _textDirection,
readOnly: widget.readOnly,
autofocus: widget.autofocus,
enableSuggestions: widget.enableSuggestions,
maxLines: widget.maxLines,
decoration: decoration,
onChanged: _onTextFieldChange,
onChanged: (value) {
_onTextFieldChange.call(value);
if (value.isNotEmpty) {
final directionByText = DirectionHelper.getDirectionByEndsText(value);
if (directionByText != _textDirection) {
setState(() {
_textDirection = directionByText;
});
}
}
},
onSubmitted: _onSubmitted,
inputFormatters: widget.inputFormatters,
),
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/direction_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import 'package:flutter/material.dart';
import 'package:intl/intl.dart' as intl;

abstract class DirectionHelper {

static bool isDirectionRTLByEndsText(String text) => intl.Bidi.endsWithRtl(text);

static TextDirection getDirectionByEndsText(String text) => isDirectionRTLByEndsText(text) ? TextDirection.rtl : TextDirection.ltr;
}
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ dependencies:

debounce_throttle: ^2.0.0

intl: ^0.17.0

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down