diff --git a/dev/a11y_assessments/lib/use_cases/text_button.dart b/dev/a11y_assessments/lib/use_cases/text_button.dart index e39552af71..852a68f4dc 100644 --- a/dev/a11y_assessments/lib/use_cases/text_button.dart +++ b/dev/a11y_assessments/lib/use_cases/text_button.dart @@ -25,9 +25,8 @@ class MainWidget extends StatefulWidget { } class MainWidgetState extends State { - int _count = 0; - String pageTitle = getUseCaseName(TextButtonUseCase()); + final GlobalKey _formKey = GlobalKey(); @override Widget build(BuildContext context) { @@ -36,35 +35,34 @@ class MainWidgetState extends State { backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Semantics(headingLevel: 1, child: Text('$pageTitle Demo')), ), - body: Center( + body: Form( + key: _formKey, child: Column( - mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.start, children: [ - MergeSemantics( - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text('This is a TextButton:'), - TextButton( - onPressed: () { - setState(() { _count++; }); - }, - child: const Text('Action'), - ), - Text('Clicked $_count time(s).'), - ], - ), + TextFormField( + // The validator receives the text that the user has entered. + validator: (String? value) { + if (value == null || value.isEmpty) { + return 'Please enter some text'; + } + return null; + }, ), - const MergeSemantics( - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text('This is a disabled TextButton:'), - TextButton( - onPressed: null, - child: Text('Action Disabled'), - ), - ], + Padding( + padding: const EdgeInsets.symmetric(vertical: 16), + child: ElevatedButton( + onPressed: () { + // Validate returns true if the form is valid, or false otherwise. + if (_formKey.currentState!.validate()) { + // If the form is valid, display a snackbar. In the real world, + // this might also send a request to a server. + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Form submitted')), + ); + } + }, + child: const Text('Submit'), ), ), ], diff --git a/dev/a11y_assessments/test/text_button_test.dart b/dev/a11y_assessments/test/text_button_test.dart index e821a51ff3..4851a6adc0 100644 --- a/dev/a11y_assessments/test/text_button_test.dart +++ b/dev/a11y_assessments/test/text_button_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:a11y_assessments/use_cases/text_button.dart'; +import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'test_utils.dart'; @@ -10,22 +11,21 @@ import 'test_utils.dart'; void main() { testWidgets('text button can run', (WidgetTester tester) async { await pumpsUseCase(tester, TextButtonUseCase()); - expect(find.text('Action'), findsOneWidget); - expect(find.text('Action Disabled'), findsOneWidget); + expect(find.text('Submit'), findsOneWidget); }); - testWidgets('text button increments correctly when clicked', - (WidgetTester tester) async { + testWidgets('submit causes snackbar to show', (WidgetTester tester) async { await pumpsUseCase(tester, TextButtonUseCase()); + final Finder textFormField = find.byType(TextFormField); + final Finder submitButton = find.text('Submit'); - expect(find.text('Action'), findsOneWidget); - await tester.tap(find.text('Action')); - await tester.pumpAndSettle(); - expect(find.text('Clicked 1 time(s).'), findsOneWidget); + // Enter text in field and submit. + await tester.enterText(textFormField, 'test text'); + await tester.tap(submitButton); + await tester.pump(); - await tester.tap(find.text('Action')); - await tester.pumpAndSettle(); - expect(find.text('Clicked 2 time(s).'), findsOneWidget); + // Verify that the snackbar is visible. + expect(find.text('Form submitted'), findsOneWidget); }); testWidgets('text button demo page has one h1 tag', (WidgetTester tester) async {