Skip to content
Snippets Groups Projects
Select Git revision
  • ae63e6b1b907335bf1110205359c37841394efb8
  • master default protected
  • 82-reword-settings
  • 67-improve-app-metadata
  • 54-improve-discoveries-page
  • 7-add-lastfm-link
  • Release_0.9.2_79 protected
  • Release_0.9.1_78 protected
  • Release_0.9.0_77 protected
  • Release_0.8.4_76 protected
  • Release_0.8.3_75 protected
  • Release_0.8.2_74 protected
  • Release_0.8.1_73 protected
  • Release_0.8.0_72 protected
  • Release_0.7.0_71 protected
  • Release_0.6.0_70 protected
  • Release_0.5.0_69 protected
  • Release_0.4.2_68 protected
  • Release_0.4.1_67 protected
  • Release_0.4.0_66 protected
  • Release_0.3.1_65 protected
  • Release_0.3.0_64 protected
  • Release_0.2.2_63 protected
  • Release_0.2.1_62 protected
  • Release_0.2.0_61 protected
  • Release_0.1.2_60 protected
26 results

statistics_global.dart

Blame
  • statistics_global.dart 2.02 KiB
    import 'dart:convert';
    
    import 'package:easy_localization/easy_localization.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_bloc/flutter_bloc.dart';
    
    import 'package:scrobbles/cubit/data_statistics_global_cubit.dart';
    import 'package:scrobbles/models/statistics_global.dart';
    import 'package:scrobbles/network/scrobbles.dart';
    import 'package:scrobbles/ui/widgets/card_content.dart';
    import 'package:scrobbles/ui/widgets/content/statistics_global.dart';
    import 'package:scrobbles/ui/widgets/error.dart';
    
    class CardStatisticsGlobal extends StatelessWidget {
      const CardStatisticsGlobal({super.key});
    
      @override
      Widget build(BuildContext context) {
        return BlocBuilder<DataStatisticsGlobalCubit, DataStatisticsGlobalState>(
          builder: (BuildContext context, DataStatisticsGlobalState state) {
            return CardContent(
              color: Theme.of(context).colorScheme.primary,
              title: 'global_statistics'.tr(),
              loader: updateStatisticsGlobal(),
              content: ContentStatisticsGlobal(
                statistics:
                    StatisticsGlobalData.fromJson(jsonDecode(state.statisticsGlobal.toString())),
                isLoading: false,
              ),
            );
          },
        );
      }
    
      Widget updateStatisticsGlobal() {
        final Widget loading = const Text('⏳');
        final Widget done = const Text('');
    
        late Future<StatisticsGlobalData> futureStatisticsGlobal =
            ScrobblesApi.fetchGlobalStatistics();
    
        return BlocBuilder<DataStatisticsGlobalCubit, DataStatisticsGlobalState>(
          builder: (BuildContext context, DataStatisticsGlobalState dataState) {
            return FutureBuilder<StatisticsGlobalData>(
              future: futureStatisticsGlobal,
              builder: (context, snapshot) {
                if (snapshot.hasError) {
                  return ShowErrorWidget(message: '${snapshot.error}');
                }
    
                BlocProvider.of<DataStatisticsGlobalCubit>(context).update(snapshot.data);
    
                return !snapshot.hasData ? loading : done;
              },
            );
          },
        );
      }
    }