Skip to content

Instantly share code, notes, and snippets.

@vsomayaji
Last active February 4, 2022 18:37
Show Gist options
  • Save vsomayaji/4400d4a07c7d0e782a629ed952af7b07 to your computer and use it in GitHub Desktop.
Save vsomayaji/4400d4a07c7d0e782a629ed952af7b07 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
// Auto-enable accessibility for our Blind and Low Vision customers.
RendererBinding.instance?.setSemanticsEnabled(true);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyStatefulWidget(),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
@override
Widget build(BuildContext context) {
return Column(children: [
TextButton(
onPressed: () => showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) => AlertDialog(
title: const Text('This title is not announced'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Neither is this button'),
),
],
),
),
child: const Text('showDialog with issue'),
),
TextButton(
onPressed: () => showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) => AlertDialog(
// Workaround: Exclude the title from the semantics tree.
title:
const ExcludeSemantics(child: Text('This title is announced')),
actions: [
TextButton(
autofocus: true, // Workaround: Autofocus the node.
onPressed: () => Navigator.pop(context),
// Workaround: Add back the title to the semantics tree.
child: Semantics(
label: 'This title is announced',
child: const Text('So is this button'),
),
),
],
),
),
child: const Text('showDialog with workaround'),
),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment