text stringlengths 1 372 |
|---|
command. this app allows a user to tap on a button |
to increase a counter. |
<code_start> |
import 'package:flutter/material.dart'; |
void main() => runApp(const MyApp()); |
class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
@override |
widget build(BuildContext context) { |
return const MaterialApp( |
title: 'counter app', |
home: MyHomePage(title: 'counter app home page'), |
); |
} |
} |
class MyHomePage extends StatefulWidget { |
const MyHomePage({super.key, required this.title}); |
final string title; |
@override |
State<MyHomePage> createState() => _MyHomePageState(); |
} |
class _MyHomePageState extends State<MyHomePage> { |
int _counter = 0; |
void _incrementCounter() { |
setState(() { |
_counter++; |
}); |
} |
@override |
widget build(BuildContext context) { |
return scaffold( |
appBar: AppBar( |
title: text(widget.title), |
), |
body: center( |
child: column( |
mainAxisAlignment: MainAxisAlignment.center, |
children: <widget>[ |
const text( |
'you have pushed the button this many times:', |
), |
text( |
'$_counter', |
style: Theme.of(context).textTheme.headlineMedium, |
), |
], |
), |
), |
floatingActionButton: FloatingActionButton( |
// provide a key to this button. this allows finding this |
// specific button inside the test suite, and tapping it. |
key: const key('increment'), |
onPressed: _incrementCounter, |
tooltip: 'increment', |
child: const Icon(Icons.add), |
), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
2. add the integration_test dependency |
next, use the integration_test and flutter_test packages |
to write integration tests. add these dependencies to the dev_dependencies |
section of the app’s pubspec.yaml file. |
<topic_end> |
<topic_start> |
3. create the test files |
create a new directory, integration_test, with an empty app_test.dart file: |
<topic_end> |
<topic_start> |
4. write the integration test |
now you can write tests. this involves three steps: |
<code_start> |
import 'package:flutter/material.dart'; |
import 'package:flutter_test/flutter_test.dart'; |
import 'package:integration_test/integration_test.dart'; |
import 'package:introduction/main.dart'; |
void main() { |
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
group('end-to-end test', () { |
testWidgets('tap on the floating action button, verify counter', |
(tester) async { |
// load app widget. |
await tester.pumpWidget(const MyApp()); |
// verify the counter starts at 0. |
expect(find.text('0'), findsOneWidget); |
// finds the floating action button to tap on. |
final fab = find.byKey(const key('increment')); |
// emulate a tap on the floating action button. |
await tester.tap(fab); |
// trigger a frame. |
await tester.pumpAndSettle(); |
// verify the counter increments by 1. |
expect(find.text('1'), findsOneWidget); |
}); |
}); |
} |
<code_end> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.