semantic_tree_extension 1.1.1
semantic_tree_extension: ^1.1.1 copied to clipboard
A Flutter DevTools extension to inspect and visualize the SemanticsTree in real time.
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:semantic_tree_extension/semantic_tree_extension.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SemanticTreeExtension.ensureInitialized();
runApp(const SemanticTreeExampleApp());
}
class SemanticTreeExampleApp extends StatelessWidget {
const SemanticTreeExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SemanticTree Extension Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
useMaterial3: true,
),
home: const DemoHomePage(),
);
}
}
class DemoHomePage extends StatefulWidget {
const DemoHomePage({super.key});
@override
State<DemoHomePage> createState() => _DemoHomePageState();
}
class _DemoHomePageState extends State<DemoHomePage> {
int _counter = 0;
bool _notificationsEnabled = true;
bool _acceptedTerms = false;
double _volume = 60;
final TextEditingController _nameController = TextEditingController();
@override
void dispose() {
_nameController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
debugDumpSemanticsTree();
return Scaffold(
appBar: AppBar(
title: const Text('SemanticTree Demo'),
actions: [
IconButton(
tooltip: 'Reset all controls',
icon: const Icon(Icons.restore),
onPressed: () {
setState(() {
_counter = 0;
_notificationsEnabled = true;
_acceptedTerms = false;
_volume = 60;
_nameController.clear();
});
},
),
],
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Semantics(
header: true,
child: Text(
'Interactive Controls',
style: Theme.of(context).textTheme.headlineSmall,
),
),
const SizedBox(height: 12),
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Expanded(
child: Semantics(
liveRegion: true,
child: Text(
'Counter value: $_counter',
style: Theme.of(context).textTheme.titleMedium,
),
),
),
ElevatedButton.icon(
onPressed: () => setState(() => _counter++),
icon: const Icon(Icons.add),
label: const Text('Increment'),
),
],
),
),
),
const SizedBox(height: 12),
SwitchListTile(
title: const Text('Enable notifications'),
subtitle: const Text('Receive accessibility alerts'),
value: _notificationsEnabled,
onChanged: (bool value) =>
setState(() => _notificationsEnabled = value),
),
CheckboxListTile(
title: const Text('Accept terms & conditions'),
value: _acceptedTerms,
onChanged: (bool? value) =>
setState(() => _acceptedTerms = value ?? false),
),
const SizedBox(height: 8),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Volume: ${_volume.round()}%'),
Slider(
value: _volume,
min: 0,
max: 100,
divisions: 10,
label: '${_volume.round()}%',
onChanged: (double value) => setState(() => _volume = value),
),
],
),
),
const SizedBox(height: 12),
TextField(
controller: _nameController,
decoration: const InputDecoration(
labelText: 'Display name',
hintText: 'Enter your name',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 20),
Semantics(
header: true,
child: Text(
'Custom Semantics Patterns',
style: Theme.of(context).textTheme.headlineSmall,
),
),
const SizedBox(height: 12),
MergeSemantics(
child: Card(
child: ListTile(
leading: const Icon(Icons.verified_user),
title: const Text('Merged Profile Card'),
subtitle: const Text(
'Title and subtitle are merged into a single SemanticsNode',
),
),
),
),
const SizedBox(height: 8),
ExcludeSemantics(
child: Card(
color: Theme.of(context).colorScheme.surfaceContainerHighest,
child: const Padding(
padding: EdgeInsets.all(16),
child: Text(
'Decorative banner (excluded from the SemanticsTree)',
),
),
),
),
const SizedBox(height: 20),
Semantics(
header: true,
child: Text(
'Intentional Accessibility Issues (Audit Demo)',
style: Theme.of(context).textTheme.headlineSmall,
),
),
const SizedBox(height: 12),
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Wrap(
spacing: 12,
runSpacing: 12,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
// Missing accessible label (no tooltip)
IconButton(
icon: const Icon(Icons.favorite_border),
onPressed: () {},
),
// Small touch target (< 48x48 dp)
Semantics(
label: 'Tiny action',
button: true,
child: GestureDetector(
onTap: () {},
child: const SizedBox(
width: 28,
height: 28,
child: Icon(Icons.close, size: 18),
),
),
),
// Redundant role word in label ("Save button" + isButton)
ElevatedButton(
onPressed: () {},
child: const Text('Save button'),
),
// Duplicate interactive labels ("Details" twice)
OutlinedButton(
onPressed: () {},
child: const Text('Details'),
),
OutlinedButton(
onPressed: () {},
child: const Text('Details'),
),
],
),
),
),
],
),
);
}
}