import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:settings_ui/settings_ui.dart'; import 'package:shared_preferences/shared_preferences.dart'; /// Allows setting the type of graph for certain data class GraphTypeSettingsView extends StatefulWidget { /// Allows setting the type of graph for certain data const GraphTypeSettingsView({super.key}); @override State createState() => _GraphTypeSettingsViewState(); } class _GraphTypeSettingsViewState extends State { var _yearly = 1; var _monthly = 2; @override void initState() { super.initState(); SharedPreferences.getInstance().then((prefs) { _yearly = prefs.getInt("yearlygraph") ?? 1; _monthly = prefs.getInt("monthlygraph") ?? 2; setState(() {}); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(AppLocalizations.of(context).graphType), ), body: SettingsList( applicationType: ApplicationType.both, darkTheme: SettingsThemeData( settingsListBackground: Theme.of(context).colorScheme.surface, titleTextColor: Theme.of(context).colorScheme.primary, ), sections: [ SettingsSection( tiles: [ SettingsTile.navigation( title: Text(AppLocalizations.of(context).yearly), value: Text( _yearly == 1 ? AppLocalizations.of(context).barChart : AppLocalizations.of(context).lineChart, ), onPressed: (c) { if (Platform.isIOS) { // iOS does not use Material widgets => no inkwell support showCupertinoModalPopup( context: context, builder: (ctx) => CupertinoActionSheet( title: Text(AppLocalizations.of(context).selectType), actions: [ CupertinoActionSheetAction( onPressed: () async { final s = await SharedPreferences.getInstance(); await s.setInt("yearlygraph", 1); _yearly = 1; if (!ctx.mounted) return; Navigator.of(ctx).pop(); setState(() {}); }, child: Text( AppLocalizations.of(context).barChart, textAlign: TextAlign.center, ), ), CupertinoActionSheetAction( onPressed: () async { final s = await SharedPreferences.getInstance(); await s.setInt("yearlygraph", 2); _yearly = 2; if (!ctx.mounted) return; Navigator.of(ctx).pop(); setState(() {}); }, child: Text( AppLocalizations.of(context).lineChart, textAlign: TextAlign.center, ), ), ], ), ); } else { showAdaptiveDialog( context: c, builder: (ctx) => AlertDialog.adaptive( title: Text(AppLocalizations.of(context).selectType), content: SizedBox( height: 80, child: Column( children: [ SizedBox( width: MediaQuery.of(ctx).size.width, child: InkWell( child: Padding( padding: const EdgeInsets.all(8), child: Text( AppLocalizations.of(context).barChart, textAlign: TextAlign.center, ), ), onTap: () async { final s = await SharedPreferences.getInstance(); await s.setInt("yearlygraph", 1); _yearly = 1; if (!ctx.mounted) return; Navigator.of(ctx).pop(); setState(() {}); }, ), ), SizedBox( width: MediaQuery.of(context).size.width, child: InkWell( child: Padding( padding: const EdgeInsets.all(8), child: Text( AppLocalizations.of(context).lineChart, textAlign: TextAlign.center, ), ), onTap: () async { final s = await SharedPreferences.getInstance(); await s.setInt("yearlygraph", 2); _yearly = 2; if (!ctx.mounted) return; Navigator.of(ctx).pop(); setState(() {}); }, ), ), ], ), ), ), ); } }, ), SettingsTile.navigation( title: Text(AppLocalizations.of(context).monthly), value: Text( _monthly == 1 ? AppLocalizations.of(context).barChart : AppLocalizations.of(context).lineChart, ), onPressed: (c) { if (Platform.isIOS) { // iOS does not use Material widgets => no inkwell support showCupertinoModalPopup( context: context, builder: (ctx) => CupertinoActionSheet( title: Text(AppLocalizations.of(context).selectType), actions: [ CupertinoActionSheetAction( onPressed: () async { final s = await SharedPreferences.getInstance(); await s.setInt("monthlygraph", 1); _monthly = 1; if (!ctx.mounted) return; Navigator.of(ctx).pop(); setState(() {}); }, child: Text( AppLocalizations.of(context).barChart, textAlign: TextAlign.center, ), ), CupertinoActionSheetAction( onPressed: () async { final s = await SharedPreferences.getInstance(); await s.setInt("monthlygraph", 2); _monthly = 2; if (!ctx.mounted) return; Navigator.of(ctx).pop(); setState(() {}); }, child: Text( AppLocalizations.of(context).lineChart, textAlign: TextAlign.center, ), ), ], ), ); } else { showAdaptiveDialog( context: c, builder: (ctx) => AlertDialog.adaptive( title: Text(AppLocalizations.of(context).selectType), content: SizedBox( height: 80, child: Column( children: [ SizedBox( width: MediaQuery.of(ctx).size.width, child: InkWell( child: Padding( padding: const EdgeInsets.all(8), child: Text( AppLocalizations.of(context).barChart, textAlign: TextAlign.center, ), ), onTap: () async { final s = await SharedPreferences.getInstance(); await s.setInt("monthlygraph", 1); _monthly = 1; if (!ctx.mounted) return; Navigator.of(ctx).pop(); setState(() {}); }, ), ), SizedBox( width: MediaQuery.of(context).size.width, child: InkWell( child: Padding( padding: const EdgeInsets.all(8), child: Text( AppLocalizations.of(context).lineChart, textAlign: TextAlign.center, ), ), onTap: () async { final s = await SharedPreferences.getInstance(); await s.setInt("monthlygraph", 2); _monthly = 2; if (!ctx.mounted) return; Navigator.of(ctx).pop(); setState(() {}); }, ), ), ], ), ), ), ); } }, ), ], ), ], ), ); } }