[iOS] Ensure the keyboard bottom inset is zero when keyboard dismiss. (flutter/engine#31982)

This commit is contained in:
WenJingRui
2022-04-01 03:30:04 +08:00
committed by GitHub
parent 9a0dd84074
commit 15bc6bd60f
2 changed files with 52 additions and 2 deletions

View File

@@ -1180,8 +1180,30 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
}
- (void)keyboardWillBeHidden:(NSNotification*)notification {
// When keyboard hide, the keyboardWillChangeFrame function will be called to update viewport
// metrics. So do not call [self updateViewportMetrics] here again.
NSDictionary* info = [notification userInfo];
if (@available(iOS 9, *)) {
// Ignore keyboard notifications related to other apps.
id isLocal = info[UIKeyboardIsLocalUserInfoKey];
if (isLocal && ![isLocal boolValue]) {
return;
}
}
// Ignore keyboard notifications if engine’s viewController is not current viewController.
if ([_engine.get() viewController] != self) {
return;
}
if (self.targetViewInsetBottom != 0) {
// Ensure the keyboard will be dismissed. Just like the keyboardWillChangeFrame,
// keyboardWillBeHidden is also in an animation block in iOS sdk, so we don't need to set the
// animation curve. Related issue: https://github.com/flutter/flutter/issues/99951
self.targetViewInsetBottom = 0;
NSTimeInterval duration =
[[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[self startKeyBoardAnimation:duration];
}
}
- (void)startKeyBoardAnimation:(NSTimeInterval)duration {

View File

@@ -125,6 +125,9 @@ typedef enum UIAccessibilityContrast : NSInteger {
@end
@interface FlutterViewController (Tests)
@property(nonatomic, assign) double targetViewInsetBottom;
- (void)surfaceUpdated:(BOOL)appeared;
- (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences;
- (void)handlePressEvent:(FlutterUIPressProxy*)press
@@ -135,6 +138,7 @@ typedef enum UIAccessibilityContrast : NSInteger {
- (void)applicationWillTerminate:(NSNotification*)notification;
- (void)goToApplicationLifecycle:(nonnull NSString*)state;
- (void)keyboardWillChangeFrame:(NSNotification*)notification;
- (void)keyboardWillBeHidden:(NSNotification*)notification;
- (void)startKeyBoardAnimation:(NSTimeInterval)duration;
- (void)ensureViewportMetricsIsCorrect;
- (void)invalidateDisplayLink;
@@ -198,6 +202,30 @@ typedef enum UIAccessibilityContrast : NSInteger {
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
- (void)testEnsureBottomInsetIsZeroWhenKeyboardDismissed {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
CGRect keyboardFrame = CGRectMake(0, 0, 0, 0);
BOOL isLocal = YES;
NSNotification* fakeNotification = [NSNotification
notificationWithName:@""
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : [NSValue valueWithCGRect:keyboardFrame],
@"UIKeyboardAnimationDurationUserInfoKey" : @(0.25),
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
viewControllerMock.targetViewInsetBottom = 10;
[viewControllerMock keyboardWillBeHidden:fakeNotification];
XCTAssertTrue(viewControllerMock.targetViewInsetBottom == 0);
}
- (void)testEnsureViewportMetricsWillInvokeAndDisplayLinkWillInvalidateInViewDidDisappear {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];