Flutter:如何测试滚动 [英] Flutter: how to test the scroll
本文介绍了Flutter:如何测试滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个以 ScrollController 作为控制器的 ListView.builder:
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);
}
我想测试 _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.
推荐答案
你可以创建一个 TestGesture 在您的测试中并以这种方式执行滚动.
You can create a TestGesture in your tests and perform a scroll that way.
final gesture = await tester.startGesture(Offset(0, 300)); //Position of the scrollview
await gesture.moveBy(Offset(0, -300)); //How much to scroll by
await tester.pump();
这篇关于Flutter:如何测试滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文