skip to Main Content

I am working on To Do Flutter app and I have a form to add new task with (title – description – deadline datetime) in the deadline TextFormField I made it readOnly and I set the onTap() to show the date picker and then show the time picker and then format it together and put it into the TextFormField, but the problem is when I enter the title and description and then I click on the deadline TextFormField to choose date and time, the tap clears what I typed in the title and description TextFormFields
this is the code

class AddTaskFormWidget extends StatelessWidget {
  AddTaskFormWidget({Key? key}) : super(key: key);

  static final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  final TextEditingController titleController = TextEditingController();
  final TextEditingController descriptionController = TextEditingController();
  final TextEditingController deadlineController = TextEditingController();
  
  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Column(
        children: [
          TextFormField(
            controller: titleController,
            decoration: const InputDecoration(
              label: Text('Task Title'),
            ),
            validator: (val) {
              if (!val!.isValidTitle) {
                return 'Please enter valid title';
              }

              return null;
            },
          ),
          TextFormField(
            controller: descriptionController,
            decoration: const InputDecoration(
              label: Text('Task Description'),
            ),
            validator: (val) {
              if (!val!.isValidDescription) {
                return 'Please enter valid description';
              }

              return null;
            },
          ),
          TextFormField(
            readOnly: true,
            onTap: () async {
              DateTime? deadlineDate = await showDatePicker(
                context: context,
                initialDate: DateTime.now(),
                firstDate: DateTime.now(),
                lastDate: DateTime(2100),
              );

              if (deadlineDate == null) return;

              TimeOfDay? deadlineTime = await showTimePicker(
                context: context,
                initialTime: TimeOfDay(
                  hour: 12,
                  minute: 0,
                ),
              );

              if (deadlineTime == null) return;

              final DateTime deadlineDateTime = DateTime(
                deadlineDate.year,
                deadlineDate.month,
                deadlineDate.day,
                deadlineTime.hour,
                deadlineTime.minute,
              );

              deadlineController.text = DateFormat('dd-MM-yyyy hh:mm').format(deadlineDateTime).toString();
            },
            controller: deadlineController,
            decoration: const InputDecoration(
              label: Text('Task Deadline'),
            ),
            validator: (val) {
              if (val!.isEmpty) {
                return 'Please enter deadline';
              }

              return null;
            },
          ),
          ElevatedButton(
            onPressed: () {
              if (_formKey.currentState!.validate()) {
                Navigator.pop(context);
              }
            },
            child: const Text('Submit'),
          ),
        ],
      ),
    );
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    The issue I was facing is due to the fact that when I open the date and time picker dialogs, I'm losing focus on the other text form fields, which triggers their validation and clears their values.

    I found that to solve this, you can use the FocusNode class to manage the focus of the text form fields

    final titleFocusNode = FocusNode();
    final descriptionFocusNode = FocusNode();
    final deadlineFocusNode = FocusNode();
    
    @override
    void dispose() {
      titleController.dispose();
      descriptionController.dispose();
      deadlineController.dispose();
      titleFocusNode.dispose();
      descriptionFocusNode.dispose();
      deadlineFocusNode.dispose();
      super.dispose();
    }
    

  2. There doesn’t seem to have any issues that you mentioned in the question.
    It works fine onTap of date and time TextFormField.
    The issue usually happens when we declare the TextEditingController inside the build method.
    But here everything is Fine.

    If you provide more info, I can Help you with it.

    Output of the code you send

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search