text stringlengths 1 372 |
|---|
appBar: AppBar( |
title: const text(title), |
), |
body: ListView.builder( |
// add a key to the ListView. this makes it possible to |
// find the list and scroll through it in the tests. |
key: const key('long_list'), |
itemCount: items.length, |
itemBuilder: (context, index) { |
return ListTile( |
title: text( |
items[index], |
// add a key to the text widget for each item. this makes |
// it possible to look for a particular item in the list |
// and verify that the text is correct |
key: key('item_${index}_text'), |
), |
); |
}, |
), |
), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
2. write a test that scrolls through the list |
now, you can write a test. in this example, scroll through the list of items and |
verify that a particular item exists in the list. the WidgetTester class |
provides the scrollUntilVisible() method, which scrolls through a list |
until a specific widget is visible. this is useful because the height of the |
items in the list can change depending on the device. |
rather than assuming that you know the height of all the items |
in a list, or that a particular widget is rendered on all devices, |
the scrollUntilVisible() method repeatedly scrolls through |
a list of items until it finds what it’s looking for. |
the following code shows how to use the scrollUntilVisible() method |
to look through the list for a particular item. this code lives in a |
file called test/widget_test.dart. |
<code_start> |
// this is a basic flutter widget test. |
// |
// to perform an interaction with a widget in your test, use the WidgetTester |
// utility that flutter provides. for example, you can send tap and scroll |
// gestures. you can also use WidgetTester to find child widgets in the widget |
// tree, read text, and verify that the values of widget properties are correct. |
import 'package:flutter/material.dart'; |
import 'package:flutter_test/flutter_test.dart'; |
import 'package:scrolling/main.dart'; |
void main() { |
testWidgets('finds a deep item in a long list', (tester) async { |
// build our app and trigger a frame. |
await tester.pumpWidget(MyApp( |
items: List<String>.generate(10000, (i) => 'item $i'), |
)); |
final listFinder = find.byType(Scrollable); |
final itemFinder = find.byKey(const ValueKey('item_50_text')); |
// scroll until the item to be found appears. |
await tester.scrollUntilVisible( |
itemFinder, |
500.0, |
scrollable: listFinder, |
); |
// verify that the item contains the correct text. |
expect(itemFinder, findsOneWidget); |
}); |
} |
<code_end> |
<topic_end> |
<topic_start> |
3. run the test |
run the test using the following command from the root of the project: |
<topic_end> |
<topic_start> |
tap, drag, and enter text |
many widgets not only display information, but also respond |
to user interaction. this includes buttons that can be tapped, |
and TextField for entering text. |
to test these interactions, you need a way to simulate them |
in the test environment. for this purpose, use the |
WidgetTester library. |
the WidgetTester provides methods for entering text, |
tapping, and dragging. |
in many cases, user interactions update the state of the app. in the test |
environment, flutter doesn’t automatically rebuild widgets when the state |
changes. to ensure that the widget tree is rebuilt after simulating a user |
interaction, call the pump() or pumpAndSettle() |
methods provided by the WidgetTester. |
this recipe uses the following steps: |
<topic_end> |
<topic_start> |
1. create a widget to test |
for this example, |
create a basic todo app that tests three features: |
to keep the focus on testing, |
this recipe won’t provide a detailed guide on how to build the todo app. |
to learn more about how this app is built, |
see the relevant recipes: |
<code_start> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.