Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: how to test the scroll

I have a ListView.builder with a ScrollController as controller:

  _buildListView(state) => ListView.builder(
        itemBuilder: (_, int index) => _draw(index, state),
        itemCount: state.count
        controller: _scrollController,
      );

I add a listener to the controller:

  View() {
    _scrollController.addListener(_onScroll);
  }

I would like to test the _onScroll function:

 void _onScroll() {
    final maxScroll = _scrollController.position.maxScrollExtent;
    final currentScroll = _scrollController.position.pixels;
    if (maxScroll - currentScroll <= _scrollThreshold) {
      _bloc.dispatch(Fetch());
    }
  }

But I don't know how can I test it. This is what I tried so far:

  testWidgets('Should test the scroll',
      (WidgetTester tester) async {
await tester.pumpWidget(generateApp());
    await tester.pump();
    await tester.drag(find.byType(ListView), const Offset(0.0, -300));
    await tester.pump();
...

)}

but it doesn't call at all that function.

like image 384
Little Monkey Avatar asked May 24 '19 11:05

Little Monkey


1 Answers

for those using the new flutter_test lib, we also have the dragUntilVisible method:

await tester.dragUntilVisible(
    find.text('Earn mana!'), // what you want to find
    find.byKey(ValueKey('OnboardingCarousel')), // widget you want to scroll
    const Offset(-250, 0), // delta to move
);
like image 73
Guilherme V. Avatar answered Oct 08 '22 20:10

Guilherme V.