import 'dart:async'; import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; import '../l10n/app_localizations.dart'; import '../service/notifying.dart'; import '../service/permission_service.dart'; import '../service/storage.dart'; class SettingsPage extends StatefulWidget { static const routeName = '/settings'; const SettingsPage({super.key}); @override State createState() => _SettingsPageState(); } class _SettingsPageState extends State { bool storagePermissionIsGranted = false; final tileSpacing = 16.0; @override void initState() { PermissionService.storagePermissionStatus.then((value) { storagePermissionIsGranted = value.isGranted; if (context.mounted) setState(() {}); }); super.initState(); } @override Widget build(BuildContext context) { final titlePadding = Theme.of(context).listTileTheme.contentPadding!; checkStoragePermission(); return Scaffold( appBar: AppBar(title: Text(AppLocalizations.of(context)!.settings)), body: Center( child: SizedBox( width: MediaQuery.of(context).size.width * 0.9, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: titlePadding, child: Text( AppLocalizations.of(context)!.appData, style: Theme.of(context).textTheme.titleLarge, ), ), SizedBox(height: tileSpacing), ListTile( title: Text('Grant storage permisson'), subtitle: Text( 'For app-data settings to work, you need to grant the app permissions to manage internal storage.', ), onTap: () => PermissionService.requestStoragePermission .whenComplete(() => checkStoragePermission()), trailing: Icon(Icons.arrow_forward_ios_rounded), enabled: !storagePermissionIsGranted, ), SizedBox(height: tileSpacing), ListTile( title: Text(AppLocalizations.of(context)!.import), subtitle: Text('Import app-data from a json file.'), onTap: () => onActivateJsonImportPressed(), trailing: Icon(Icons.arrow_forward_ios_rounded), enabled: storagePermissionIsGranted, ), SizedBox(height: tileSpacing), ListTile( title: Text(AppLocalizations.of(context)!.export), subtitle: Text( 'Export app-data to a json file in the selected directory.', ), onTap: () => onActivateJsonExportPressed(), trailing: Icon(Icons.arrow_forward_ios_rounded), enabled: storagePermissionIsGranted, ), ], ), ), ), ); } void onActivateJsonExportPressed() async { if (!await PermissionService.storagePermissionStatus.isGranted) return; Storage.exportToJsonFile().then(showExportInfo); } void onActivateJsonImportPressed() async { if (!await PermissionService.storagePermissionStatus.isGranted) return; Storage.importFromJsonFile().then(showImportInfo); } Future checkStoragePermission() async { PermissionService.storagePermissionStatus.then((value) { storagePermissionIsGranted = value.isGranted; if (context.mounted && value.isGranted != storagePermissionIsGranted) { setState(() {}); } }); } void showExportInfo(bool success) => Notifying.showSnackbar( context, text: success ? AppLocalizations.of(context)!.exportSuccess : AppLocalizations.of(context)!.exportFailed, isError: !success, ); void showImportInfo(bool success) => Notifying.showSnackbar( context, text: success ? AppLocalizations.of(context)!.importSuccess : AppLocalizations.of(context)!.importFailed, isError: !success, ); }