Skip to content
Snippets Groups Projects
statistics_card.dart 1.38 KiB
import 'dart:convert';

import 'package:flutter/material.dart';

import '../../../models/statistics.dart';
import '../../../network/scrobbles_api.dart';
import '../../../ui/widgets/error.dart';
import '../../../ui/widgets/main_screen/statistics_content.dart';

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

  @override
  Widget build(BuildContext context) {
    late Future<StatisticsData> futureStatistics = ScrobblesApi.fetchStatistics();

    return FutureBuilder<StatisticsData>(
      future: futureStatistics,
      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.primary,
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(12),
            ),
          ),
          child: Padding(
            padding: const EdgeInsets.all(12.0),
            child: StatisticsContent(
              statistics: snapshot.hasData
                  ? StatisticsData.fromJson(jsonDecode(snapshot.data.toString()))
                  : StatisticsData.createEmpty(),
              isLoading: !snapshot.hasData,
            ),
          ),
        );
      },
    );
  }
}