Created
March 8, 2022 12:05
-
-
Save huynguyennovem/2d98c007c6d2b788a258ba6f95c27630 to your computer and use it in GitHub Desktop.
VoiceOver only works if TextFormField validator value changed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/rendering.dart'; | |
void main() { | |
WidgetsFlutterBinding.ensureInitialized(); | |
// Auto-enable accessibility for our Blind and Low Vision customers (see | |
// https://docs.flutter.dev/development/accessibility-and-localization/accessibility#screen-readers). | |
RendererBinding.instance!.setSemanticsEnabled(true); | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
const appTitle = 'Form Validation Demo'; | |
return MaterialApp( | |
title: appTitle, | |
home: Scaffold( | |
appBar: AppBar( | |
title: const Text(appTitle), | |
), | |
body: const MyCustomForm(), | |
), | |
); | |
} | |
} | |
class MyCustomForm extends StatefulWidget { | |
const MyCustomForm({Key? key}) : super(key: key); | |
@override | |
State<MyCustomForm> createState() => _MyCustomFormState(); | |
} | |
class _MyCustomFormState extends State<MyCustomForm> { | |
final _formKey = GlobalKey<FormState>(); | |
bool _shouldAppendSpaceToErrorText = false; | |
@override | |
Widget build(BuildContext context) { | |
return Form( | |
key: _formKey, | |
child: Column( | |
children: <Widget>[ | |
TextFormField( | |
decoration: const InputDecoration( | |
helperText: ' ', // Required to announce errorText (https://github.com/flutter/flutter/issues/99715). | |
labelText: 'Username', | |
), | |
validator: (String? value) { | |
if (value == null || value.isEmpty) { | |
return 'Please enter your username' + (_shouldAppendSpaceToErrorText ? ' ' : ''); | |
} | |
return null; | |
}, | |
), | |
TextButton( | |
onPressed: () { | |
// Ensure that the errorText changes so that it is announced. | |
_shouldAppendSpaceToErrorText = !_shouldAppendSpaceToErrorText; | |
if (_formKey.currentState!.validate()) { | |
ScaffoldMessenger.of(context).showSnackBar( | |
const SnackBar(content: Text('Submitting...')), | |
); | |
} | |
}, | |
child: const Text('Submit'), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Chrome 98.0.4758.109 (Official Build) (arm64)
(Demo the second code sample)
https://user-images.githubusercontent.com/29337364/157238232-90af4153-eb1f-46c0-b487-16a3bf00eac4.mp4