text stringlengths 1 372 |
|---|
<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 discuss the pros on 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) { |
const title = 'long list'; |
return MaterialApp( |
title: title, |
home: scaffold( |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.