fix: limit cover image upload to allowed 256kb size

This commit is contained in:
Kingkor Roy Tirtho 2023-09-10 21:39:16 +06:00
parent b9d5c70301
commit 1c50612559

View File

@ -1,4 +1,5 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -154,14 +155,30 @@ class PlaylistCreateDialog extends HookConsumerWidget {
child: ListView( child: ListView(
shrinkWrap: true, shrinkWrap: true,
children: [ children: [
Center( FormField<XFile?>(
initialValue: image.value,
onSaved: (newValue) {
image.value = newValue;
},
validator: (value) {
if (value == null) return null;
final file = File(value.path);
if (file.lengthSync() > 256000) {
return "Image size should be less than 256kb";
}
return null;
},
builder: (field) {
return Center(
child: Stack( child: Stack(
children: [ children: [
UniversalImage( UniversalImage(
path: image.value?.path ?? path: field.value?.path ??
TypeConversionUtils.image_X_UrlString( TypeConversionUtils.image_X_UrlString(
updatingPlaylist?.images, updatingPlaylist?.images,
placeholder: ImagePlaceholder.collection, placeholder:
ImagePlaceholder.collection,
), ),
height: 200, height: 200,
), ),
@ -171,22 +188,52 @@ class PlaylistCreateDialog extends HookConsumerWidget {
child: IconButton.filled( child: IconButton.filled(
icon: const Icon(SpotubeIcons.edit), icon: const Icon(SpotubeIcons.edit),
style: IconButton.styleFrom( style: IconButton.styleFrom(
backgroundColor: theme.colorScheme.surface, backgroundColor:
foregroundColor: theme.colorScheme.primary, theme.colorScheme.surface,
foregroundColor:
theme.colorScheme.primary,
elevation: 2, elevation: 2,
shadowColor: theme.colorScheme.onSurface, shadowColor: theme.colorScheme.onSurface,
), ),
onPressed: () async { onPressed: () async {
final imageFile = await ImagePicker() final imageFile = await ImagePicker()
.pickImage(source: ImageSource.gallery); .pickImage(
source: ImageSource.gallery);
image.value = imageFile ?? image.value; if (imageFile != null) {
field.didChange(imageFile);
field.validate();
field.save();
}
}, },
), ),
), ),
if (field.hasError)
Positioned(
bottom: 20,
left: 20,
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 4,
),
decoration: BoxDecoration(
color: theme.colorScheme.error,
borderRadius: BorderRadius.circular(4),
),
child: Text(
field.errorText ?? "",
style: theme.textTheme.bodyMedium!
.copyWith(
color: theme.colorScheme.onError,
),
),
),
),
], ],
), ),
), );
}),
const SizedBox(height: 10), const SizedBox(height: 10),
TextFormField( TextFormField(
controller: playlistName, controller: playlistName,
@ -203,6 +250,7 @@ class PlaylistCreateDialog extends HookConsumerWidget {
hintText: context.l10n.description, hintText: context.l10n.description,
), ),
keyboardType: TextInputType.multiline, keyboardType: TextInputType.multiline,
validator: ValidationBuilder().required().build(),
maxLines: 5, maxLines: 5,
), ),
const SizedBox(height: 10), const SizedBox(height: 10),