MVP II (Criação de conta funcional)

This commit is contained in:
Carlos Correia
2026-06-25 14:42:59 +01:00
parent fee538eebd
commit de0b39231e
2 changed files with 251 additions and 8 deletions

View File

@@ -1,4 +1,6 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import '../constants/item_categories.dart';
import '../theme/app_theme.dart';
@@ -661,6 +663,7 @@ class ItemDetailScreen extends StatelessWidget {
Widget build(BuildContext context) {
final cat = categoryById(item['categoria'] as String?);
final tags = List<String>.from(item['tags'] ?? []);
final notes = item['nota'] ?? item['notes'];
return Scaffold(
backgroundColor: AppColors.background,
@@ -783,6 +786,24 @@ class ItemDetailScreen extends StatelessWidget {
.toList(),
),
],
const SizedBox(height: 24),
if (notes != null && notes.toString().isNotEmpty) ...[
const Text('Notas', style: AppText.label),
const SizedBox(height: 8),
Container(
width: double.infinity,
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: AppColors.surface,
borderRadius: BorderRadius.circular(AppRadius.lg),
border: Border.all(color: AppColors.border),
),
child: Text(
notes.toString(),
style: AppText.body.copyWith(height: 1.4),
),
),
],
],
),
),
@@ -806,27 +827,126 @@ class EditItemScreen extends StatefulWidget {
class _EditItemScreenState extends State<EditItemScreen> {
late TextEditingController _nameController;
late TextEditingController _notesController;
ItemCategory? _selectedCategory;
final Set<String> _selectedTags = {};
final _imagePicker = ImagePicker();
XFile? _pickedImage;
String? _currentImageUrl;
bool _isLoading = false;
@override
void initState() {
super.initState();
_nameController = TextEditingController(text: widget.item['nome'] ?? '');
_notesController = TextEditingController(
text: widget.item['nota'] ?? widget.item['notes'] ?? '',
);
final catId = widget.item['categoria'] as String?;
if (catId != null) {
_selectedCategory = categoryById(catId);
}
_selectedTags.addAll(List<String>.from(widget.item['tags'] ?? []));
final images = widget.item['item_images'] as List?;
if (images != null && images.isNotEmpty) {
_currentImageUrl = images.first['image_url'] as String?;
}
}
@override
void dispose() {
_nameController.dispose();
_notesController.dispose();
super.dispose();
}
Future<void> _pickImage() async {
try {
final image = await _imagePicker.pickImage(
source: ImageSource.gallery,
maxWidth: 1080,
maxHeight: 1080,
imageQuality: 80,
);
if (image != null) {
setState(() => _pickedImage = image);
}
} catch (e) {
if (mounted) AppSnack.error(context, 'Erro ao selecionar foto: \$e');
}
}
Future<void> _takePhoto() async {
try {
final image = await _imagePicker.pickImage(
source: ImageSource.camera,
maxWidth: 1080,
maxHeight: 1080,
imageQuality: 80,
);
if (image != null) {
setState(() => _pickedImage = image);
}
} catch (e) {
if (mounted) AppSnack.error(context, 'Erro ao abrir câmara: \$e');
}
}
void _showImageOptions() {
showModalBottomSheet(
context: context,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(AppRadius.xl)),
),
builder: (ctx) => SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 20),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Alterar foto', style: AppText.h3),
const SizedBox(height: 20),
ListTile(
leading: const Icon(Icons.photo_library_outlined),
title: const Text('Galeria'),
onTap: () {
Navigator.pop(ctx);
_pickImage();
},
),
ListTile(
leading: const Icon(Icons.camera_alt_outlined),
title: const Text('Câmara'),
onTap: () {
Navigator.pop(ctx);
_takePhoto();
},
),
if (_currentImageUrl != null || _pickedImage != null)
ListTile(
leading: const Icon(
Icons.delete_outline,
color: AppColors.error,
),
title: Text(
'Remover foto',
style: TextStyle(color: AppColors.error),
),
onTap: () {
Navigator.pop(ctx);
setState(() {
_pickedImage = null;
_currentImageUrl = null;
});
},
),
],
),
),
),
);
}
Future<void> _save() async {
if (_nameController.text.trim().isEmpty) {
AppSnack.error(context, 'Nome não pode ser vazio');
@@ -840,8 +960,28 @@ class _EditItemScreenState extends State<EditItemScreen> {
'nome': _nameController.text.trim(),
'categoria': _selectedCategory?.id,
'tags': _selectedTags.toList(),
'nota': _notesController.text.trim().isEmpty
? null
: _notesController.text.trim(),
})
.eq('id', widget.item['id']);
if (_pickedImage != null) {
final file = File(_pickedImage!.path);
final fileName =
'item_${widget.item['id']}_${DateTime.now().millisecondsSinceEpoch}.jpg';
await Supabase.instance.client.storage
.from('avatars')
.upload(fileName, file);
final imageUrl = Supabase.instance.client.storage
.from('avatars')
.getPublicUrl(fileName);
await Supabase.instance.client.from('item_images').insert({
'item_id': widget.item['id'],
'image_url': imageUrl,
});
}
if (mounted) {
AppSnack.success(context, 'Item atualizado!');
Navigator.pop(context);
@@ -868,6 +1008,83 @@ class _EditItemScreenState extends State<EditItemScreen> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Foto do item
Center(
child: GestureDetector(
onTap: _showImageOptions,
child: Container(
width: 140,
height: 140,
decoration: BoxDecoration(
color: AppColors.surface,
borderRadius: BorderRadius.circular(AppRadius.lg),
border: Border.all(color: AppColors.border),
image: _pickedImage != null
? DecorationImage(
image: FileImage(File(_pickedImage!.path)),
fit: BoxFit.cover,
)
: _currentImageUrl != null
? DecorationImage(
image: NetworkImage(_currentImageUrl!),
fit: BoxFit.cover,
)
: null,
),
child: _pickedImage == null && _currentImageUrl == null
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.add_photo_alternate_outlined,
size: 40,
color: AppColors.textSecondary,
),
const SizedBox(height: 8),
Text(
'Adicionar foto',
style: AppText.caption.copyWith(
color: AppColors.textSecondary,
),
),
],
)
: Stack(
alignment: Alignment.bottomRight,
children: [
Positioned(
bottom: 8,
right: 8,
child: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.black54,
borderRadius: BorderRadius.circular(
AppRadius.md,
),
),
child: const Icon(
Icons.edit,
color: Colors.white,
size: 18,
),
),
),
],
),
),
),
),
const SizedBox(height: 12),
Center(
child: TextButton.icon(
onPressed: _showImageOptions,
icon: const Icon(Icons.camera_alt_outlined, size: 18),
label: const Text('Alterar foto'),
style: TextButton.styleFrom(foregroundColor: AppColors.primary),
),
),
const SizedBox(height: 24),
const Text('Nome', style: AppText.label),
const SizedBox(height: 8),
Container(
@@ -886,6 +1103,25 @@ class _EditItemScreenState extends State<EditItemScreen> {
),
),
const SizedBox(height: 20),
const Text('Notas / Descrição', style: AppText.label),
const SizedBox(height: 8),
Container(
decoration: AppDecorations.outlined(),
child: TextField(
controller: _notesController,
style: AppText.body,
maxLines: 3,
decoration: const InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(
horizontal: 16,
vertical: 14,
),
hintText: 'Detalhes, tamanho, cor, localização...',
),
),
),
const SizedBox(height: 20),
const Text('Categoria', style: AppText.label),
const SizedBox(height: 8),
Wrap(