Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
4.2 kB
2
Indexable
Never
class MusicScreenState extends StatefulWidget {
  final PanelController _panelController = PanelController();

  MusicScreenState({Key? key}) : super(key: key);

  @override
  _MusicScreenState createState() => _MusicScreenState();
}

class _MusicScreenState extends State<MusicScreenState> {
  final _firestore = FirebaseFirestore.instance;
  final Map<String, ValueNotifier<bool>> _favoriteMap = {};
  LinkedHashSet<MusicInfo> _favoriteList = LinkedHashSet<MusicInfo>();
  final AudioPlayer _audioPlayer = AudioPlayerService.audioPlayer;
  SharedPreferences? prefs;
  bool _isPanelOpen = false;

  @override
  void initState() {
    super.initState();
    loadDatakeys();
  }

  Future<void> loadDatakeys() async {
    prefs = await SharedPreferences.getInstance();
  }

  

  @override
  void dispose() {
    _audioPlayer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _isPanelOpen
          ? null
          : AppBar(
              elevation: 0,
              backgroundColor: Colors.blue,
              title: const Text(
                "Music Library",
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.w600,
                  color: Colors.white,
                ),
              ),
            ),
      body: StreamBuilder<QuerySnapshot>(
        stream: _firestore.collection('musicCollection').snapshots(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          } else if (snapshot.hasData) {
            final musicDocs = snapshot.data!.docs;
            List<Widget> musicTiles = [];

            for (var doc in musicDocs) {
              String jsonString = doc.get('info');
              Map<String, dynamic> jsonData = _decodeJson(jsonString);

              if (jsonData.containsKey('info')) {
                List<dynamic> jsonList = jsonData['info'] as List<dynamic>;

                List<MusicInfo> musicInfos = jsonList.map((jsonMap) {
                  return MusicInfo.fromJson(jsonMap);
                }).toList();

               

                  return InkWell(
                    onTap: () {
                      // Handle onTap
                    },
                    child: Card(
                      elevation: 3,
                      margin: EdgeInsets.zero,
                      child: ListTile(
                        leading: Image.network(musicInfo.thumbnailLink),
                        title: Text(
                          musicInfo.heading,
                          style: const TextStyle(
                            fontSize: 20,
                            fontWeight: FontWeight.bold,
                            color: Colors.black,
                          ),
                        ),
                        subtitle: Text(
                          musicInfo.location,
                          style: const TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                            color: Colors.red,
                          ),
                        ),
                      
                      ),
                    ),
                  );
                }));

                musicTiles.add(const SizedBox(height: 20));
              }
            }

            return Padding(
              padding: const EdgeInsets.only(top: 5.0),
              child: ListView(children: [
                Column(
                  children: musicTiles,
                ),
              ]),
            );
          } else {
            return const Center(child: Text("Try Again After Some Time"));
          }
        },
      ),
    );
  }

  Map<String, dynamic> _decodeJson(String jsonString) {
    try {
      return json.decode(jsonString) as Map<String, dynamic>;
    } catch (e) {
      print("Error decoding JSON: $e");
      return {};
    }
  }
}
Leave a Comment