Skip to content

Instantly share code, notes, and snippets.

@vsomayaji
Created August 3, 2022 23:42
Show Gist options
  • Save vsomayaji/82c4c7df3e5b2d6941d58e766eaede38 to your computer and use it in GitHub Desktop.
Save vsomayaji/82c4c7df3e5b2d6941d58e766eaede38 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 (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 = 'Checkbox 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 _checkbox1Value = false;
bool _checkbox2Value = false;
bool _checkbox3Value = false;
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: <Widget>[
MergeSemantics(
child: Row(
children: <Widget>[
Checkbox(
value: _checkbox1Value,
onChanged: (bool? newValue) => setState(() => _checkbox1Value = newValue!),
),
const Text('Checkbox using MergeSemantics'),
],
),
),
Row(
children: <Widget>[
Semantics(
label: 'Checkbox using ExcludeSemantics',
child: Checkbox(
value: _checkbox2Value,
onChanged: (bool? newValue) => setState(() => _checkbox2Value = newValue!),
),
),
const ExcludeSemantics(child: Text('Checkbox using ExcludeSemantics')),
],
),
Row(
children: <Widget>[
Semantics(
// Use Semantics instead of MergeSemantics for the label, since the latter breaks the checkbox
// (https://github.com/flutter/flutter/issues/99719).
label: 'Checkbox using ExcludeSemantics and InkWell',
child: Checkbox(
value: _checkbox3Value,
onChanged: (bool? newValue) => setState(() => _checkbox3Value = newValue!),
),
),
ExcludeSemantics(
child: InkWell(
canRequestFocus: false, // Avoid a second tab stop.
onTap: () => setState(() => _checkbox3Value = !_checkbox3Value),
child: const Text('Checkbox using ExcludeSemantics and InkWell'),
),
),
],
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment