Home > Back-end >  how to make today's date default value as dropdown value in flutter?
how to make today's date default value as dropdown value in flutter?

Time:07-05

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);
                                                                                              
                                });
                              }
                            },
                          ),

CodePudding user response:

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

 String? dropdownvalue;

  var items = [
    DateTime.now().toString(),
    'Choose from calendar',
  ];
  • Related