Skip to content

Commit f60eef8

Browse files
authored
Fix ime insets animation jump (flutter#191453)
Instead of relying on deprecated window flags for the IME insets - mathematically calculate the insets during the animation. Testing with [Keyboard Test App](https://github.com/mboetger/keyboard_test): API 33: | Before | After | | --- | --- | | <img width="272" height="600" alt="before" src="https://github.com/user-attachments/assets/e508d14b-5576-4e61-bef8-25b977d05dc2" /> | <img width="272" height="600" alt="after" src="https://github.com/user-attachments/assets/575f1d31-bb33-4c72-99f4-af0e1f3504fa" /> | Fixes: flutter#190974, flutter#191156 ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing.
1 parent e673f1a commit f60eef8

12 files changed

Lines changed: 1483 additions & 121 deletions

File tree

‎.ci.yaml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3129,6 +3129,16 @@ targets:
31293129
["devicelab", "android", "linux", "pixel", "7pro"]
31303130
task_name: integration_ui_keyboard_resize
31313131

3132+
- name: Linux_pixel_7pro integration_ui_keyboard_inset_jump
3133+
recipe: devicelab/devicelab_drone
3134+
presubmit: false
3135+
bringup: true
3136+
timeout: 60
3137+
properties:
3138+
tags: >
3139+
["devicelab", "android", "linux", "pixel", "7pro"]
3140+
task_name: integration_ui_keyboard_inset_jump
3141+
31323142
- name: Linux_pixel_7pro integration_ui_textfield
31333143
recipe: devicelab/devicelab_drone
31343144
presubmit: false

‎TESTOWNERS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
/dev/devicelab/bin/tasks/integration_test_test.dart @bkonyi @flutter/tool
159159
/dev/devicelab/bin/tasks/integration_ui_driver.dart @bkonyi @flutter/tool
160160
/dev/devicelab/bin/tasks/integration_ui_frame_number.dart @jtmcdole @flutter/engine
161+
/dev/devicelab/bin/tasks/integration_ui_keyboard_inset_jump.dart @mboetger @flutter/android
161162
/dev/devicelab/bin/tasks/integration_ui_keyboard_resize.dart @bkonyi @flutter/tool
162163
/dev/devicelab/bin/tasks/integration_ui_textfield.dart @bkonyi @flutter/tool
163164
/dev/devicelab/bin/tasks/microbenchmarks.dart @jtmcdole @flutter/engine
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_devicelab/framework/devices.dart';
6+
import 'package:flutter_devicelab/framework/framework.dart';
7+
import 'package:flutter_devicelab/tasks/integration_tests.dart';
8+
9+
Future<void> main() async {
10+
deviceOperatingSystem = DeviceOperatingSystem.android;
11+
await task(createEndToEndKeyboardInsetJumpTest());
12+
}

‎dev/devicelab/lib/tasks/integration_tests.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ TaskFunction createEndToEndKeyboardTest({String? deviceIdOverride}) {
102102
).call;
103103
}
104104

105+
TaskFunction createEndToEndKeyboardInsetJumpTest({String? deviceIdOverride}) {
106+
return DriverTest(
107+
'${flutterDirectory.path}/dev/integration_tests/ui',
108+
'lib/keyboard_inset_jump.dart',
109+
deviceIdOverride: deviceIdOverride,
110+
).call;
111+
}
112+
105113
TaskFunction createEndToEndFrameNumberTest({String? deviceIdOverride}) {
106114
return DriverTest(
107115
'${flutterDirectory.path}/dev/integration_tests/ui',

‎dev/integration_tests/ui/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Normally they are run via the devicelab.
99

1010
Verifies that showing and hiding the keyboard resizes the content.
1111

12+
## keyboard\_inset\_jump
13+
14+
Verifies that showing and hiding the keyboard in edge-to-edge mode does not produce terminal insets jumps or discontinuous animation steps (validates fix for flutter/flutter#190974).
15+
1216
## routing
1317

1418
Verifies that `flutter drive --route` works correctly.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:convert';
6+
import 'dart:ui' as ui;
7+
8+
import 'package:flutter/material.dart';
9+
import 'package:flutter/scheduler.dart';
10+
import 'package:flutter/services.dart';
11+
import 'package:flutter_driver/driver_extension.dart';
12+
13+
import 'keys.dart' as keys;
14+
15+
/// Integration test host application for measuring soft keyboard animation smoothness.
16+
///
17+
/// What it tests:
18+
/// Hosts an edge-to-edge text editing UI and samples FlutterView.viewInsets.bottom
19+
/// on every frame via persistent frame callbacks, exposing frame-by-frame history and deltas
20+
/// over FlutterDriverExtension.
21+
///
22+
/// Why it was added:
23+
/// Provides an end-to-end integration target for testing fix of flutter/flutter#190974
24+
/// on live Android emulators/devices.
25+
void main() {
26+
final recordedDeltas = <double>[];
27+
final recordedInsets = <double>[];
28+
double? lastInset;
29+
30+
enableFlutterDriverExtension(
31+
enableTextEntryEmulation: false,
32+
handler: (String? message) async {
33+
if (message == 'get_history') {
34+
return jsonEncode(<String, dynamic>{'insets': recordedInsets, 'deltas': recordedDeltas});
35+
}
36+
if (message == 'reset_history') {
37+
recordedDeltas.clear();
38+
recordedInsets.clear();
39+
lastInset = null;
40+
return 'ok';
41+
}
42+
return 'unknown_command';
43+
},
44+
);
45+
46+
SchedulerBinding.instance.addPersistentFrameCallback((Duration _) {
47+
final ui.FlutterView? view = WidgetsBinding.instance.platformDispatcher.views.firstOrNull;
48+
if (view == null) {
49+
return;
50+
}
51+
final double insets = view.viewInsets.bottom / view.devicePixelRatio;
52+
if (lastInset == null || (insets - lastInset!).abs() > 0.01) {
53+
final double delta = lastInset == null ? 0.0 : (insets - lastInset!).abs();
54+
recordedInsets.add(insets);
55+
recordedDeltas.add(delta);
56+
lastInset = insets;
57+
}
58+
});
59+
60+
runApp(const KeyboardInsetJumpApp());
61+
}
62+
63+
class KeyboardInsetJumpApp extends StatefulWidget {
64+
const KeyboardInsetJumpApp({super.key});
65+
66+
@override
67+
State<KeyboardInsetJumpApp> createState() => _KeyboardInsetJumpAppState();
68+
}
69+
70+
class _KeyboardInsetJumpAppState extends State<KeyboardInsetJumpApp> {
71+
final TextEditingController _controller = TextEditingController();
72+
final FocusNode _focusNode = FocusNode();
73+
74+
@override
75+
void initState() {
76+
super.initState();
77+
// Default to edge-to-edge system UI mode for testing issue #190974
78+
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
79+
}
80+
81+
@override
82+
void dispose() {
83+
_controller.dispose();
84+
_focusNode.dispose();
85+
super.dispose();
86+
}
87+
88+
@override
89+
Widget build(BuildContext context) {
90+
final double insetsBottom = MediaQuery.viewInsetsOf(context).bottom;
91+
92+
return MaterialApp(
93+
debugShowCheckedModeBanner: false,
94+
home: Scaffold(
95+
resizeToAvoidBottomInset: false,
96+
body: SafeArea(
97+
bottom: false,
98+
child: Padding(
99+
padding: EdgeInsets.only(bottom: insetsBottom),
100+
child: Column(
101+
children: <Widget>[
102+
const SizedBox(height: 40),
103+
Text(
104+
insetsBottom.toStringAsFixed(1),
105+
key: const Key(keys.kInsetsText),
106+
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
107+
),
108+
ElevatedButton(
109+
key: const Key(keys.kEdgeToEdgeButton),
110+
onPressed: () {
111+
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
112+
},
113+
child: const Text('Enable EdgeToEdge'),
114+
),
115+
const Spacer(),
116+
Container(height: 8, color: Colors.red),
117+
Padding(
118+
padding: const EdgeInsets.all(8.0),
119+
child: TextField(
120+
key: const Key(keys.kDefaultTextField),
121+
controller: _controller,
122+
focusNode: _focusNode,
123+
decoration: const InputDecoration(
124+
hintText: 'Tap to open soft keyboard',
125+
border: OutlineInputBorder(),
126+
),
127+
),
128+
),
129+
],
130+
),
131+
),
132+
),
133+
floatingActionButton: FloatingActionButton(
134+
key: const Key(keys.kUnfocusButton),
135+
onPressed: () {
136+
_focusNode.unfocus();
137+
},
138+
child: const Icon(Icons.keyboard_hide),
139+
),
140+
),
141+
);
142+
}
143+
}

‎dev/integration_tests/ui/lib/keys.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ const String kUnfocusButton = 'unfocus_button';
88
const String kOffsetText = 'offset_text';
99
const String kListView = 'list_view';
1010
const String kKeyboardVisibleView = 'keyboard_visible';
11+
const String kEdgeToEdgeButton = 'edge_to_edge_button';
12+
const String kInsetsText = 'insets_text';
13+
const String kMaxDeltaText = 'max_delta_text';
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:convert';
6+
7+
import 'package:flutter_driver/flutter_driver.dart';
8+
import 'package:integration_ui/keys.dart' as keys;
9+
import 'package:test/test.dart' hide isInstanceOf;
10+
11+
/// End-to-end Flutter Driver test validating soft keyboard animation smoothness.
12+
///
13+
/// What it tests:
14+
/// 1. Connects to keyboard_inset_jump application on a live device/emulator.
15+
/// 2. Enables edgeToEdge system UI mode.
16+
/// 3. Focuses text field to trigger native soft keyboard opening animation.
17+
/// 4. Checks that viewInsets.bottom increases monotonically and that the final
18+
/// step is < 20.0dp (below the 24dp+ nav bar jump from #190974, with some
19+
/// room for dropped frames on CI).
20+
/// 5. Unfocuses text field and verifies smooth return to 0dp insets.
21+
///
22+
/// Why it was added:
23+
/// Automated integration/driver test for flutter/flutter#190974.
24+
void main() {
25+
group('IME Insets Animation Jump Driver Test', () {
26+
late FlutterDriver driver;
27+
28+
setUpAll(() async {
29+
driver = await FlutterDriver.connect();
30+
});
31+
32+
tearDownAll(() async {
33+
await driver.close();
34+
});
35+
36+
test('validates smooth IME insets animation without terminal jump in edgeToEdge mode', () async {
37+
final SerializableFinder edgeToEdgeButton = find.byValueKey(keys.kEdgeToEdgeButton);
38+
final SerializableFinder defaultTextField = find.byValueKey(keys.kDefaultTextField);
39+
final SerializableFinder insetsText = find.byValueKey(keys.kInsetsText);
40+
final SerializableFinder unfocusButton = find.byValueKey(keys.kUnfocusButton);
41+
42+
await driver.waitFor(edgeToEdgeButton);
43+
await driver.tap(edgeToEdgeButton);
44+
45+
// Reset recorded insets frame history before opening keyboard.
46+
await driver.requestData('reset_history');
47+
48+
// Tap text field to trigger IME opening animation.
49+
await driver.waitFor(defaultTextField);
50+
await driver.tap(defaultTextField);
51+
52+
// Wait for keyboard to open and settle.
53+
const pollDelay = Duration(milliseconds: 300);
54+
var settled = false;
55+
var lastReportedInset = 0.0;
56+
for (var i = 0; i < 40; ++i) {
57+
await Future<void>.delayed(pollDelay);
58+
final String insetsStr = await driver.getText(insetsText);
59+
final double currentInset = double.tryParse(insetsStr) ?? 0.0;
60+
if (currentInset > 50.0 && currentInset == lastReportedInset) {
61+
settled = true;
62+
break;
63+
}
64+
lastReportedInset = currentInset;
65+
}
66+
expect(settled, isTrue, reason: 'Keyboard should open and reach a settled non-zero inset');
67+
68+
// Fetch frame-by-frame trajectory recorded during opening.
69+
final String rawHistory = await driver.requestData('get_history');
70+
final dynamic decoded = jsonDecode(rawHistory);
71+
// #190974 causes a jump equal to the nav bar height (24dp for gesture nav,
72+
// 48dp for 3-button) when onEnd fires. Since unchanged frames aren't
73+
// recorded, deltas.last is the final onProgress step when there's no jump.
74+
// 20dp is below the 24dp nav bar height while leaving room for a dropped
75+
// frame near the end of the animation on CI (which can be 10-15dp).
76+
final Map<String, dynamic> history = decoded is Map<String, dynamic>
77+
? decoded
78+
: <String, dynamic>{};
79+
final List<dynamic> insets = history['insets'] as List<dynamic>? ?? <dynamic>[];
80+
81+
expect(
82+
insets.length,
83+
greaterThan(1),
84+
reason: 'Should have captured multiple animation frames',
85+
);
86+
87+
final double finalInset = (insets.last as num).toDouble();
88+
expect(
89+
finalInset,
90+
greaterThan(100.0),
91+
reason: 'Keyboard should reach full height, but reached $finalInset',
92+
);
93+
94+
// Verify that the opening animation trajectory is monotonically non-decreasing
95+
// and does not exhibit erratic reversals or jumping backwards.
96+
for (var i = 1; i < insets.length; i++) {
97+
final double current = (insets[i] as num).toDouble();
98+
final double previous = (insets[i - 1] as num).toDouble();
99+
expect(
100+
current,
101+
greaterThanOrEqualTo(previous),
102+
reason:
103+
'Opening animation must be monotonic: frame $i ($current) < frame ${i - 1} ($previous)',
104+
);
105+
}
106+
107+
// Test dismissal
108+
await driver.requestData('reset_history');
109+
await driver.waitFor(unfocusButton);
110+
await driver.tap(unfocusButton);
111+
112+
var closed = false;
113+
for (var i = 0; i < 30; ++i) {
114+
await Future<void>.delayed(pollDelay);
115+
final String insetsStr = await driver.getText(insetsText);
116+
final double currentInset = double.tryParse(insetsStr) ?? 0.0;
117+
if (currentInset == 0.0) {
118+
closed = true;
119+
break;
120+
}
121+
}
122+
expect(closed, isTrue, reason: 'Keyboard should dismiss back to 0 inset');
123+
}, timeout: Timeout.none);
124+
});
125+
}

0 commit comments

Comments
 (0)