Skip to content

Instantly share code, notes, and snippets.

@abhaysood
Last active October 4, 2021 04:56
Show Gist options
  • Save abhaysood/b14e9378c32ec9b1b24778479f3fe347 to your computer and use it in GitHub Desktop.
Save abhaysood/b14e9378c32ec9b1b24778479f3fe347 to your computer and use it in GitHub Desktop.
Trigger semantics action in widget tests (medium_blog)
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
static const String label = "Hello World!";
void _showSnackbar(BuildContext context, String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
duration: const Duration(milliseconds: 1500),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Semantics(
child: const Text(label),
onTap: () => _showSnackbar(context, "onTap"),
onDidGainAccessibilityFocus: () =>
_showSnackbar(context, "onDidGainAccessibilityFocus"),
onDidLoseAccessibilityFocus: () =>
_showSnackbar(context, "onDidLoseAccessibilityFocus"),
),
ElevatedButton(
child: const Text("button"),
onPressed: () {},
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment