skip to Main Content

I am using showDatePicker() to select the date in my flutter application. Everything works perfectly except that when I select the date, and try to change the year, everything else is just white, even if I select any year, it won’t be displayed.

The code:

final datePicked = await showDatePicker(
      context: context,
      initialDate: _dDate,
      firstDate: widget.firstDate ?? DateTime.now(),
      lastDate: widget.lastDate ?? DateTime.now(),
      initialDatePickerMode: widget.initialDatePickerMode ?? DatePickerMode.day,
      initialEntryMode: widget.initialEntryMode ?? DatePickerEntryMode.calendar,
    );

The issue I am getting:

date picker issue

2

Answers


  1. Chosen as BEST ANSWER

    Solved the issue by adding the datePickerTheme in themeData

    datePickerTheme: const DatePickerThemeData().copyWith(
          yearBackgroundColor: const MaterialStatePropertyAll(Colors.white),
          yearForegroundColor: MaterialStatePropertyAll(Colors.black),
        ),
    

  2. try below code :

    DateTime? selectedDate;
    TimeOfDay? selectedTime;
    
    Future<void> _selectDate(BuildContext context) async {
        final DateTime? picked = await showDatePicker(
          context: context,
          initialDate: selectedDate ?? DateTime.now(),
          firstDate: DateTime(2000),
          lastDate: DateTime(2101),
          builder: (BuildContext context, Widget? child) {
            return Theme(
              data: ThemeData.light().copyWith(
                colorScheme: ColorScheme.light(primary: Colors.black),
                buttonTheme: ButtonThemeData(textTheme: ButtonTextTheme.primary),
              ),
              child: child!,
            );
          },
        );
    
        if (picked != null && picked != selectedDate) {
          setState(() {
            selectedDate = picked;
          });
         
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search