semantics_service.dart 1.59 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:ui' show TextDirection;

import 'package:flutter/services.dart' show SystemChannels;

10
import 'semantics_event.dart' show AnnounceSemanticsEvent, TooltipSemanticsEvent;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29


/// Allows access to the platform's accessibility services.
///
/// Events sent by this service are handled by the platform-specific
/// accessibility bridge in Flutter's engine.
///
/// When possible, prefer using mechanisms like [Semantics] to implicitly
/// trigger announcements over using this event.
class SemanticsService {
  SemanticsService._();

  /// Sends a semantic announcement.
  ///
  /// This should be used for announcement that are not seamlessly announced by
  /// the system as a result of a UI state change.
  ///
  /// For example a camera application can use this method to make accessibility
  /// announcements regarding objects in the viewfinder.
30
  static Future<void> announce(String message, TextDirection textDirection) async {
31
    final AnnounceSemanticsEvent event = AnnounceSemanticsEvent(message, textDirection);
32 33
    await SystemChannels.accessibility.send(event.toMap());
  }
34 35

  /// Sends a semantic announcement of a tooltip.
36
  ///
37 38
  /// Currently only honored on Android. The contents of [message] will be
  /// read by TalkBack.
39
  static Future<void> tooltip(String message) async {
40
    final TooltipSemanticsEvent event = TooltipSemanticsEvent(message);
41 42
    await SystemChannels.accessibility.send(event.toMap());
  }
43
}