Skip to content
Snippets Groups Projects
discoveries_card.dart 1.98 KiB
import 'dart:convert';

import 'package:flutter/material.dart';

import '../../../models/discoveries.dart';
import '../../../network/scrobbles_api.dart';
import '../../../ui/widgets/error.dart';
import 'discoveries_content_artists.dart';
import 'discoveries_content_tracks.dart';

class ChartDiscoveriesCard extends StatelessWidget {
  const ChartDiscoveriesCard({super.key});

  @override
  Widget build(BuildContext context) {
    final int daysCount = 14;
    late Future<DiscoveriesData> futureTimeline = ScrobblesApi.fetchDiscoveries(daysCount);

    return FutureBuilder<DiscoveriesData>(
      future: futureTimeline,
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return ShowErrorWidget(message: '${snapshot.error}');
        }

        return Card(
          elevation: 2,
          shadowColor: Theme.of(context).colorScheme.shadow,
          color: Theme.of(context).colorScheme.surface,
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(8),
            ),
          ),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              children: [
                ChartDiscoveriesArtistsCardContent(
                  daysCount: daysCount,
                  chartData: snapshot.hasData
                      ? DiscoveriesData.fromJson(jsonDecode(snapshot.data.toString()))
                      : DiscoveriesData.createEmpty(),
                  isLoading: !snapshot.hasData,
                ),
                const SizedBox(height: 8),
                ChartDiscoveriesTracksCardContent(
                  daysCount: daysCount,
                  chartData: snapshot.hasData
                      ? DiscoveriesData.fromJson(jsonDecode(snapshot.data.toString()))
                      : DiscoveriesData.createEmpty(),
                  isLoading: !snapshot.hasData,
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}