content: import 'package:flutter/material.dart';
  import 'package:flutterfire_samples/res/custom_colors.dart';
  
  import '../../../common/widgets/custom_form_field.dart';
  import '../utils/database.dart';
  import '../utils/validator.dart';
  
  class DbAddItemForm extends StatefulWidget {
    final FocusNode titleFocusNode;
    final FocusNode descriptionFocusNode;
  
    const DbAddItemForm({
      super.key,
      required this.titleFocusNode,
      required this.descriptionFocusNode,
    });
  
    @override
    State<DbAddItemForm> createState() => _DbAddItemFormState();
  }
  
  class _DbAddItemFormState extends State<DbAddItemForm> {
    final _addItemFormKey = GlobalKey<FormState>();
  
    bool _isProcessing = false;
  
    final TextEditingController _titleController = TextEditingController();
    final TextEditingController _descriptionController = TextEditingController();
  
    @override
    Widget build(BuildContext context) {
      return Form(
        key: _addItemFormKey,
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(
                left: 8.0,
                right: 8.0,
                bottom: 24.0,
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const SizedBox(height: 24.0),
                  const Text(
                    'Title',
                    style: TextStyle(
                      color: Palette.firebaseGrey,
                      fontSize: 22.0,
                      letterSpacing: 1,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(height: 8.0),
                  CustomFormField(
                    isLabelEnabled: false,
                    controller: _titleController,
                    focusNode: widget.titleFocusNode,
                    keyboardType: TextInputType.text,
                    inputAction: TextInputAction.next,
                    validator: (value) => Validator.validateField(
                      value: value,
                    ),
                    label: 'Title',
                    hint: 'Enter your note title',
                  ),
                  const SizedBox(height: 24.0),
                  const Text(
                    'Description',
                    style: TextStyle(
                      color: Palette.firebaseGrey,
                      fontSize: 22.0,
                      letterSpacing: 1,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(height: 8.0),
                  CustomFormField(
                    maxLines: 10,
                    isLabelEnabled: false,
                    controller: _descriptionController,
                    focusNode: widget.descriptionFocusNode,
                    keyboardType: TextInputType.text,
                    inputAction: TextInputAction.done,
                    validator: (value) => Validator.validateField(
                      value: value,
                    ),
                    label: 'Description',
                    hint: 'Enter your note description',
                  ),
                ],
              ),
            ),
            _isProcessing
                ? const Padding(
                    padding: EdgeInsets.all(16.0),
                    child: CircularProgressIndicator(
                      valueColor: AlwaysStoppedAnimation<Color>(
                        Palette.firebaseOrange,
                      ),
                    ),
                  )
                : SizedBox(
                    width: double.maxFinite,
                    child: ElevatedButton(
                      style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all(
                          Palette.firebaseOrange,
                        ),
                        shape: MaterialStateProperty.all(
                          RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10),
                          ),
                        ),
                      ),
                      onPressed: () async {
                        widget.titleFocusNode.unfocus();
                        widget.descriptionFocusNode.unfocus();
  
                        if (_addItemFormKey.currentState!.validate()) {
                          setState(() {
                            _isProcessing = true;
                          });
  
                          await Database.addItem(
                            title: _titleController.text,
                            description: _descriptionController.text,
                          );
  
                          setState(() {
                            _isProcessing = false;
                          });
                          if (!mounted) return;
                          Navigator.of(context).pop();
                        }
                      },
                      child: const Padding(
                        padding: EdgeInsets.only(top: 16.0, bottom: 16.0),
                        child: Text(
                          'ADD ITEM',
                          style: TextStyle(
                            fontSize: 24,
                            fontWeight: FontWeight.bold,
                            color: Palette.firebaseGrey,
                            letterSpacing: 2,
                          ),
                        ),
                      ),
                    ),
                  ),
          ],
        ),
      );
    }
  }
  