Files
Client/lib/widget/avatar_actions_sheet.dart
T

117 lines
3.8 KiB
Dart

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'avatar_crop_page.dart';
import 'file_pick.dart';
/// Result of the user's choice inside [showAvatarActionsSheet]. The sheet
/// only collects intent + the (cropped) image bytes — the actual upload /
/// delete and any loading state are the caller's responsibility, so failures
/// surface in the screen that owns the avatar, not in a transient sheet.
sealed class AvatarSheetResult {
const AvatarSheetResult();
}
class AvatarUploadResult extends AvatarSheetResult {
final Uint8List bytes;
const AvatarUploadResult(this.bytes);
}
class AvatarRemoveResult extends AvatarSheetResult {
const AvatarRemoveResult();
}
/// Bottom sheet with "from gallery", "take photo" and optional "remove"
/// actions. The picker + 1:1 cropper run with the sheet still mounted, so a
/// cancelled pick simply returns the user to the sheet. The sheet only pops
/// once a concrete result exists (or never, if everything was cancelled).
Future<AvatarSheetResult?> showAvatarActionsSheet(
BuildContext context, {
required bool allowRemove,
}) async {
AvatarSheetResult? result;
await showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
showDragHandle: true,
useSafeArea: true,
builder: (sheetContext) => SafeArea(
child: SingleChildScrollView(
padding: EdgeInsets.only(
bottom: 16 + MediaQuery.viewInsetsOf(sheetContext).bottom,
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ListTile(
leading: const Icon(Icons.photo_library_outlined),
title: const Text('Aus Galerie wählen'),
onTap: () async {
final bytes = await _pickAndCrop(
sheetContext,
FilePick.singleGalleryPick,
);
if (bytes == null || !sheetContext.mounted) return;
result = AvatarUploadResult(bytes);
Navigator.of(sheetContext).pop();
},
),
ListTile(
leading: const Icon(Icons.photo_camera_outlined),
title: const Text('Foto aufnehmen'),
onTap: () async {
final bytes = await _pickAndCrop(
sheetContext,
FilePick.cameraPick,
);
if (bytes == null || !sheetContext.mounted) return;
result = AvatarUploadResult(bytes);
Navigator.of(sheetContext).pop();
},
),
if (allowRemove) ...[
const Divider(),
ListTile(
leading: Icon(
Icons.delete_outline,
color: Theme.of(sheetContext).colorScheme.error,
),
title: Text(
'Profilbild entfernen',
style: TextStyle(
color: Theme.of(sheetContext).colorScheme.error,
),
),
onTap: () {
result = const AvatarRemoveResult();
Navigator.of(sheetContext).pop();
},
),
],
],
),
),
),
);
return result;
}
Future<Uint8List?> _pickAndCrop(
BuildContext context,
Future<XFile?> Function() pick,
) async {
final picked = await pick();
if (picked == null) return null;
final bytes = await picked.readAsBytes();
if (!context.mounted) return null;
return Navigator.of(context).push<Uint8List>(
MaterialPageRoute(
fullscreenDialog: true,
builder: (_) => AvatarCropPage(imageBytes: bytes),
),
);
}