93 lines
3.0 KiB
Dart
93 lines
3.0 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';
|
||
import '../../../core/utils/validators.dart';
|
||
|
||
class IdeaCreateScreen extends ConsumerStatefulWidget {
|
||
const IdeaCreateScreen({super.key});
|
||
|
||
@override
|
||
ConsumerState<IdeaCreateScreen> createState() => _IdeaCreateScreenState();
|
||
}
|
||
|
||
class _IdeaCreateScreenState extends ConsumerState<IdeaCreateScreen> {
|
||
final _formKey = GlobalKey<FormState>();
|
||
final _titleController = TextEditingController();
|
||
final _descriptionController = TextEditingController();
|
||
|
||
@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);
|
||
|
||
return Scaffold(
|
||
appBar: AppBar(
|
||
title: const Text('Новая идея'),
|
||
actions: [
|
||
TextButton(
|
||
onPressed: state.isLoading
|
||
? null
|
||
: () async {
|
||
if (_formKey.currentState!.validate()) {
|
||
final success = await notifier.createIdea(
|
||
CreateIdeaRequest(
|
||
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: 'Название',
|
||
hintText: 'О чём ваша идея?',
|
||
),
|
||
validator: (v) => Validators.required(v, 'Название'),
|
||
textCapitalization: TextCapitalization.sentences,
|
||
),
|
||
const SizedBox(height: 16),
|
||
TextFormField(
|
||
controller: _descriptionController,
|
||
decoration: const InputDecoration(
|
||
labelText: 'Описание',
|
||
hintText: 'Подробно опишите идею...',
|
||
alignLabelWithHint: true,
|
||
),
|
||
maxLines: 8,
|
||
textCapitalization: TextCapitalization.sentences,
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|