text
stringlengths 1
372
|
|---|
class TodoList extends StatefulWidget {
|
const TodoList({super.key});
|
@override
|
State<TodoList> createState() => _TodoListState();
|
}
|
class _TodoListState extends State<TodoList> {
|
static const _appTitle = 'todo list';
|
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.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.