Reputation: 3
below code is just to show AlertDialog when button is clicked in flutter and I'm searching the way to trigger device's alert sound when alert dialog pops up for both Android and iOS. I tried search pub.dev for package or other website for related topic but nothing found so far.
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Application',
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return TextButton(
onPressed: () {
showAlertDialog(context);
},
child: const Text('Press button'),
);
}
}
showAlertDialog(BuildContext context) {
Widget button = TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.pop(context);
},
);
AlertDialog alert = AlertDialog(
title: const Text("Alert title"),
content: const Text('Alert message.'),
actions: [
button,
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
Upvotes: 0
Views: 796
Reputation: 72
import 'package:flutter_ringtone_player/flutter_ringtone_player.dart';
// Call on 'onPressed' to play system default notification sound.
FlutterRingtonePlayer.playNotification();
// Call on 'onPressed' to play a specific sound.
FlutterRingtonePlayer.play(
android: AndroidSounds.notification,
ios: IosSounds.glass,
looping: true, // Android only - API >= 28
volume: 0.1, // Android only - API >= 28
asAlarm: false, // Android only - all APIs
);
Upvotes: 2
Reputation: 572
Use the audio player package to play alert audio when you open the alert box -- https://pub.dev/packages/audioplayers
Upvotes: 0