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

<details>
<summary>expand to view the code sample</summary> 

```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<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  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: <Widget>[
            const Text('Login (Without Scaffold)'),
            AutofillGroup(
              child: Column(
                children: <Widget>[
                  TextField(
                    controller: controller1,
                    autofillHints: const <String>[AutofillHints.username],
                  ),
                  TextField(
                    controller: controller2,
                    autofillHints: const <String>[AutofillHints.password],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
```

</details>

### 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
This commit is contained in:
Taha Tesser
2024-02-05 23:53:03 +02:00
committed by GitHub
parent 0c52de1fda
commit 5f93393312
2 changed files with 63 additions and 0 deletions

View File

@@ -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;

View File

@@ -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