bugfix(image): Перенос хранения картинок из бд в папку приложения
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:auto_route/auto_route.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:remever/common/functions.dart';
|
||||
import 'package:remever/common/resources.dart';
|
||||
import 'package:remever/common/widgets/bottom_safe_space.dart';
|
||||
@@ -24,6 +25,7 @@ import 'package:remever/services/collection/collections_interface.dart';
|
||||
import 'package:remever/widgets/primary_button.dart';
|
||||
|
||||
import '../../../components/extensions/state.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
@RoutePage()
|
||||
class CrudCollectionScreen extends StatefulWidget {
|
||||
@@ -58,10 +60,27 @@ class _CrudCollectionScreenState extends State<CrudCollectionScreen> {
|
||||
Future<void> _pickImage() async {
|
||||
final result = await FilePicker.platform.pickFiles();
|
||||
|
||||
if (result?.files.single.path case final String? path?) {
|
||||
if (result?.files.single.path case final String? originPath?) {
|
||||
try {
|
||||
final bytes = await File(path!).readAsBytes();
|
||||
_updateCollection(avatar: bytes);
|
||||
// Получаем директорию документов
|
||||
final Directory directory = await getApplicationDocumentsDirectory();
|
||||
final String collectionsDirPath = path.join(
|
||||
directory.path,
|
||||
'collections',
|
||||
);
|
||||
final Directory collectionsDir = Directory(collectionsDirPath);
|
||||
|
||||
// Создаём директорию рекурсивно
|
||||
if (!(await collectionsDir.exists())) {
|
||||
await collectionsDir.create(recursive: true);
|
||||
}
|
||||
|
||||
final String fileName = path.basename(originPath!);
|
||||
final String destinationPath = path.join(collectionsDirPath, fileName);
|
||||
|
||||
await File(originPath).copy(destinationPath);
|
||||
|
||||
_updateCollection(avatar: destinationPath);
|
||||
} catch (e) {
|
||||
showErrorToast('Не удалось загрузить изображение');
|
||||
}
|
||||
@@ -74,7 +93,7 @@ class _CrudCollectionScreenState extends State<CrudCollectionScreen> {
|
||||
String? title,
|
||||
String? desc,
|
||||
bool? isPublic,
|
||||
Uint8List? avatar,
|
||||
String? avatar,
|
||||
}) {
|
||||
_collection = _collection.copyWith(
|
||||
title: title ?? _collection.title,
|
||||
@@ -389,8 +408,8 @@ class _CrudCollectionScreenState extends State<CrudCollectionScreen> {
|
||||
condition: _collection.avatar != null,
|
||||
builder:
|
||||
(_) => ClipOval(
|
||||
child: Image.memory(
|
||||
_collection.avatar!,
|
||||
child: Image.file(
|
||||
File(_collection.avatar!),
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => _buildPhotoPlaceholder(),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user