From 6e252772d158fb2608effea848ab9028332cddd8 Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Fri, 24 Jul 2020 15:49:41 -0700 Subject: [PATCH] Add support for system alert sound (#62086) * Add SystemSoundType.alert (supported in the engine in flutter/engine#19970) * Play system alert sound when user tries to dismiss a non-dismissable modal barrier --- .../lib/src/services/system_sound.dart | 13 +++++++++ .../lib/src/widgets/modal_barrier.dart | 3 +++ .../test/widgets/modal_barrier_test.dart | 27 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/packages/flutter/lib/src/services/system_sound.dart b/packages/flutter/lib/src/services/system_sound.dart index 13517e0e71..536bc3ddc5 100644 --- a/packages/flutter/lib/src/services/system_sound.dart +++ b/packages/flutter/lib/src/services/system_sound.dart @@ -9,9 +9,19 @@ import 'dart:async'; import 'system_channels.dart'; /// A sound provided by the system. +/// +/// These sounds may be played with [SystemSound.play]. enum SystemSoundType { /// A short indication that a button was pressed. click, + + /// A short system alert sound indicating the need for user attention. + /// + /// Desktop platforms are the only platforms that support a system alert + /// sound, so on mobile platforms (Android, iOS), this will be ignored. The + /// web platform does not support playing any sounds, so this will be + /// ignored on the web as well. + alert, } /// Provides access to the library of short system specific sounds for common @@ -24,6 +34,9 @@ class SystemSound { /// Play the specified system sound. If that sound is not present on the /// system, the call is ignored. + /// + /// The web platform currently does not support playing sounds, so this call + /// will yield no behavior on that platform. static Future play(SystemSoundType type) async { await SystemChannels.platform.invokeMethod( 'SystemSound.play', diff --git a/packages/flutter/lib/src/widgets/modal_barrier.dart b/packages/flutter/lib/src/widgets/modal_barrier.dart index 816ee81e01..90438e7629 100644 --- a/packages/flutter/lib/src/widgets/modal_barrier.dart +++ b/packages/flutter/lib/src/widgets/modal_barrier.dart @@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; import 'basic.dart'; import 'container.dart'; @@ -103,6 +104,8 @@ class ModalBarrier extends StatelessWidget { onDismiss: () { if (dismissible) Navigator.maybePop(context); + else + SystemSound.play(SystemSoundType.alert); }, child: Semantics( label: semanticsDismissible ? semanticsLabel : null, diff --git a/packages/flutter/test/widgets/modal_barrier_test.dart b/packages/flutter/test/widgets/modal_barrier_test.dart index 6e1bc76728..dd22fcb0de 100644 --- a/packages/flutter/test/widgets/modal_barrier_test.dart +++ b/packages/flutter/test/widgets/modal_barrier_test.dart @@ -8,6 +8,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter/gestures.dart' show kSecondaryButton, PointerDeviceKind; @@ -165,6 +166,32 @@ void main() { hovered = false; }); + testWidgets('ModalBarrier plays system alert sound when user tries to dismiss it', (WidgetTester tester) async { + final List playedSystemSounds = []; + try { + SystemChannels.platform.setMockMethodCallHandler((MethodCall methodCall) async { + if (methodCall.method == 'SystemSound.play') + playedSystemSounds.add(methodCall.arguments as String); + }); + + final Widget subject = Stack( + textDirection: TextDirection.ltr, + children: [ + tapTarget, + const ModalBarrier(dismissible: false), + ], + ); + + await tester.pumpWidget(subject); + await tester.tap(find.text('target')); + await tester.pumpWidget(subject); + } finally { + SystemChannels.platform.setMockMethodCallHandler(null); + } + expect(playedSystemSounds, hasLength(1)); + expect(playedSystemSounds[0], SystemSoundType.alert.toString()); + }); + testWidgets('ModalBarrier pops the Navigator when dismissed by primary tap', (WidgetTester tester) async { final Map routes = { '/': (BuildContext context) => const FirstWidget(),