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'; /// Bottom sheet with "from gallery" and "take photo" actions for choosing a /// chat background. Returns the picked image bytes **as-is** — cropping is a /// separate, explicit step (see [cropChatBackgroundImage]) so the user isn't /// forced through a cropper after every pick. Returns `null` if the user /// cancelled everything. Future showChatBackgroundPickerSheet(BuildContext context) async { Uint8List? result; await showModalBottomSheet( 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 _pickRaw(FilePick.singleGalleryPick); if (bytes == null || !sheetContext.mounted) return; result = bytes; Navigator.of(sheetContext).pop(); }, ), ListTile( leading: const Icon(Icons.photo_camera_outlined), title: const Text('Foto aufnehmen'), onTap: () async { final bytes = await _pickRaw(FilePick.cameraPick); if (bytes == null || !sheetContext.mounted) return; result = bytes; Navigator.of(sheetContext).pop(); }, ), ], ), ), ), ); return result; } /// Opens the free-form cropper on [bytes] and returns the cropped result, or /// `null` if the user cancelled. Free aspect ratio since the background fills /// the whole screen. Future cropChatBackgroundImage( BuildContext context, Uint8List bytes, ) => Navigator.of(context).push( MaterialPageRoute( fullscreenDialog: true, builder: (_) => AvatarCropPage(imageBytes: bytes, aspectRatio: null), ), ); Future _pickRaw(Future Function() pick) async { final picked = await pick(); if (picked == null) return null; return picked.readAsBytes(); }