import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../providers/instruction_provider.dart'; class AddInstructionPage extends StatefulWidget { const AddInstructionPage({super.key}); @override State createState() => _AddInstructionPageState(); } class _AddInstructionPageState extends State { final _formKey = GlobalKey(); final _titleController = TextEditingController(); final _contentController = TextEditingController(); final _tagController = TextEditingController(); final List _tags = []; void _addTag() { final tag = _tagController.text.trim().toLowerCase(); if (tag.isNotEmpty && !_tags.contains(tag)) { setState(() { _tags.add(tag); _tagController.clear(); }); } } void _removeTag(String tag) { setState(() { _tags.remove(tag); }); } Future _saveInstruction() async { if (_formKey.currentState!.validate()) { final provider = Provider.of(context, listen: false); await provider.addInstruction( _titleController.text, _contentController.text, _tags, ); if (mounted) { Navigator.of(context).pop(); } } } @override void dispose() { _titleController.dispose(); _contentController.dispose(); _tagController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Новый инструктаж'), actions: [ IconButton( icon: const Icon(Icons.save), onPressed: _saveInstruction, ) ], ), body: Padding( padding: const EdgeInsets.all(16.0), child: Form( key: _formKey, child: ListView( children: [ TextFormField( controller: _titleController, decoration: const InputDecoration(labelText: 'Название'), validator: (value) => (value == null || value.isEmpty) ? 'Введите название' : null, ), const SizedBox(height: 16), TextFormField( controller: _contentController, decoration: const InputDecoration(labelText: 'Содержание'), maxLines: 5, validator: (value) => (value == null || value.isEmpty) ? 'Введите содержание' : null, ), const SizedBox(height: 16), Row( children: [ Expanded( child: TextField( controller: _tagController, decoration: const InputDecoration(labelText: 'Добавить тег'), onSubmitted: (_) => _addTag(), ), ), IconButton( icon: const Icon(Icons.add_circle_outline), onPressed: _addTag, ), ], ), const SizedBox(height: 10), Wrap( spacing: 8.0, children: _tags.map((tag) => Chip( label: Text(tag), onDeleted: () => _removeTag(tag), )).toList(), ), ], ), ), ), ); } }