105 lines
3.2 KiB
Dart
105 lines
3.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../../../providers/ideas_provider.dart';
|
|
import '../../../models/idea.dart';
|
|
|
|
class IdeaEditScreen extends ConsumerStatefulWidget {
|
|
final String id;
|
|
|
|
const IdeaEditScreen({super.key, required this.id});
|
|
|
|
@override
|
|
ConsumerState<IdeaEditScreen> createState() => _IdeaEditScreenState();
|
|
}
|
|
|
|
class _IdeaEditScreenState extends ConsumerState<IdeaEditScreen> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _titleController = TextEditingController();
|
|
final _descriptionController = TextEditingController();
|
|
bool _initialized = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
Future.microtask(() {
|
|
ref.read(ideasProvider.notifier).loadIdea(widget.id);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_titleController.dispose();
|
|
_descriptionController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final state = ref.watch(ideasProvider);
|
|
final notifier = ref.read(ideasProvider.notifier);
|
|
|
|
if (!_initialized && state.selectedIdea != null) {
|
|
_titleController.text = state.selectedIdea!.title;
|
|
_descriptionController.text = state.selectedIdea!.description;
|
|
_initialized = true;
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Редактировать идею'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: state.isLoading
|
|
? null
|
|
: () async {
|
|
if (_formKey.currentState!.validate()) {
|
|
final success = await notifier.updateIdea(
|
|
widget.id,
|
|
UpdateIdeaRequest(
|
|
title: _titleController.text.trim(),
|
|
description: _descriptionController.text.trim(),
|
|
),
|
|
);
|
|
if (success && mounted) context.pop();
|
|
}
|
|
},
|
|
child: state.isLoading
|
|
? const SizedBox(
|
|
height: 16,
|
|
width: 16,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Text('Сохранить'),
|
|
),
|
|
],
|
|
),
|
|
body: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
children: [
|
|
TextFormField(
|
|
controller: _titleController,
|
|
decoration: const InputDecoration(labelText: 'Название'),
|
|
textCapitalization: TextCapitalization.sentences,
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
controller: _descriptionController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Описание',
|
|
alignLabelWithHint: true,
|
|
),
|
|
maxLines: 8,
|
|
textCapitalization: TextCapitalization.sentences,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|