在Flutter中使用AlertDialog发出警报声音

xmq68pz9  于 2023-10-22  发布在  Flutter
关注(0)|答案(2)|浏览(175)

下面的代码只是为了显示AlertDialog当按钮被点击在Flutter,我正在寻找的方式来触发设备的警报声时,警报对话框弹出Android和iOS.我试着搜索pub.dev的包或其他网站的相关主题,但没有发现到目前为止。

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;
    },
  );
}
wixjitnu

wixjitnu1#

试试ringtone player package

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
);
rwqw0loc

rwqw0loc2#

使用音频播放器包在打开警报框时播放警报音频--https://pub.dev/packages/audioplayers

相关问题