gpt4 book ai didi

testing - Flutter:如何测试滚动

转载 作者:IT王子 更新时间:2023-10-29 07:19:33 25 4
gpt4 key购买 nike

我有一个带有 ScrollController 作为 Controller 的 ListView.builder:

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

我向 Controller 添加一个监听器:

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

我想测试 _onScroll 函数:

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

但我不知道如何测试它。到目前为止,这是我尝试过的:

  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();
...

)}

但它根本不调用该函数。

最佳答案

对于那些使用新的 flutter_test 库的用户,我们还有 dragUntilVisible 方法:

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
);

关于testing - Flutter:如何测试滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56291806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com