From 5f93393312241b5616a9253b7bf9cd82b5d77dda Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Mon, 5 Feb 2024 23:53:03 +0200 Subject: [PATCH] Fix iOS password autofill prompt dismissal causes layout to resize (flutter/engine#50364) fixes [Save password prompt dismiss is pushing UI up and down](https://github.com/flutter/flutter/issues/112281) ### Code sample
expand to view the code sample ```dart import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( debugShowCheckedModeBanner: false, home: HomePage(), ); } } class HomePage extends StatefulWidget { const HomePage({super.key}); @override State createState() => _HomePageState(); } class _HomePageState extends State { final TextEditingController controller1 = TextEditingController(); final TextEditingController controller2 = TextEditingController(); @override void dispose() { controller1.dispose(); controller2.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( // Replace Scaffold with Material to fix glitch. body: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ const Text('Login (Without Scaffold)'), AutofillGroup( child: Column( children: [ TextField( controller: controller1, autofillHints: const [AutofillHints.username], ), TextField( controller: controller2, autofillHints: const [AutofillHints.password], ), ], ), ), ], ), ), ); } } ```
### Before https://github.com/flutter/engine/assets/48603081/dfe36616-e1dd-4c6c-95b0-e4bd89bd3a6a ### After https://github.com/flutter/engine/assets/48603081/cfb15252-10cd-4521-a1ef-2cace0004588 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../framework/Source/FlutterViewController.mm | 9 ++++ .../Source/FlutterViewControllerTest.mm | 54 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 64d53602ad..f92830c713 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -1520,6 +1520,15 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) BOOL keyboardAnimationIsCompounding = self.keyboardAnimationIsShowing == keyboardWillShow && _keyboardAnimationVSyncClient != nil; + // Avoid triggering startKeyBoardAnimation when keyboard notifications are triggered + // by the dismissal of password autofill prompt. When this happens, there is + // no keyboard on the screen and FlutterTextInputViewAccessibilityHider is nil. + FlutterTextInputPlugin* textInputPlugin = self.engine.textInputPlugin; + UIView* textInputHider = [[textInputPlugin textInputView] superview]; + if (keyboardWillShow && textInputHider == nil) { + return; + } + // Mark keyboard as showing or hiding. self.keyboardAnimationIsShowing = keyboardWillShow; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm index a68a55287f..afd89130af 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm @@ -305,6 +305,18 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil]; + FlutterMethodCall* setClientCall = + [FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient" + arguments:@[ @(123), @{@"autocorrect" : @YES} ]]; + [engine.textInputPlugin handleMethodCall:setClientCall + result:^(id _Nullable result){ + }]; + + // Check if FlutterTextInputViewAccessibilityHider exists. + FlutterTextInputPlugin* textInputPlugin = engine.textInputPlugin; + UIView* textInputHider = [[textInputPlugin textInputView] superview]; + XCTAssertNotNil(textInputHider); + FlutterViewController* viewControllerMock = OCMPartialMock(viewController); UIScreen* screen = [self setUpMockScreen]; CGRect viewFrame = screen.bounds; @@ -2188,4 +2200,46 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; XCTAssertNil(viewController.keyboardAnimationVSyncClient); } +- (void)testAvoidKeyboardAnimationWhenFlutterTextInputViewAccessibilityHiderIsNil { + FlutterEngine* engine = [[FlutterEngine alloc] init]; + [engine runWithEntrypoint:nil]; + FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine + nibName:nil + bundle:nil]; + + FlutterViewController* viewControllerMock = OCMPartialMock(viewController); + UIScreen* screen = [self setUpMockScreen]; + CGRect viewFrame = screen.bounds; + [self setUpMockView:viewControllerMock + screen:screen + viewFrame:viewFrame + convertedFrame:viewFrame]; + + CGFloat screenHeight = screen.bounds.size.height; + CGFloat screenWidth = screen.bounds.size.height; + + // Check if FlutterTextInputViewAccessibilityHider is nil. + FlutterTextInputPlugin* textInputPlugin = engine.textInputPlugin; + UIView* textInputHider = [[textInputPlugin textInputView] superview]; + XCTAssertNil(textInputHider); + + // Start show keyboard animation. + CGRect showKeyboardBeginFrame = CGRectMake(0, screenHeight, screenWidth, 250); + CGRect showKeyboardEndFrame = CGRectMake(0, screenHeight - 250, screenWidth, 250); + NSNotification* fakeNotification = + [NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification + object:nil + userInfo:@{ + @"UIKeyboardFrameBeginUserInfoKey" : @(showKeyboardBeginFrame), + @"UIKeyboardFrameEndUserInfoKey" : @(showKeyboardEndFrame), + @"UIKeyboardAnimationDurationUserInfoKey" : @(0.25), + @"UIKeyboardIsLocalUserInfoKey" : @(YES) + }]; + + viewControllerMock.targetViewInsetBottom = 0; + [viewControllerMock handleKeyboardNotification:fakeNotification]; + BOOL isShowingAnimation = viewControllerMock.keyboardAnimationIsShowing; + XCTAssertFalse(isShowingAnimation); +} + @end