Skip to content

Instantly share code, notes, and snippets.

@huynguyennovem
Created March 8, 2022 12:05
Show Gist options
  • Save huynguyennovem/2d98c007c6d2b788a258ba6f95c27630 to your computer and use it in GitHub Desktop.
Save huynguyennovem/2d98c007c6d2b788a258ba6f95c27630 to your computer and use it in GitHub Desktop.
VoiceOver only works if TextFormField validator value changed
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'),
),
],
),
);
}
}
@huynguyennovem
Copy link
Author

huynguyennovem commented Mar 8, 2022

  • Issue: flutter/flutter#99715
  • Flutter version: 2.10.3
  • Tested device: Mac OS Big Sur 11.6 M1
    Chrome 98.0.4758.109 (Official Build) (arm64)
  • Result: Work normally with second author's temporary solution
  • Replicable:
    • Yes (for first code sample)
    • No (for second code sample)

(Demo the second code sample)
https://user-images.githubusercontent.com/29337364/157238232-90af4153-eb1f-46c0-b487-16a3bf00eac4.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment