From 1637aaadfbb8471fa8bb51b3ebf686459be5dfb9 Mon Sep 17 00:00:00 2001 From: Lucas Delanier Date: Sun, 30 Jul 2023 00:38:53 +0200 Subject: [PATCH] fix playing song --- Sources/justMUSIC/android/build.gradle | 2 +- .../lib/components/music_list_component.dart | 6 ++-- .../lib/components/play_button_component.dart | 21 +++++------- .../lib/screens/search_song_screen.dart | 33 +++++++++++++++++-- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/Sources/justMUSIC/android/build.gradle b/Sources/justMUSIC/android/build.gradle index 83ae220..3cdaac9 100644 --- a/Sources/justMUSIC/android/build.gradle +++ b/Sources/justMUSIC/android/build.gradle @@ -26,6 +26,6 @@ subprojects { project.evaluationDependsOn(':app') } -task clean(type: Delete) { +tasks.register("clean", Delete) { delete rootProject.buildDir } diff --git a/Sources/justMUSIC/lib/components/music_list_component.dart b/Sources/justMUSIC/lib/components/music_list_component.dart index 82cea64..9a777da 100644 --- a/Sources/justMUSIC/lib/components/music_list_component.dart +++ b/Sources/justMUSIC/lib/components/music_list_component.dart @@ -8,12 +8,12 @@ class MusicListComponent extends StatelessWidget { final Music music; final bool playing; final int index; - final Function(int) selectMusic; + final Function(int) callback; const MusicListComponent({ Key? key, required this.music, required this.playing, - required this.selectMusic, + required this.callback, required this.index, }) : super(key: key); @@ -99,7 +99,7 @@ class MusicListComponent extends StatelessWidget { music.previewUrl != null ? PlayButtonComponent( music: music, - callback: selectMusic, + callback: callback, playing: playing, index: index, ) diff --git a/Sources/justMUSIC/lib/components/play_button_component.dart b/Sources/justMUSIC/lib/components/play_button_component.dart index 3b1f77a..9c4ebb0 100644 --- a/Sources/justMUSIC/lib/components/play_button_component.dart +++ b/Sources/justMUSIC/lib/components/play_button_component.dart @@ -3,6 +3,7 @@ import 'package:flutter/Material.dart'; import 'package:flutter_animated_play_button/flutter_animated_play_button.dart'; import 'package:ionicons/ionicons.dart'; +import '../main.dart'; import '../model/Music.dart'; class PlayButtonComponent extends StatefulWidget { @@ -24,29 +25,21 @@ class PlayButtonComponent extends StatefulWidget { class _PlayButtonComponentState extends State { final player = AudioPlayer(); - void switchStatePlaying() { - setState(() { - widget.playing = !widget.playing; - }); - widget.music.stopSong(); - } @override void initState() { - player.onPlayerComplete.listen((event) { - switchStatePlaying(); + MyApp.audioPlayer.onPlayerComplete.listen((event) { + widget.callback(widget.index); }); super.initState(); } @override Widget build(BuildContext context) { - if (widget.playing) { - widget.music.playSong(); - } else {} return !widget.playing ? GestureDetector( onTap: () { + widget.music.playSong(); widget.callback(widget.index); }, child: Container( @@ -59,7 +52,11 @@ class _PlayButtonComponentState extends State { )), ) : GestureDetector( - onTap: switchStatePlaying, + onTap: () { + widget.music.stopSong(); + + widget.callback(widget.index); + }, child: Container( width: 30, height: 30, diff --git a/Sources/justMUSIC/lib/screens/search_song_screen.dart b/Sources/justMUSIC/lib/screens/search_song_screen.dart index 30b1757..1042b6a 100644 --- a/Sources/justMUSIC/lib/screens/search_song_screen.dart +++ b/Sources/justMUSIC/lib/screens/search_song_screen.dart @@ -20,6 +20,7 @@ class SearchSongScreen extends StatefulWidget { class _SearchSongScreenState extends State { final ScrollController _scrollController = ScrollController(); final TextEditingController _textEditingController = TextEditingController(); + int? playingIndex; Future resetFullScreen() async { await SystemChannels.platform.invokeMethod( @@ -62,6 +63,18 @@ class _SearchSongScreenState extends State { List filteredData = []; + void playMusic(int index) { + if (playingIndex == index) { + setState(() { + playingIndex = null; + }); + } else { + setState(() { + playingIndex = index; + }); + } + } + @override Widget build(BuildContext context) { double screenHeight = MediaQuery.of(context).size.height; @@ -104,7 +117,7 @@ class _SearchSongScreenState extends State { controller: _textEditingController, keyboardAppearance: Brightness.dark, onEditingComplete: resetFullScreen, - onChanged: (value) async { + onSubmitted: (value) async { if (_textEditingController.text.isEmpty) { } else if (value == " ") { print("popular"); @@ -155,9 +168,25 @@ class _SearchSongScreenState extends State { controller: _scrollController, itemCount: filteredData.length, itemBuilder: (context, index) { + if (playingIndex == index) { + return Padding( + padding: const EdgeInsets.symmetric(horizontal: 20), + child: MusicListComponent( + music: filteredData[index], + playing: true, + callback: playMusic, + index: index, + ), + ); + } return Padding( padding: const EdgeInsets.symmetric(horizontal: 20), - child: MusicListComponent(music: filteredData[index]), + child: MusicListComponent( + music: filteredData[index], + playing: false, + callback: playMusic, + index: index, + ), ); }), ))