mirrored_repositories/notes_app_learning/lib/views
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:notter/cubits/add_note_cubit/add_note_cubit.dart';
import 'package:notter/models/note_model.dart';
import 'colors_list_view.dart';
import 'custom_button.dart';
import 'custom_text_fiedls.dart';

class AddNoteForm extends StatefulWidget {
  const AddNoteForm({
    super.key,
  });

  @override
  State<AddNoteForm> createState() => _AddNoteFormState();
}

class _AddNoteFormState extends State<AddNoteForm> {
  final GlobalKey<FormState> formKey = GlobalKey();

  AutovalidateMode autovalidateMode = AutovalidateMode.disabled;

  String? title, subtitle;

  @override
  Widget build(BuildContext context) {
    return Form(
      key: formKey,
      autovalidateMode: autovalidateMode,
      child: Column(
        children: [
          const SizedBox(height: 32.0),
          CustomTextField(
            hint: 'Title',
            onSaved: (value) {
              title = value;
            },
          ),
          const SizedBox(height: 24.0),
          CustomTextField(
            hint: 'Note Content',
            maxLines: 4,
            onSaved: (value) {
              subtitle = value;
            },
          ),
          const SizedBox(height: 35.0),
          const ColorsListView(),
          const SizedBox(height: 35.0),
          BlocBuilder<AddNoteCubit, AddNoteState>(
            builder: (context, state) {
              return CustomButton(
                isLoading: state is AddNoteLoading ? true : false,
                onTap: () {
                  if (formKey.currentState!.validate()) {
                    formKey.currentState!.save();

                    // create note model to receive the data from the form
                    var noteModel = NoteModel(
                      title: title!,
                      subTitle: subtitle!,
                      date: DateFormat('EEEE, MMM d, yyyy')
                          .format(DateTime.now())
                          .toString(),
                      color: Colors.blue.value,
                    );

                    BlocProvider.of<AddNoteCubit>(context).addNote(noteModel);
                  } else {
                    autovalidateMode = AutovalidateMode.always;
                    setState(() {});
                  }
                },
              );
            },
          ),
        ],
      ),
    );
  }
}
