diff --git a/packages/flutter/lib/src/semantics/semantics_service.dart b/packages/flutter/lib/src/semantics/semantics_service.dart index b78864f0d5..7086ee5716 100644 --- a/packages/flutter/lib/src/semantics/semantics_service.dart +++ b/packages/flutter/lib/src/semantics/semantics_service.dart @@ -7,6 +7,7 @@ library; import 'dart:ui' show TextDirection; +import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart' show SystemChannels; import 'semantics_event.dart' show AnnounceSemanticsEvent, Assertiveness, TooltipSemanticsEvent; @@ -33,6 +34,9 @@ abstract final class SemanticsService { /// Currently, this is only supported by the web engine and has no effect on /// other platforms. The default mode is [Assertiveness.polite]. /// + /// Not all platforms support announcements. Check to see if + /// [isAnnounceSupported] before calling this method. + /// /// ### Android /// Android has [deprecated announcement events][1] due to its disruptive /// behavior with TalkBack forcing it to clear its speech queue and speak the @@ -62,4 +66,12 @@ abstract final class SemanticsService { final TooltipSemanticsEvent event = TooltipSemanticsEvent(message); await SystemChannels.accessibility.send(event.toMap()); } + + /// Checks if announce is supported on the given platform. + /// + /// On Android the announce method is deprecated, therefore will return false. + /// On other platforms, this will return true. + static bool isAnnounceSupported() { + return defaultTargetPlatform != TargetPlatform.android; + } } diff --git a/packages/flutter/test/semantics/semantics_service_test.dart b/packages/flutter/test/semantics/semantics_service_test.dart index f741f316e5..59c8fab6ad 100644 --- a/packages/flutter/test/semantics/semantics_service_test.dart +++ b/packages/flutter/test/semantics/semantics_service_test.dart @@ -2,8 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:flutter/foundation.dart'; import 'package:flutter/semantics.dart'; -import 'package:flutter/services.dart' show SystemChannels; +import 'package:flutter/services.dart' show SystemChannels, TargetPlatform; import 'package:flutter_test/flutter_test.dart'; void main() { @@ -44,4 +45,12 @@ void main() { ]), ); }); + + for (final TargetPlatform platform in TargetPlatform.values) { + test('Announce not supported on Android. (platform=$platform)', () { + debugDefaultTargetPlatformOverride = platform; + expect(SemanticsService.isAnnounceSupported(), platform != TargetPlatform.android); + debugDefaultTargetPlatformOverride = null; + }); + } }