Skip to content

Commit 00863d6

Browse files
authored
Fix keyboard cover SearchAnchor list results (flutter#165382)
Fix flutter#159261 We can leverage `MediaQuery.viewInsets` to handle this case. Once the keyboard prompts and covers the SearchAnchor suggestion list, it will need a bottom padding equivalent to `MediaQuery.viewInsetsOf(context).bottom` so that it's possible to view the whole list. | before | after | | --------------- | --------------- | <video src="https://github.com/user-attachments/assets/38a1c343-e3eb-4e42-a523-eef9fb4d5dbc"/> | <video src="https://github.com/user-attachments/assets/533aeca4-08c1-494f-9d47-3775ed9157d0"/> ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Signed-off-by: huycozy <huy@nevercode.io>
1 parent 8785fb3 commit 00863d6

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

packages/flutter/lib/src/material/search_anchor.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,9 @@ class _ViewContentState extends State<_ViewContent> {
11461146
context: context,
11471147
removeTop: true,
11481148
child: ListView(
1149+
padding: EdgeInsets.only(
1150+
bottom: MediaQuery.viewInsetsOf(context).bottom,
1151+
),
11491152
shrinkWrap: effectiveShrinkWrap,
11501153
children: result.toList(),
11511154
),

packages/flutter/test/material/search_anchor_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4058,6 +4058,47 @@ void main() {
40584058
await tester.pump();
40594059
expect(searchViewState, 'Closed');
40604060
});
4061+
4062+
testWidgets(
4063+
'The last element of the suggestion list should be visible when scrolling to the end of list',
4064+
(WidgetTester tester) async {
4065+
await tester.pumpWidget(
4066+
MaterialApp(
4067+
home: SearchAnchor.bar(
4068+
suggestionsBuilder: (BuildContext context, SearchController controller) {
4069+
return List<Widget>.generate(30, (int index) {
4070+
return ListTile(
4071+
titleAlignment: ListTileTitleAlignment.center,
4072+
title: Text('Item $index'),
4073+
);
4074+
});
4075+
},
4076+
),
4077+
),
4078+
);
4079+
4080+
// Open search view.
4081+
await tester.tap(find.byIcon(Icons.search));
4082+
await tester.pumpAndSettle();
4083+
const double fakeKeyboardHeight = 500.0;
4084+
final double physicalBottomPadding = fakeKeyboardHeight * tester.view.devicePixelRatio;
4085+
4086+
// Simulate the keyboard opening resizing the view.
4087+
tester.view.viewInsets = FakeViewPadding(bottom: physicalBottomPadding);
4088+
addTearDown(tester.view.reset);
4089+
4090+
// Scroll down to the end of the list.
4091+
expect(find.byType(ListView), findsOne);
4092+
await tester.fling(find.byType(ListView), const Offset(0, -5000), 5000);
4093+
await tester.pumpAndSettle();
4094+
4095+
// The last item should not be hidden by the keyboard.
4096+
final double lastItemBottom = tester.getRect(find.text('Item 29')).bottom;
4097+
final double fakeKeyboardTop =
4098+
tester.getSize(find.byType(MaterialApp)).height - fakeKeyboardHeight;
4099+
expect(lastItemBottom, lessThanOrEqualTo(fakeKeyboardTop));
4100+
},
4101+
);
40614102
}
40624103

40634104
Future<void> checkSearchBarDefaults(

0 commit comments

Comments
 (0)