bugfix(image): Перенос хранения картинок из бд в папку приложения
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
@@ -152,7 +153,7 @@ class CollectionDetailScreen extends StatelessWidget {
|
||||
condition: collection.image != null,
|
||||
builder:
|
||||
(context) => ClipOval(
|
||||
child: Image.memory(collection.image!, fit: BoxFit.cover),
|
||||
child: Image.file(File(collection.image!), fit: BoxFit.cover),
|
||||
),
|
||||
fallback:
|
||||
(context) => Center(
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -258,7 +260,7 @@ class _Collection extends StatelessWidget {
|
||||
condition: collection.image != null,
|
||||
builder:
|
||||
(context) => ClipOval(
|
||||
child: Image.memory(collection.image!, fit: BoxFit.cover),
|
||||
child: Image.file(File(collection.image!), fit: BoxFit.cover),
|
||||
),
|
||||
fallback:
|
||||
(context) => Center(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -172,7 +173,7 @@ class CollectionCard extends StatelessWidget {
|
||||
condition: collection.image != null,
|
||||
builder:
|
||||
(context) => ClipOval(
|
||||
child: Image.memory(collection.image!, fit: BoxFit.cover),
|
||||
child: Image.file(File(collection.image!), fit: BoxFit.cover),
|
||||
),
|
||||
fallback:
|
||||
(context) => Center(
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_slidable/flutter_slidable.dart';
|
||||
@@ -181,7 +183,7 @@ class _TicketCardState extends State<TicketCard> {
|
||||
dimension: 64.r,
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8).r,
|
||||
child: Image.memory(imageBytes!, fit: BoxFit.cover),
|
||||
child: Image.file(File(imageBytes!), fit: BoxFit.cover),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -3,6 +3,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/utils.dart';
|
||||
@@ -21,6 +22,7 @@ import 'package:remever/screens/create_card/widgets/crud_ticket.dart';
|
||||
import 'package:remever/services/tickets/tickets_interface.dart';
|
||||
import 'package:remever/widgets/debug/app_debug.dart';
|
||||
import 'package:remever/widgets/primary_button.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
@RoutePage()
|
||||
class CreateScreen extends StatefulWidget {
|
||||
@@ -50,13 +52,27 @@ class _CreateScreenState extends State<CreateScreen> {
|
||||
showErrorToast('Не удалось получить путь к файлу');
|
||||
return;
|
||||
}
|
||||
final file = File(filePath);
|
||||
final bytes = await file.readAsBytes();
|
||||
|
||||
// Получаем директорию документов
|
||||
final Directory directory = await getApplicationDocumentsDirectory();
|
||||
final String ticketsDirPath = path.join(directory.path, 'tickets');
|
||||
final Directory ticketsDir = Directory(ticketsDirPath);
|
||||
|
||||
// Создаём директорию рекурсивно
|
||||
if (!(await ticketsDir.exists())) {
|
||||
await ticketsDir.create(recursive: true);
|
||||
}
|
||||
|
||||
final String fileName = path.basename(filePath);
|
||||
final String destinationPath = path.join(ticketsDirPath, fileName);
|
||||
|
||||
final copiedFile = await File(filePath).copy(destinationPath);
|
||||
|
||||
safeSetState(() {
|
||||
_dto =
|
||||
isQuestion
|
||||
? _dto.copyWith(questionImage: bytes)
|
||||
: _dto.copyWith(answerImage: bytes);
|
||||
? _dto.copyWith(questionImage: copiedFile.path)
|
||||
: _dto.copyWith(answerImage: copiedFile.path);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -284,7 +300,10 @@ class _CreateScreenState extends State<CreateScreen> {
|
||||
condition: _dto.collection?.image != null,
|
||||
builder:
|
||||
(context) => ClipOval(
|
||||
child: Image.memory(_dto.collection!.image!, fit: BoxFit.cover),
|
||||
child: Image.file(
|
||||
File(_dto.collection!.image!),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
fallback:
|
||||
(context) => Center(
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:readmore/readmore.dart';
|
||||
import 'package:remever/common/resources.dart';
|
||||
@@ -100,8 +102,8 @@ class CrudTicket extends StatelessWidget {
|
||||
builder: (context) {
|
||||
return ClipRRect(
|
||||
borderRadius: const BorderRadius.all(Radius.circular(8)).r,
|
||||
child: Image.memory(
|
||||
isQuestion ? dto.questionImage! : dto.answerImage!,
|
||||
child: Image.file(
|
||||
File(isQuestion ? dto.questionImage! : dto.answerImage!),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -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(),
|
||||
),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:remever/common/resources.dart';
|
||||
@@ -94,7 +95,7 @@ class InfoDialog extends StatelessWidget {
|
||||
condition: collection.image != null,
|
||||
builder:
|
||||
(context) => ClipOval(
|
||||
child: Image.memory(collection.image!, fit: BoxFit.cover),
|
||||
child: Image.file(File(collection.image!), fit: BoxFit.cover),
|
||||
),
|
||||
fallback:
|
||||
(context) => Center(
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:remever/common/functions.dart';
|
||||
@@ -217,7 +219,7 @@ class _ReplaceDialogState extends State<ReplaceDialog> {
|
||||
condition: collection.image != null,
|
||||
builder:
|
||||
(context) => ClipOval(
|
||||
child: Image.memory(collection.image!, fit: BoxFit.cover),
|
||||
child: Image.file(File(collection.image!), fit: BoxFit.cover),
|
||||
),
|
||||
fallback:
|
||||
(context) => Center(
|
||||
|
||||
Reference in New Issue
Block a user