flutter_kit_audio 0.0.2
flutter_kit_audio: ^0.0.2 copied to clipboard
Private plugins / packages
example/lib/main.dart
import 'dart:async';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_kit_audio/flutter_kit_audio.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _flutterKitAudioPlugin = FlutterKitAudio();
int selectedItem = 0;
int selectedCustomItem = 0;
final TextEditingController _controller = TextEditingController();
List<FlutterKitAudioCustomSoundType> customList = customSoundList;
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion = await FlutterKitAudio.getPlatformVersion() ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
void showError(BuildContext context, PlatformException error) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(error.message as String),
behavior: SnackBarBehavior.floating, // 关键
margin: const EdgeInsets.only(
bottom: 200, // 👈 往上抬
left: 16,
right: 16,
),
duration: const Duration(seconds: 2),
),
);
}
@override
Widget build(BuildContext _) {
List<int> list = [];
if (Platform.isAndroid) {
list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
} else if (Platform.isIOS) {
list = [4095, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1022, 1104];
}
return MaterialApp(
home: Scaffold(
appBar: CupertinoNavigationBar(
middle: Text(_platformVersion),
),
body: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
return GestureDetector(
onTap: () {
setState(() {
FocusScope.of(context).unfocus();
});
},
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
///系统音效
const SizedBox(height: 20, child: Text('系统音效:')),
SizedBox(
height: 200,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
///选择器
SizedBox(
width: 140,
child: CupertinoPicker(
itemExtent: 30, // 每个选项的高度
onSelectedItemChanged: (index) => setState(() {
selectedItem = index;
_controller.text = '${list[index]}';
}),
children: List.generate(list.length, (index) {
return Text('${list[index]}', style: const TextStyle(fontSize: 20));
}),
),
),
///
TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.pink)),
onPressed: () async {
FocusScope.of(context).unfocus();
try {
await FlutterKitAudio.systemSound(soundId: list[selectedItem]);
} on PlatformException catch (e) {
showError(context, e);
}
},
child: const Text('播放音效')),
],
),
),
///输入系统音效
const SizedBox(height: 20, child: Text('输入系统音效:')),
SizedBox(
height: 80,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
///输入框
SizedBox(
width: 140,
child: TextField(
keyboardType: TextInputType.number,
controller: _controller, // 控制器,获取输入的文本
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: '${list[selectedItem]}',
),
onChanged: (value) {
_controller.text = value;
},
),
),
///
TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.pink)),
onPressed: () async {
if (int.tryParse(_controller.text, radix: 10) != null) {
FocusScope.of(context).unfocus();
try {
await FlutterKitAudio.systemSound(soundId: int.parse(_controller.text, radix: 10));
} on PlatformException catch (e) {
showError(context, e);
}
}
},
child: const Text('播放音效')),
],
),
),
///本地文件音效
const SizedBox(height: 20, child: Text('本地文件音效:')),
SizedBox(
height: 200,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
///选择器
SizedBox(
width: 140,
child: CupertinoPicker(
itemExtent: 30, // 每个选项的高度
onSelectedItemChanged: (index) => setState(() {
selectedCustomItem = index;
}),
children: List.generate(customList.length, (index) {
return Text(customList[index].name, style: const TextStyle(fontSize: 20));
}),
),
),
///
TextButton(
style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Colors.pink)),
onPressed: () async {
FocusScope.of(context).unfocus();
try {
await FlutterKitAudio.customSound(soundType: customList[selectedCustomItem]);
} on PlatformException catch (e) {
showError(context, e);
}
},
child: const Text('播放音效')),
],
),
),
],
),
),
);
}),
));
}
}