Skip to content

Commit

Permalink
fix unexpected behaviours of onSubmitted action (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
henryleunghk authored Dec 4, 2021
1 parent 50862ad commit 009d79e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 0 additions & 1 deletion example/lib/more_use_case_listing_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class _MoreUseCaseListingPageState extends State<MoreUseCaseListingPage> {
child: NativeTextInput(
minLines: 3,
maxLines: 5,
returnKeyType: ReturnKeyType.done,
onChanged: _onChangeText,
onSubmitted: _onSubmittedText,
)),
Expand Down
14 changes: 14 additions & 0 deletions ios/Classes/NativeTextInput.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ @implementation NativeInputField {
int64_t _viewId;
FlutterMethodChannel* _channel;
NativeTextInputDelegate* _delegate;
id _Nullable _args;

float _containerWidth;
}
Expand All @@ -22,6 +23,7 @@ - (instancetype)initWithFrame:(CGRect)frame
_channel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:messenger];

_viewId = viewId;
_args = args;

_textView = [[UITextView alloc] initWithFrame:frame];
_textView.backgroundColor = UIColor.clearColor;
Expand Down Expand Up @@ -97,6 +99,18 @@ - (void)onUnFocus:(FlutterMethodCall*)call result:(FlutterResult)result {

- (void)onSetText:(FlutterMethodCall*)call result:(FlutterResult)result {
_textView.text = call.arguments[@"text"];
_textView.textColor = _delegate.fontColor;
_textView.font = _delegate.font;

if (_textView.text.length == 0) {
_textView.text = _args[@"placeholder"];
_textView.textColor = _delegate.placeholderFontColor;
_textView.font = _delegate.placeholderFont;
}

if (_textView.textContainer.maximumNumberOfLines == 1) {
_textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
}
result(nil);
}

Expand Down
6 changes: 3 additions & 3 deletions ios/Classes/NativeTextInputDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ - (void)textViewDidEndEditing:(UITextView *)textView {
if (textView.textContainer.maximumNumberOfLines == 1) {
textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
}

[_channel invokeMethod:@"inputFinished"
arguments:@{ @"text": textView.text }];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ((textView.returnKeyType != UIReturnKeyDefault ||
textView.textContainer.maximumNumberOfLines == 1) &&
[text isEqualToString:@"\n"]
) {
[_channel invokeMethod:@"inputFinished"
arguments:@{ @"text": textView.text }];

[textView resignFirstResponder];
return false;
}
Expand Down

0 comments on commit 009d79e

Please sign in to comment.