How do I pass controller without ‘invalid date error’ error

How do I pass controller without ‘invalid date error’ error


0

I’m trying to pass the date from the datepicker and timepicker but it always gives an invalid date error

I already tried .year .month .day .hour. minute. DateTime.parse doesnt wor
enter image description here

I already tried .year .month .day .hour. .minute parse, toString nothing work

      import 'package:datetime_picker_formfield_new/datetime_picker_formfield.dart';
  import 'package:flutter/material.dart';
  import 'package:get/get.dart';
  import 'package:horario/features/Date/presentation/controller/date_controller.dart';
  import 'package:intl/intl.dart';

  class DateRegisterPage extends StatelessWidget {
    final _dateController = Get.find<DateController>();
    final format = DateFormat("yyyy-MM-dd HH:mm");
DateRegisterPage({super.key});
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: SingleChildScrollView(
      child: Column(children: <Widget>[
        Text('Basic date & time field (${format.pattern})'),
        Form(
          key: _dateController.formKey,
          child: DateTimeField(
            controller: _dateController.tdatedInputController,
            format: format,
            onShowPicker: (context, currentValue) async {
              return await showDatePicker(
                context: context,
                firstDate: DateTime(1900),
                initialDate: currentValue ?? DateTime.now(),
                lastDate: DateTime(2100),
              ).then((DateTime? date) async {
                if (date != null) {
                  final time = await showTimePicker(
                    context: context,
                    initialTime:
                      TimeOfDay.fromDateTime(currentValue ?? DateTime.now()),
                  );
                  return DateTimeField.combine(date, time);
                } else {
                  return currentValue;
                }
              });
            },
          ),
        ),
        TextButton(onPressed: _dateController.validForm,
        child: const Text('Agendar')
        )
      ]),
    ),
  );
}

}

New contributor

Pedro Rodrigues is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

  • Can you share as code-snippet instead of code-image?

    – Md. Yeasin Sheikh

    2 hours ago

  • code is here, i can combine the timeofday with date time?

    – Pedro Rodrigues

    1 hour ago

1 Answer
1


0

It seems like you’re facing challenges when working with the date and time picked from a date picker and a time picker in Flutter. Let’s troubleshoot and provide some guidance.

Here’s an example of how you can combine the date and time picked from two separate pickers:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  DateTime? selectedDate = DateTime.now();
  TimeOfDay? selectedTime = TimeOfDay.now();

  Future<void> _selectDate(BuildContext context) async {
    final DateTime? pickedDate = await showDatePicker(
      context: context,
      initialDate: selectedDate ?? DateTime.now(),
      firstDate: DateTime(2000),
      lastDate: DateTime(2101),
    );
    if (pickedDate != null && pickedDate != selectedDate)
      setState(() {
        selectedDate = pickedDate;
      });
  }

  Future<void> _selectTime(BuildContext context) async {
    final TimeOfDay? pickedTime = await showTimePicker(
      context: context,
      initialTime: selectedTime ?? TimeOfDay.now(),
    );
    if (pickedTime != null && pickedTime != selectedTime)
      setState(() {
        selectedTime = pickedTime;
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Date and Time Picker Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Selected Date: ${selectedDate?.toLocal()}',
              style: TextStyle(fontSize: 20),
            ),
            SizedBox(height: 20),
            Text(
              'Selected Time: ${selectedTime?.format(context)}',
              style: TextStyle(fontSize: 20),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () => _selectDate(context),
              child: Text('Select Date'),
            ),
            ElevatedButton(
              onPressed: () => _selectTime(context),
              child: Text('Select Time'),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                DateTime? selectedDateTime = selectedDate != null && selectedTime != null
                    ? DateTime(
                        selectedDate!.year,
                        selectedDate!.month,
                        selectedDate!.day,
                        selectedTime!.hour,
                        selectedTime!.minute,
                      )
                    : null;
                print('Combined DateTime: $selectedDateTime');
              },
              child: Text('Combine Date and Time'),
            ),
          ],
        ),
      ),
    );
  }
}

In this example:

  1. A date picker is used to pick the date.
  2. A time picker is used to pick the time.
  3. The selected date and time are displayed.
  4. The date and time are combined into a DateTime object when a button is pressed.

Make sure to adapt this code to your specific use case.



Leave a Reply

Your email address will not be published. Required fields are marked *