on_audio_room 1.0.1 on_audio_room: ^1.0.1 copied to clipboard
Flutter Plugin used to create a database for storage audio sections [Favorites, Internal Playlists, Most Played, etc...].
/*
=============
Author: Lucas Josino
Github: https://github.com/LucasPJS
Website: https://lucasjosino.com/
=============
Plugin/Id: on_audio_room#4
Homepage: https://github.com/LucasPJS/on_audio_room
Pub: https://pub.dev/packages/on_audio_room
License: https://github.com/LucasPJS/on_audio_room/blob/main/LICENSE
Copyright: © 2021, Lucas Josino. All rights reserved.
=============
*/
import 'package:flutter/material.dart';
import 'package:on_audio_query/on_audio_query.dart';
import 'package:on_audio_room/on_audio_room.dart';
import 'package:on_toast_widget/on_toast_widget.dart';
void main() {
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
//Toast animation controller, package: [on_toast_widget].
late AnimationController _controller =
AnimationController(vsync: this, duration: Duration(seconds: 1));
//A easy way to load images from [on_audio_query], just to test.
//For a real application use [queryDeviceInfo] from [on_audio_query].
Map<dynamic, dynamic> device = {"device_sdk": 29};
//Toast text and color.
String toastText = "";
Color toastColor = Colors.white;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("OnAudioRoomExample"),
actions: [
IconButton(
icon: Icon(Icons.favorite),
onPressed: () async => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => Favorites())),
)
],
),
body: Center(
child: Stack(
children: [
FutureBuilder<List<SongModel>>(
future: OnAudioQuery().querySongs(null, null, null, true),
builder: (context, item) {
if (item.data != null) {
List<SongModel> songs = item.data!;
return ListView.builder(
itemCount: songs.length,
itemBuilder: (context, index) {
return ListTile(
onTap: () async {
Map<dynamic, dynamic> favoritesMap = {
"last_data": songs[index].data,
"display_name": songs[index].displayName,
"id": songs[index].id,
"album": songs[index].album,
"album_id": songs[index].albumId,
"artist": songs[index].artist,
"artist_id": songs[index].artistId,
"date_added": songs[index].dateAdded,
"date_modified": songs[index].dateModified ?? 0,
"duration": songs[index].duration,
"title": songs[index].title,
"track": 0,
};
await OnAudioRoom()
.addTo(RoomType.FAVORITES, favoritesMap);
setState(() {
toastText =
"Song: ${songs[index].title} added to Favorites";
toastColor = Colors.green;
_controller.forward();
});
},
onLongPress: () async {
bool checkResult = await OnAudioRoom()
.checkIn(RoomType.FAVORITES, songs[index].id);
setState(() {
toastText = checkResult == true
? "Song exist in Favorites"
: "Song don't exist in Favorites";
toastColor =
checkResult == true ? Colors.green : Colors.red;
_controller.forward();
});
},
title: Text(songs[index].title),
subtitle: Text(songs[index].artist),
leading: QueryArtworkWidget(
id: songs[index].id,
artwork: songs[index].artwork,
type: ArtworkType.AUDIO,
deviceInfo: DeviceModel(device),
),
);
},
);
}
return CircularProgressIndicator();
},
),
OnToastWidget(
effectType: EffectType.SLIDE,
slidePositionType: SlidePositionType.BOTTOM,
controller: _controller,
child: Container(
height: 50,
child: Center(child: Text(toastText)),
color: toastColor,
),
)
],
),
),
);
}
}
class Favorites extends StatefulWidget {
const Favorites({Key? key}) : super(key: key);
@override
_FavoritesState createState() => _FavoritesState();
}
class _FavoritesState extends State<Favorites>
with SingleTickerProviderStateMixin {
late AnimationController _controller =
AnimationController(vsync: this, duration: Duration(seconds: 1));
Map<dynamic, dynamic> device = {"device_sdk": 29};
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("OnAudioRoomFavorites"),
),
body: Center(
child: Stack(
children: [
FutureBuilder<List<FavoritesEntity>>(
future: OnAudioRoom().queryAllFromFavorites(),
builder: (context, item) {
if (item.data != null) {
List<FavoritesEntity> songs = item.data!;
return ListView.builder(
itemCount: songs.length,
itemBuilder: (context, index) {
return ListTile(
onTap: () async {
await OnAudioRoom()
.deleteFrom(RoomType.FAVORITES, songs[index].id);
setState(() {
_controller.forward();
});
},
title: Text(songs[index].title),
subtitle: Text(songs[index].artist),
leading: QueryArtworkWidget(
id: songs[index].id,
artwork: songs[index].artwork,
type: ArtworkType.AUDIO,
deviceInfo: DeviceModel(device),
),
);
},
);
}
return CircularProgressIndicator();
},
),
OnToastWidget(
effectType: EffectType.SLIDE,
slidePositionType: SlidePositionType.BOTTOM,
controller: _controller,
child: Container(
height: 50,
child: Center(child: Text("Song removed from Favorites")),
color: Colors.lightBlueAccent,
),
)
],
),
),
);
}
}