【问题标题】:Flutter: how to test the scrollFlutter:如何测试滚动
【发布时间】:2019-05-24 11:29:52
【问题描述】:

我有一个带有 ScrollController 作为控制器的 ListView.builder:

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

我给控制器添加了一个监听器:

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

)}

但它根本不调用那个函数。

【问题讨论】:

    标签: testing scroll flutter


    【解决方案1】:

    对于那些使用新的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
    );
    

    【讨论】:

      【解决方案2】:

      您可以在测试中创建TestGesture 并以这种方式执行滚动。

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

      【讨论】:

      • 我应该只使用这些行还是做更多?因为只有这样是行不通的
      • 第一行需要右括号,否则可以!
      • 我正在尝试使用这个 sn-p 来测试 PageView 滚动,但它似乎不起作用:gesture = await tester.startGesture(Offset(500, 500)); await gesture.moveBy(Offset(-100, 0));
      • 我必须在泵调用之前添加 await gesture.up(); 才能滚动工作。
      【解决方案3】:

      如果将key参数传递给builder:

      ListView.builder(key: Key('ListViewKey'),...);
      

      然后按键查找:

      await tester.drag(find.byKey(Key('ListViewKey')), const Offset(0.0, -300));
      await tester.pump();
      

      会起作用的。

      【讨论】:

        【解决方案4】:

        可以使用dragUntilVisible 测试滚动,它会滚动小部件直到它在屏幕上可见,只需确保添加了正确的增量以垂直或水平移动它。

         final expectedWidget = find.byText("Find me!");
        
          await tester.dragUntilVisible(
              expectedWidget, // what you want to find
              find.byType(ListView),
              // widget you want to scroll
              const Offset(0, 500) // delta to move
             );
        

        【讨论】:

          【解决方案5】:

          作为替代方案,也可以搜索 ListView 小部件本身并检索其滚动控制器以直接操作它:

          final listView = tester.widget<ListView>(find.byType(ListView));
          final ctrl = listView.controller;
          ctrl.jumpTo(ctrl.offset + 300);
          await tester.pumpAndSettle(duration);
          

          【讨论】:

          • 谢谢你,为我工作得很好!
          【解决方案6】:

          我强烈建议您注意屏幕/拖动运动的“笛卡尔平面”。

          让我解释一下:

          1. 您应该使用: await tester.drag(keyCartItemProduct1, Offset(-500.0, 0.0));
          2. 但是,您的“偏移”命令必须遵循与拖动相同的“笛卡尔方向”。

          2.1)因此:(命令偏移使用笛卡尔“方向”)-让我们看看: a) 左拖动:偏移量(-500.0, 0.0) b) 右拖动:Offset(+500.0, 0.0) c) 向上拖动:Offset(0.0, +500.0) d) 向下拖动:Offset(0.0, -500.0)

          【讨论】:

            猜你喜欢
            • 2020-06-28
            • 2019-06-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-05
            • 2022-11-16
            • 2021-04-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多