ความหมาย ตัวอ่านหน้าจอ และวิดเจ็ตที่เข้าถึงได้
ใส่คำอธิบายให้วิดเจ็ตด้วย API Semantics เพื่อรองรับผู้ใช้ TalkBack และ VoiceOver
ความหมาย ตัวอ่านหน้าจอ และวิดเจ็ตที่เข้าถึงได้ เป็นบทเรียน Flutter Mobile Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flutter Mobile Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Accessibility Matters
Millions of people rely on screen readers to use mobile apps. On Android the screen reader is TalkBack; on iOS it is VoiceOver. When enabled, the user swipes between elements and the phone speaks a description of each one out loud.
Flutter does not draw native widgets, so the OS cannot inspect your UI directly. Instead, Flutter builds a parallel semantics tree describing each element (its label, role, and state) and hands it to the platform's accessibility services.
- Good news: many built-in widgets (
Text,ElevatedButton,Checkbox) already populate this tree automatically. - Your job: fix the cases where the automatic description is missing, wrong, or confusing.
The Semantics Tree
Every visible widget can contribute a SemanticsNode to a tree that mirrors your widget tree. Each node carries properties such as label, value, hint, and flags like isButton or isChecked.
You rarely build this tree by hand. Instead you annotate widgets using the Semantics widget or the convenience properties exposed by existing widgets (for example Image's semanticLabel).
- The screen reader reads nodes in roughly top-to-bottom, left-to-right order.
- You can merge several widgets into one spoken node, or exclude purely decorative widgets entirely.
The Semantics Widget
The core tool is the Semantics widget. Wrap any widget with it and supply the properties the screen reader should announce.
Here an icon-only button has no visible text, so without a label TalkBack would just say "button". Adding label makes it meaningful.
label— what the element is ("Delete item").button: true— marks the node as tappable so the reader says "Delete item, button".
Semantics(
label: 'Delete item',
button: true,
child: IconButton(
icon: const Icon(Icons.delete),
onPressed: _deleteItem,
),
)Labeling Images and Icons
Images and icons are the most common accessibility gap: visually they convey meaning, but they carry no text. Provide a semanticLabel so the description is spoken.
If an image is purely decorative (adds no information), set excludeFromSemantics: true so the reader skips it instead of announcing a filename or "image".
Column(
children: [
// Meaningful image: describe it
Image.asset(
'assets/profile.png',
semanticLabel: 'Profile photo of Ada Lovelace',
),
// Decorative divider image: skip it
Image.asset(
'assets/flourish.png',
excludeFromSemantics: true,
),
],
)label, value, and hint
Three properties handle most situations, and they are not interchangeable:
label— the identity of the control ("Volume").value— its current state ("70 percent").hint— what happens on interaction ("Adjust to change volume").
A screen reader typically announces them as: label, value, hint. Keeping state in value (not baked into label) lets the reader re-announce only the changing part when the user interacts.
Semantics(
label: 'Volume',
value: '$volumePercent percent',
hint: 'Swipe up or down to adjust',
slider: true,
child: VolumeSlider(value: volumePercent),
)Merging Semantics
A card might contain an avatar, a name, and a subtitle as three separate widgets. By default the screen reader stops on each one, forcing three swipes for one logical item.
Wrap them in MergeSemantics to combine their descriptions into a single node. The reader then announces them together: "Ada Lovelace, Online".
Use merging when several widgets form one conceptual element. Do not merge unrelated controls, since merging also hides their individual interactions.
MergeSemantics(
child: Row(
children: const [
CircleAvatar(child: Text('A')),
SizedBox(width: 8),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Ada Lovelace'),
Text('Online'),
],
),
],
),
)Excluding Decorative Widgets
Sometimes a widget produces noise: a redundant decorative icon next to text, or a background graphic. Wrap it in ExcludeSemantics to drop it (and its subtree) from the semantics tree.
Here the chevron icon is purely visual; the row's text already tells the user everything, so we exclude the icon to avoid "chevron right" clutter.
Row(
children: [
const Text('Account settings'),
const Spacer(),
ExcludeSemantics(
child: const Icon(Icons.chevron_right),
),
],
)Live Announcements
Some changes are not tied to a focused widget — for example "Message sent" after a network call. To speak these immediately, use SemanticsService.announce.
Pass the message and the current TextDirection (so right-to-left languages are handled correctly). Use this sparingly; too many announcements overwhelm the user.
import 'package:flutter/semantics.dart';
Future<void> _onSendComplete(BuildContext context) async {
await SemanticsService.announce(
'Message sent',
Directionality.of(context),
);
}Touch Target Size
Accessibility is not only about screen readers. People with motor difficulties need targets large enough to tap reliably. The recommended minimum is roughly 48 x 48 logical pixels (matching Material's kMinInteractiveDimension).
Wrapping a small control with a tight SizedBox can shrink its hit area. Prefer IconButton (which enforces a minimum) or expand the target explicitly.
// Guarantee a comfortable tap target
ConstrainedBox(
constraints: const BoxConstraints(
minWidth: kMinInteractiveDimension,
minHeight: kMinInteractiveDimension,
),
child: InkWell(
onTap: _toggleFavorite,
child: const Icon(Icons.favorite_border),
),
)Building Accessible Labels in Dart
Labels are just strings, so you often build them with plain Dart logic before passing them to Semantics. Keeping this logic in a pure function makes it easy to test without any UI.
The snippet below is standalone Dart: it formats a button's accessible label from its state. Notice how the label describes identity while a separate state word communicates the current value clearly.
String favoriteLabel({required bool isFavorited, required String title}) {
final action = isFavorited ? 'Remove from favorites' : 'Add to favorites';
return '$action: $title';
}
void main() {
print(favoriteLabel(isFavorited: false, title: 'Dart Basics'));
print(favoriteLabel(isFavorited: true, title: 'Dart Basics'));
}Testing Your Semantics
You do not have to guess what the screen reader will say. Flutter offers several ways to verify:
- Turn on TalkBack or VoiceOver on a real device and swipe through your screen.
- Use the Accessibility Inspector in DevTools to view the live semantics tree.
- Write widget tests with the
SemanticsTester/matchesSemanticsmatcher to assert labels and flags.
Flutter also ships accessibility guideline checks you can run in tests, such as minimum tap-target size and text contrast.
testWidgets('delete button is labeled', (tester) async {
await tester.pumpWidget(const MyApp());
expect(
tester.getSemantics(find.byIcon(Icons.delete)),
matchesSemantics(label: 'Delete item', isButton: true),
);
});Quick Check
You have an icon-only IconButton showing a trash can. With TalkBack enabled, users only hear "button" and cannot tell what it does. What is the most appropriate fix?
Recap
You learned how to make Flutter widgets usable with TalkBack and VoiceOver:
- Flutter builds a semantics tree from your widgets; built-ins fill it in, but icons and images often need help.
- Use the
Semanticswidget withlabel(identity),value(state), andhint(interaction). - Give images a
semanticLabel, or setexcludeFromSemantics: truewhen decorative. MergeSemanticscombines several widgets into one spoken node;ExcludeSemanticsremoves noise.- Announce dynamic events with
SemanticsService.announce, and keep tap targets at least 48 x 48. - Verify with TalkBack/VoiceOver, the DevTools Accessibility Inspector, and
matchesSemanticsin widget tests.
Accessible apps reach more users and are simply better engineered.
คำถามที่พบบ่อย
บทเรียน “ความหมาย ตัวอ่านหน้าจอ และวิดเจ็ตที่เข้าถึงได้” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ความหมาย ตัวอ่านหน้าจอ และวิดเจ็ตที่เข้าถึงได้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flutter Mobile Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ความหมาย ตัวอ่านหน้าจอ และวิดเจ็ตที่เข้าถึงได้”
ใส่คำอธิบายให้วิดเจ็ตด้วย API Semantics เพื่อรองรับผู้ใช้ TalkBack และ VoiceOver คุณปฏิบัติ Flutter Mobile Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flutter Mobile Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flutter Mobile Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ความหมาย ตัวอ่านหน้าจอ และวิดเจ็ตที่เข้าถึงได้” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flutter Mobile Development นี้ได้ไหม
ได้ บทเรียน Flutter Mobile Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ไฟล์ ARB และกระบวนการแปลภาษาด้วย gen_l10n
- พหูพจน์ เพศ และการจัดรูปแบบข้อความ ICU
- เลย์เอาต์ RTL และการจัดการทิศทาง
- ความหมาย ตัวอ่านหน้าจอ และวิดเจ็ตที่เข้าถึงได้