From 1893c43a9f1d4bbfec37c51a0afdd01439a899e6 Mon Sep 17 00:00:00 2001 From: toly <1981462002@qq.com> Date: Wed, 3 May 2023 08:32:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=89=E6=8B=A9=E9=9F=B3=E9=A2=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/muyu/models/audio_option.dart | 6 +++ lib/muyu/muyu_page.dart | 40 +++++++++++++++++-- lib/muyu/options/select_audio.dart | 62 ++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 lib/muyu/models/audio_option.dart create mode 100644 lib/muyu/options/select_audio.dart diff --git a/lib/muyu/models/audio_option.dart b/lib/muyu/models/audio_option.dart new file mode 100644 index 0000000..30152a0 --- /dev/null +++ b/lib/muyu/models/audio_option.dart @@ -0,0 +1,6 @@ +class AudioOption{ + final String name; + final String src; + + const AudioOption(this.name, this.src); +} \ No newline at end of file diff --git a/lib/muyu/muyu_page.dart b/lib/muyu/muyu_page.dart index 5cfe616..a0d71ab 100644 --- a/lib/muyu/muyu_page.dart +++ b/lib/muyu/muyu_page.dart @@ -4,6 +4,8 @@ import 'package:flame_audio/flame_audio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_first_station/muyu/animate_text.dart'; +import 'package:flutter_first_station/muyu/options/select_audio.dart'; +import 'models/audio_option.dart'; import 'models/image_option.dart'; import 'muyu_image.dart'; @@ -21,10 +23,18 @@ class MuyuPage extends StatefulWidget { class _MuyuPageState extends State { int _counter = 0; int _cruValue = 0; + int _activeImageIndex = 0; + int _activeAudioIndex = 0; final Random _random = Random(); + final List audioOptions = const [ + AudioOption('音效1', 'muyu_1.mp3'), + AudioOption('音效2', 'muyu_2.mp3'), + AudioOption('音效3', 'muyu_3.mp3'), + ]; + final List imageOptions = const [ ImageOption('基础版', 'assets/images/muyu.png', 1, 3), ImageOption('尊享版', 'assets/images/muyu2.png', 3, 6), @@ -79,7 +89,18 @@ class _MuyuPageState extends State { void _toHistory() {} - void _onTapSwitchAudio() {} + void _onTapSwitchAudio() { + showCupertinoModalPopup( + context: context, + builder: (BuildContext context) { + return AudioOptionPanel( + audioOptions: audioOptions, + activeIndex: _activeAudioIndex, + onSelect: _onSelectAudio, + ); + }, + ); + } void _onTapSwitchImage() { showCupertinoModalPopup( @@ -107,15 +128,26 @@ class _MuyuPageState extends State { int get knockValue { int min = imageOptions[_activeImageIndex].min; int max = imageOptions[_activeImageIndex].max; - return min + _random.nextInt(max+1 - min); + return min + _random.nextInt(max + 1 - min); } - void _onSelectImage(int value) { Navigator.of(context).pop(); - if(value == _activeImageIndex) return; + if (value == _activeImageIndex) return; setState(() { _activeImageIndex = value; }); } + + String get activeAudio => audioOptions[_activeAudioIndex].src; + + void _onSelectAudio(int value) async{ + Navigator.of(context).pop(); + if (value == _activeAudioIndex) return; + _activeAudioIndex = value; + pool = await FlameAudio.createPool( + activeAudio, + maxPlayers: 1, + ); + } } diff --git a/lib/muyu/options/select_audio.dart b/lib/muyu/options/select_audio.dart new file mode 100644 index 0000000..221ab68 --- /dev/null +++ b/lib/muyu/options/select_audio.dart @@ -0,0 +1,62 @@ +import 'package:flame_audio/flame_audio.dart'; +import 'package:flutter/material.dart'; + +import '../models/audio_option.dart'; + +class AudioOptionPanel extends StatelessWidget { + final List audioOptions; + final ValueChanged onSelect; + final int activeIndex; + + const AudioOptionPanel({ + Key? key, + required this.audioOptions, + required this.activeIndex, + required this.onSelect, + }) : super(key: key); + + @override + Widget build(BuildContext context) { + const TextStyle labelStyle = + TextStyle(fontSize: 16, fontWeight: FontWeight.bold); + return Material( + child: SizedBox( + height: 300, + child: Column( + children: [ + Container( + height: 46, + alignment: Alignment.center, + child: const Text( + "选择音效", + style: labelStyle, + )), + ...List.generate(audioOptions.length, _buildByIndex) + ], + ), + ), + ); + } + + Widget _buildByIndex(int index) { + bool active = index == activeIndex; + return ListTile( + selected: active, + onTap: () => onSelect(index), + title: Text(audioOptions[index].name), + trailing: IconButton( + splashRadius: 20, + onPressed: ()=>_tempPlay(audioOptions[index].src), + icon: const Icon( + Icons.record_voice_over_rounded, + color: Colors.blue, + ), + ), + ); + } + + void _tempPlay(String src) async{ + AudioPool pool = await FlameAudio.createPool(src, maxPlayers: 1); + pool.start(); + } +}