simplified code across the app and consolidated duplicated helpers
This commit is contained in:
@@ -4,7 +4,6 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
|
||||
import '../api/errors/error_mapper.dart';
|
||||
import '../api/errors/permission_exception.dart';
|
||||
import 'confirm_dialog.dart';
|
||||
import 'info_dialog.dart';
|
||||
@@ -86,12 +85,45 @@ class FilePick {
|
||||
onConfirm: AppSettings.openAppSettings,
|
||||
).asDialog(context);
|
||||
|
||||
static void _showErrorDialog(BuildContext context, Object error) {
|
||||
final message = errorToUserMessage(error);
|
||||
final details = errorToTechnicalDetails(error);
|
||||
final body = details != null && details != message
|
||||
? '$message\n\n$details'
|
||||
: message;
|
||||
InfoDialog.show(context, body, copyable: true, title: 'Fehler');
|
||||
}
|
||||
static void _showErrorDialog(BuildContext context, Object error) =>
|
||||
InfoDialog.showError(context, error);
|
||||
}
|
||||
|
||||
/// "Aus Galerie wählen" / "Foto aufnehmen" tiles for an image-source sheet.
|
||||
/// The optional [transform] (e.g. a cropper) runs with the sheet still
|
||||
/// mounted; the sheet pops only once [onPicked] received the final bytes.
|
||||
List<Widget> imageSourceTiles(
|
||||
BuildContext sheetContext, {
|
||||
required void Function(Uint8List bytes) onPicked,
|
||||
Future<Uint8List?> Function(Uint8List bytes)? transform,
|
||||
}) {
|
||||
Widget tile(IconData icon, String title, Future<XFile?> Function() pick) =>
|
||||
ListTile(
|
||||
leading: Icon(icon),
|
||||
title: Text(title),
|
||||
onTap: () async {
|
||||
final picked = await pick();
|
||||
if (picked == null) return;
|
||||
Uint8List? bytes = await picked.readAsBytes();
|
||||
if (transform != null) {
|
||||
if (!sheetContext.mounted) return;
|
||||
bytes = await transform(bytes);
|
||||
}
|
||||
if (bytes == null || !sheetContext.mounted) return;
|
||||
onPicked(bytes);
|
||||
Navigator.of(sheetContext).pop();
|
||||
},
|
||||
);
|
||||
return [
|
||||
tile(
|
||||
Icons.photo_library_outlined,
|
||||
'Aus Galerie wählen',
|
||||
() => FilePick.singleGalleryPick(sheetContext, forCrop: true),
|
||||
),
|
||||
tile(
|
||||
Icons.photo_camera_outlined,
|
||||
'Foto aufnehmen',
|
||||
() => FilePick.cameraPick(sheetContext, forCrop: true),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user