0

我未能使用小部件 DropdownButton 和 DropdownButtonFormField应用Handle 滚动文档。

我试图创建一个包含 10 000 个字符串的列表,滚动找到第 750 个并点击它。测试在最后一次点击时生成一条警告消息:“对 tap() 的调用......不会命中......也许小部件实际上不在屏幕上,或者另一个小部件遮挡了它,或者小部件无法接收指针事件。”

不知何故,测试表明它通过了,这非常令人不安。

任何帮助将不胜感激。

这是我用于小部件的代码:

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    const title = 'Long List';
    // Generate a list of "Item 1" ... "Item 10000"
    List<String> items =  List<String>.generate(10000, (i) => "Item $i");
    return MaterialApp(
      title: title,
      home: Scaffold(
        body: buildListView(items),
      ),
    );
  }

  DropdownButton buildListView(List<String> items) {
    // Flutter documentation implementation https://api.flutter.dev/flutter/material/DropdownButton-class.html
    return DropdownButton(
      items: items.map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
      onChanged: (value) {},
    );
  }
}

和“通过”测试:

testWidgets('Counter increments smoke test', (WidgetTester tester) async {
  // Build our app and trigger a frame.
  await tester.pumpWidget(MyApp());

  // Find the list
  final listFinder = find.byType(Scrollable);
  // Item searched from list
  final itemFinder = find.text('Item 750');

  // Open the list
  await tester.tap(find.byType(DropdownButton<dynamic>));
  await tester.pumpAndSettle();

  // Scroll until the item to be found appears.
  await tester.scrollUntilVisible(
    itemFinder,
    500.0,
    scrollable: listFinder,
    maxScrolls: 100
  );
  await tester.pumpAndSettle();

  // Tap the value generates warning "A call to tap()...that would not hit..."
  await tester.tap(itemFinder);
  await tester.pumpAndSettle();

  // But finding the value passes ?...
  expect(itemFinder, findsOneWidget);
});
4

0 回答 0