madari-oss/lib/features/collection/container/create_new_collection.dart
Madari Developers 16fe4a653f Project import generated by Copybara.
GitOrigin-RevId: 829626e92d5dba6a4586d1e7c4bd1615ec396e88
2025-01-02 18:46:26 +00:00

147 lines
4.3 KiB
Dart

import 'package:flutter/material.dart';
import '../../../engine/engine.dart';
class CreateCollectionBottomSheet extends StatefulWidget {
final Function() onCollectionCreated;
const CreateCollectionBottomSheet({
super.key,
required this.onCollectionCreated,
});
@override
State<CreateCollectionBottomSheet> createState() =>
_CreateCollectionBottomSheetState();
}
class _CreateCollectionBottomSheetState
extends State<CreateCollectionBottomSheet> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
final _descriptionController = TextEditingController();
bool _isPublic = false;
bool _isLoading = false;
@override
void dispose() {
_nameController.dispose();
_descriptionController.dispose();
super.dispose();
}
Future<void> _createCollection() async {
if (!_formKey.currentState!.validate()) return;
setState(() => _isLoading = true);
try {
final pb = AppEngine.engine.pb;
await pb.collection('collection').create(body: {
'name': _nameController.text.trim(),
'description': _descriptionController.text.trim(),
'is_public': _isPublic,
'order': 0,
'user': pb.authStore.record!.id,
});
if (mounted) {
Navigator.pop(context);
widget.onCollectionCreated();
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Failed to create collection: ${e.toString()}')),
);
}
} finally {
if (mounted) {
setState(() => _isLoading = false);
}
}
}
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom,
left: 16,
right: 16,
top: 16,
),
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Create New Collection',
style: Theme.of(context).textTheme.titleLarge,
),
IconButton(
onPressed: () => Navigator.pop(context),
icon: const Icon(Icons.close),
),
],
),
const SizedBox(height: 16),
TextFormField(
controller: _nameController,
decoration: const InputDecoration(
labelText: 'Collection Name',
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.trim().isEmpty) {
return 'Please enter a name';
}
return null;
},
textCapitalization: TextCapitalization.words,
),
const SizedBox(height: 16),
TextFormField(
controller: _descriptionController,
decoration: const InputDecoration(
labelText: 'Description (Optional)',
border: OutlineInputBorder(),
),
maxLines: 3,
textCapitalization: TextCapitalization.sentences,
),
const SizedBox(height: 16),
SwitchListTile(
title: const Text('Make Public'),
subtitle: const Text('Allow others to view this collection'),
value: _isPublic,
onChanged: (value) => setState(() => _isPublic = value),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _isLoading ? null : _createCollection,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16),
),
child: _isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Create Collection'),
),
const SizedBox(height: 16),
],
),
),
);
}
}