Loading...
Loading...
Implement a component-level test using `WidgetTester` to verify UI rendering and user interactions (tapping, scrolling, entering text). Use when validating that a specific widget displays correct data and responds to events as expected.
npx skill4agent add flutter/skills flutter-add-widget-testflutter_testdev_dependenciespubspec.yamltest/_test.dartwidget_test.dartflutter_testWidgetTestertestWidgets()Finderfind.text('Submit')find.byType(TextField)find.byKey(Key('submit_btn'))MatcherFinderfindsOneWidgetfindsNothingfindsNWidgets(2)matchesGoldenFiletestWidgets('description', (WidgetTester tester) async { ... })await tester.pumpWidget(MyWidget())MaterialAppDirectionalityFinderexpect(finder, matcher)await tester.tap(buttonFinder)await tester.pump()await tester.pumpAndSettle()expect()flutter test test/your_test_file_test.dartawait tester.pumpWidget()expect()await tester.tap(finder)await tester.pump()await tester.drag(finder, Offset(500, 0))await tester.pumpAndSettle()await tester.enterText(textFieldFinder, 'Input string')await tester.scrollUntilVisible(itemFinder, 500.0, scrollable: listFinder)lib/todo_list.dartimport 'package:flutter/material.dart';
class TodoList extends StatefulWidget {
const TodoList({super.key});
State<TodoList> createState() => _TodoListState();
}
class _TodoListState extends State<TodoList> {
final todos = <String>[];
final controller = TextEditingController();
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
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: (_) => setState(() => todos.removeAt(index)),
child: ListTile(title: Text(todo)),
);
},
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
todos.add(controller.text);
controller.clear();
});
},
child: const Icon(Icons.add),
),
),
);
}
}test/todo_list_test.dartimport 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/todo_list.dart';
void main() {
testWidgets('Add and remove a todo item', (WidgetTester tester) async {
// 1. Build the widget
await tester.pumpWidget(const TodoList());
// 2. Verify initial state
expect(find.byType(ListTile), findsNothing);
// 3. Enter text into the TextField
await tester.enterText(find.byType(TextField), 'Buy groceries');
// 4. Tap the add button
await tester.tap(find.byType(FloatingActionButton));
// 5. Rebuild the widget to reflect the new state
await tester.pump();
// 6. Verify the item was added
expect(find.text('Buy groceries'), findsOneWidget);
// 7. Swipe the item to dismiss it
await tester.drag(find.byType(Dismissible), const Offset(500, 0));
// 8. Build the widget until the dismiss animation ends
await tester.pumpAndSettle();
// 9. Verify the item was removed
expect(find.text('Buy groceries'), findsNothing);
});
}