Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to make today's date default value as dropdown value in flutter?

My dropDown working fine, instead of Today as a string in the dropDown initial value I want Today’s date-time using the property DateTime.now()

String dropdownvalue = 'Today';
  var items = [
    'Today',
    'Choose from calendar',
  ];

My dropdown:

 DropdownButton(
                            underline: Container(),
                            isExpanded: true,
                            value: dropdownvalue,
                            icon: const Icon(
                              Icons.keyboard_arrow_down,
                              color: Color(0xffB50000),
                            ),
                            items: items.map((String items) {
                              return DropdownMenuItem(
                                value: items,
                                child: Text(
                                  items,
                                  style: TextStyle(
                                      color: Color(0xffB50000),
                                      fontWeight: FontWeight.w400),
                                ),
                              );
                            }).toList(),
                            onChanged: (String? newValue) {
                              setState(() {
                                dropdownvalue = newValue!;
                              });
              
                              if(dropdownvalue =='Choose from calendar'){
                                  setState(() {
                                     _selectedDate(context);                                                         
                                  });
                                }
                              if(dropdownvalue =='Today'){
                                  setState(() {
                                    String today = DateFormat('dd-MM-yyyy').format(currentDateTime);
                                    print("today");
                                    print(today);
                                  
                                    final storeProvider = Provider.of<StorageProvider>(context, listen: false);
                                    storeProvider.updateAppointmentDate(today);
                                          
                                    final slotBookingProvider = Provider.of<SlotBookingProvider>(context, listen: false);
                                    slotBookingProvider.checkAvailableSlot(date: today, context: context);
                                                                                              
                                });
                              }
                            },
                          ),

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can
You can replace replacing Today with DateTime.now().toString()

 String? dropdownvalue;

  var items = [
    DateTime.now().toString(),
    'Choose from calendar',
  ];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading