text stringlengths 1 474 |
|---|
final todos = <String>[]; |
final controller = TextEditingController(); |
@override |
Widget build(BuildContext context) { |
return MaterialApp( |
title: _appTitle, |
home: Scaffold( |
appBar: AppBar( |
title: const Text(_appTitle), |
), |
body: Column( |
children: [ |
TextField( |
controller: controller, |
), |
Expanded( |
child: ListView.builder( |
itemCount: todos.length, |
itemBuilder: (context, index) { |
final todo = todos[index]; |
return Dismissible( |
key: Key('$todo$index'), |
onDismissed: (direction) => todos.removeAt(index), |
background: Container(color: Colors.red), |
child: ListTile(title: Text(todo)), |
); |
}, |
), |
), |
], |
), |
floatingActionButton: FloatingActionButton( |
onPressed: () { |
setState(() { |
todos.add(controller.text); |
controller.clear(); |
}); |
}, |
child: const Icon(Icons.add), |
), |
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start> |
2. Enter text in the text field |
Now that you have a todo app, begin writing the test. |
Start by entering text into the TextField.Accomplish this task by: |
<code_start>testWidgets('Add and remove a todo', (tester) async { |
// Build the widget |
await tester.pumpWidget(const TodoList()); |
// Enter 'hi' into the TextField. |
await tester.enterText(find.byType(TextField), 'hi'); |
});<code_end> |
info Note |
This recipe builds upon previous widget testing recipes. |
To learn the core concepts of widget testing, |
see the following recipes:<topic_end> |
<topic_start> |
3. Ensure tapping a button adds the todo |
After entering text into the TextField, ensure that tapping |
the FloatingActionButton adds the item to the list.This involves three steps: |
<code_start>testWidgets('Add and remove a todo', (tester) async { |
// Enter text code... |
// Tap the add button. |
await tester.tap(find.byType(FloatingActionButton)); |
// Rebuild the widget after the state has changed. |
await tester.pump(); |
// Expect to find the item on screen. |
expect(find.text('hi'), findsOneWidget); |
});<code_end> |
<topic_end> |
<topic_start> |
4. Ensure swipe-to-dismiss removes the todo |
Finally, ensure that performing a swipe-to-dismiss action on the todo |
item removes it from the list. This involves three steps: |
<code_start>testWidgets('Add and remove a todo', (tester) async { |
// Enter text and add the item... |
// Swipe the item to dismiss it. |
await tester.drag(find.byType(Dismissible), const Offset(500, 0)); |
// Build the widget until the dismiss animation ends. |
await tester.pumpAndSettle(); |
// Ensure that the item is no longer on screen. |
expect(find.text('hi'), findsNothing); |
});<code_end> |
<topic_end> |
<topic_start> |
Complete example |
<code_start>import 'package:flutter/material.dart'; |
import 'package:flutter_test/flutter_test.dart'; |
void main() { |
testWidgets('Add and remove a todo', (tester) async { |
// Build the widget. |
await tester.pumpWidget(const TodoList()); |
// Enter 'hi' into the TextField. |
await tester.enterText(find.byType(TextField), 'hi'); |
// Tap the add button. |
await tester.tap(find.byType(FloatingActionButton)); |
// Rebuild the widget with the new item. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.