-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TF-1884 Support RTL mode for TypeAheadFormField
- Loading branch information
Showing
6 changed files
with
166 additions
and
50 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
core/lib/presentation/views/text/type_ahead_form_field_builder.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,110 @@ | ||
import 'package:core/utils/direction_utils.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_typeahead/flutter_typeahead.dart'; | ||
|
||
class TypeAheadFormFieldBuilder<T> extends StatefulWidget { | ||
|
||
final TextDirection textDirection; | ||
final Duration debounceDuration; | ||
final SuggestionsCallback<T> suggestionsCallback; | ||
final ItemBuilder<T> itemBuilder; | ||
final SuggestionSelectionCallback<T> onSuggestionSelected; | ||
final SuggestionsBoxDecoration suggestionsBoxDecoration; | ||
final WidgetBuilder? noItemsFoundBuilder; | ||
final bool hideOnEmpty; | ||
final bool hideOnError; | ||
final bool hideOnLoading; | ||
final TextEditingController? controller; | ||
final FocusNode? focusNode; | ||
final ValueChanged<String>? onTextChange; | ||
final ValueChanged<String>? onTextSubmitted; | ||
final TextInputAction? textInputAction; | ||
final bool autocorrect; | ||
final List<String>? autofillHints; | ||
final TextInputType keyboardType; | ||
final InputDecoration decoration; | ||
|
||
const TypeAheadFormFieldBuilder({ | ||
super.key, | ||
required this.suggestionsCallback, | ||
required this.itemBuilder, | ||
required this.onSuggestionSelected, | ||
this.suggestionsBoxDecoration = const SuggestionsBoxDecoration(), | ||
this.textDirection = TextDirection.ltr, | ||
this.debounceDuration = const Duration(milliseconds: 300), | ||
this.decoration = const InputDecoration(), | ||
this.noItemsFoundBuilder, | ||
this.hideOnEmpty = false, | ||
this.hideOnError = false, | ||
this.hideOnLoading = false, | ||
this.autocorrect = false, | ||
this.keyboardType = TextInputType.text, | ||
this.controller, | ||
this.focusNode, | ||
this.autofillHints, | ||
this.textInputAction, | ||
this.onTextChange, | ||
this.onTextSubmitted, | ||
}); | ||
|
||
@override | ||
State<TypeAheadFormFieldBuilder<T>> createState() => _TypeAheadFormFieldBuilderState<T>(); | ||
} | ||
|
||
class _TypeAheadFormFieldBuilderState<T> extends State<TypeAheadFormFieldBuilder<T>> { | ||
|
||
late TextEditingController _controller; | ||
late TextDirection _textDirection; | ||
|
||
@override | ||
void initState() { | ||
_textDirection = widget.textDirection; | ||
_controller = widget.controller ?? TextEditingController(); | ||
super.initState(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return TypeAheadFormField<T>( | ||
key: widget.key, | ||
textFieldConfiguration: TextFieldConfiguration( | ||
controller: widget.controller, | ||
textInputAction: widget.textInputAction, | ||
autocorrect: widget.autocorrect, | ||
autofillHints: widget.autofillHints, | ||
keyboardType: widget.keyboardType, | ||
decoration: widget.decoration, | ||
textDirection: _textDirection, | ||
onChanged: (value) { | ||
widget.onTextChange?.call(value); | ||
if (value.isNotEmpty) { | ||
final directionByText = DirectionUtils.getDirectionByEndsText(value); | ||
if (directionByText != _textDirection) { | ||
setState(() { | ||
_textDirection = directionByText; | ||
}); | ||
} | ||
} | ||
}, | ||
onSubmitted: widget.onTextSubmitted | ||
), | ||
debounceDuration: widget.debounceDuration, | ||
suggestionsCallback: widget.suggestionsCallback, | ||
itemBuilder: widget.itemBuilder, | ||
onSuggestionSelected: widget.onSuggestionSelected, | ||
suggestionsBoxDecoration: widget.suggestionsBoxDecoration, | ||
noItemsFoundBuilder: widget.noItemsFoundBuilder, | ||
hideOnEmpty: widget.hideOnEmpty, | ||
hideOnError: widget.hideOnError, | ||
hideOnLoading: widget.hideOnLoading, | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
if (widget.controller == null) { | ||
_controller.dispose(); | ||
} | ||
super.dispose(); | ||
} | ||
} |
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
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