mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
## Description This PR adds a label to the form field inside TextButton use case page (for the A11y assessments app). It also customises the error message. ## Related Issue Fixes [[VPAT][A11y][a11y-app] fix form field in text button a11y assessment to have proper error message](https://github.com/flutter/flutter/issues/173007) ## Tests Adds 1 test.
81 lines
2.6 KiB
Dart
81 lines
2.6 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:flutter/material.dart';
|
|
import '../utils.dart';
|
|
import 'use_cases.dart';
|
|
|
|
class TextButtonUseCase extends UseCase {
|
|
TextButtonUseCase() : super(useCaseCategory: UseCaseCategory.core);
|
|
|
|
@override
|
|
String get name => 'TextButton';
|
|
|
|
@override
|
|
String get route => '/text-button';
|
|
|
|
@override
|
|
Widget build(BuildContext context) => const MainWidget();
|
|
}
|
|
|
|
class MainWidget extends StatefulWidget {
|
|
const MainWidget({super.key});
|
|
|
|
@override
|
|
State<MainWidget> createState() => MainWidgetState();
|
|
}
|
|
|
|
class MainWidgetState extends State<MainWidget> {
|
|
String pageTitle = getUseCaseName(TextButtonUseCase());
|
|
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
|
static const String fieldLabel = 'City';
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
title: Semantics(headingLevel: 1, child: Text('$pageTitle Demo')),
|
|
),
|
|
body: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
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;
|
|
},
|
|
errorBuilder: (BuildContext context, String errorText) {
|
|
return Text(errorText, semanticsLabel: '$errorText in $fieldLabel');
|
|
},
|
|
decoration: const InputDecoration(labelText: fieldLabel),
|
|
),
|
|
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'),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|