text
stringlengths 1
474
|
|---|
<topic_end>
|
<topic_start>
|
2. Find a widget with a specific Key
|
In some cases, you might want to find a widget based on the Key that has been
|
provided to it. This can be handy if displaying multiple instances of the
|
same widget. For example, a ListView might display several
|
Text widgets that contain the same text.In this case, provide a Key to each widget in the list. This allows
|
an app to uniquely identify a specific widget, making it easier to find
|
the widget in the test environment.
|
<code_start>testWidgets('finds a widget using a Key', (tester) async {
|
// Define the test key.
|
const testKey = Key('K');
|
// Build a MaterialApp with the testKey.
|
await tester.pumpWidget(MaterialApp(key: testKey, home: Container()));
|
// Find the MaterialApp widget using the testKey.
|
expect(find.byKey(testKey), findsOneWidget);
|
});<code_end>
|
<topic_end>
|
<topic_start>
|
3. Find a specific widget instance
|
Finally, you might be interested in locating a specific instance of a widget.
|
For example, this can be useful when creating widgets that take a child
|
property and you want to ensure you’re rendering the child widget.
|
<code_start>testWidgets('finds a specific instance', (tester) async {
|
const childWidget = Padding(padding: EdgeInsets.zero);
|
// Provide the childWidget to the Container.
|
await tester.pumpWidget(Container(child: childWidget));
|
// Search for the childWidget in the tree and verify it exists.
|
expect(find.byWidget(childWidget), findsOneWidget);
|
});<code_end>
|
<topic_end>
|
<topic_start>
|
Summary
|
The find constant provided by the flutter_test package provides
|
several ways to locate widgets in the test environment. This recipe
|
demonstrated three of these methods, and several more methods exist
|
for different purposes.If the above examples do not work for a particular use-case,
|
see the CommonFinders documentation
|
to review all available methods.<topic_end>
|
<topic_start>
|
Complete example
|
<code_start>import 'package:flutter/material.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
void main() {
|
testWidgets('finds a Text widget', (tester) async {
|
// Build an App with a Text widget that displays the letter 'H'.
|
await tester.pumpWidget(const MaterialApp(
|
home: Scaffold(
|
body: Text('H'),
|
),
|
));
|
// Find a widget that displays the letter 'H'.
|
expect(find.text('H'), findsOneWidget);
|
});
|
testWidgets('finds a widget using a Key', (tester) async {
|
// Define the test key.
|
const testKey = Key('K');
|
// Build a MaterialApp with the testKey.
|
await tester.pumpWidget(MaterialApp(key: testKey, home: Container()));
|
// Find the MaterialApp widget using the testKey.
|
expect(find.byKey(testKey), findsOneWidget);
|
});
|
testWidgets('finds a specific instance', (tester) async {
|
const childWidget = Padding(padding: EdgeInsets.zero);
|
// Provide the childWidget to the Container.
|
await tester.pumpWidget(Container(child: childWidget));
|
// Search for the childWidget in the tree and verify it exists.
|
expect(find.byWidget(childWidget), findsOneWidget);
|
});
|
}<code_end>
|
<topic_end>
|
<topic_start>Handle scrolling
|
Many apps feature lists of content,
|
from email clients to music apps and beyond.
|
To verify that lists contain the expected content
|
using widget tests,
|
you need a way to scroll through lists to search for particular items.To scroll through lists via integration tests,
|
use the methods provided by the WidgetTester class,
|
which is included in the flutter_test package:In this recipe, learn how to scroll through a list of items to
|
verify a specific widget is being displayed,
|
and the pros and cons of different approaches.This recipe uses the following steps:<topic_end>
|
<topic_start>
|
1. Create an app with a list of items
|
This recipe builds an app that shows a long list of items.
|
To keep this recipe focused on testing, use the app created in the
|
Work with long lists recipe.
|
If you’re unsure of how to work with long lists,
|
see that recipe for an introduction.Add keys to the widgets you want to interact with
|
inside the integration tests.
|
<code_start>import 'package:flutter/material.dart';
|
void main() {
|
runApp(MyApp(
|
items: List<String>.generate(10000, (i) => 'Item $i'),
|
));
|
}
|
class MyApp extends StatelessWidget {
|
final List<String> items;
|
const MyApp({super.key, required this.items});
|
@override
|
Widget build(BuildContext context) {
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.