semantics_service.dart 2.05 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui' show TextDirection;

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

9
import 'semantics_event.dart' show AnnounceSemanticsEvent, Assertiveness, TooltipSemanticsEvent;
10

11
export 'dart:ui' show TextDirection;
12 13 14 15 16 17 18 19 20

/// 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 {
21
  // This class is not meant to be instantiated or extended; this constructor
22
  // prevents instantiation and extension.
23 24 25 26 27 28 29 30 31
  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.
32 33 34 35 36 37
  ///
  /// The assertiveness level of the announcement is determined by [assertiveness].
  /// Currently, this is only supported by the web engine and has no effect on
  /// other platforms. The default mode is [Assertiveness.polite].
  static Future<void> announce(String message, TextDirection textDirection, {Assertiveness assertiveness = Assertiveness.polite}) async {
    final AnnounceSemanticsEvent event = AnnounceSemanticsEvent(message, textDirection, assertiveness: assertiveness);
38 39
    await SystemChannels.accessibility.send(event.toMap());
  }
40 41

  /// Sends a semantic announcement of a tooltip.
42
  ///
43 44
  /// Currently only honored on Android. The contents of [message] will be
  /// read by TalkBack.
45
  static Future<void> tooltip(String message) async {
46
    final TooltipSemanticsEvent event = TooltipSemanticsEvent(message);
47 48
    await SystemChannels.accessibility.send(event.toMap());
  }
49
}