Commit 7fbb5b9f authored by Dinesh Ahuja's avatar Dinesh Ahuja Committed by Todd Volkert

Create WidgetTester.ensureVisible(Finder) (#22620)

This is just a shorthand for
Scrollable.ensureVisible(tester.element(finder))

Fixes #8185
parent d5777b6a
......@@ -677,6 +677,12 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
SemanticsHandle ensureSemantics() {
return binding.pipelineOwner.ensureSemantics();
}
/// Given a widget `W` specified by [finder] and a [Scrollable] widget `S` in
/// its ancestry tree, this scrolls `S` so as to make `W` visible.
///
/// Shorthand for `Scrollable.ensureVisible(tester.element(finder))`
Future<void> ensureVisible(Finder finder) => Scrollable.ensureVisible(element(finder));
}
typedef _TickerDisposeCallback = void Function(_TestTicker ticker);
......
......@@ -616,6 +616,30 @@ void main() {
semanticsHandle.dispose();
});
});
group('ensureVisible', () {
testWidgets('scrolls to make widget visible', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ListView.builder(
itemCount: 20,
shrinkWrap: true,
itemBuilder: (BuildContext context, int i) => ListTile(title: Text('Item $i')),
),
),
),
);
// Make sure widget isn't on screen
expect(find.text('Item 15', skipOffstage: true), findsNothing);
await tester.ensureVisible(find.text('Item 15', skipOffstage: false));
await tester.pumpAndSettle();
expect(find.text('Item 15', skipOffstage: true), findsOneWidget);
});
});
}
class FakeMatcher extends AsyncMatcher {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment