text
stringlengths 1
474
|
|---|
const title = 'Long List';
|
return MaterialApp(
|
title: title,
|
home: Scaffold(
|
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>class TodoList extends StatefulWidget {
|
const TodoList({super.key});
|
@override
|
State<TodoList> createState() => _TodoListState();
|
}
|
class _TodoListState extends State<TodoList> {
|
static const _appTitle = 'Todo List';
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.