flutterflutter-material

Change the selected color in the material library YearPicker in Flutter


I want to change Selected color for Yearpicker

Without any changes.

I want to change selected color to green In simple Date picker there is Build method available for change in theme but For Yearpicker i could not find any way.

return AlertDialog(
      title: const Text("Select Year"),
      content: SizedBox(

        width: 300,
        height: 300,
        child: YearPicker(
          firstDate: DateTime(DateTime.now().year - 23, 1),
          lastDate: DateTime(DateTime.now().year),
          initialDate: DateTime.now(),
          selectedDate: selectedDate,
          onChanged: (DateTime dateTime) {
            Navigator.pop(context);
          },
        ),
      ),
    );

Solution

  • By wrapping it with a Theme Widget

                 return AlertDialog(
                title: const Text("Select Year"),
                content: SizedBox(
                  width: 300,
                  height: 300,
                  child: Theme(
                    data: ThemeData.light().copyWith(
                      colorScheme: ColorScheme.light(
                        primary: Colors
                            .green, // This changes the color of the year picker
                      ),
                    ),
                    child: YearPicker(
                      firstDate: DateTime(DateTime.now().year - 23, 1),
                      lastDate: DateTime(DateTime.now().year),
                      initialDate: DateTime.now(),
                      selectedDate: selectedDate,
                      onChanged: (DateTime dateTime) {
                        Navigator.pop(context);
                      },
                    ),
                  ),
                ),
              )