I want to make ‘Year picker’ in flutter, so i decided using Cupertino style picker. but i can’t make year list picker
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class DatePicker extends StatefulWidget {
const DatePicker({super.key});
@override
State<DatePicker> createState() => _DatePickerState();
}
class _DatePickerState extends State<DatePicker> {
DateTime datetime = DateTime.now();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: CupertinoButton.filled(
child: Text('value = $datetime'),
onPressed: () {
showCupertinoModalPopup(
context: context,
builder: (BuildContext context) => SizedBox(
height: 250,
child: CupertinoDatePicker(
backgroundColor: Colors.white,
initialDateTime: datetime,
onDateTimeChanged: (DateTime newTime) {
setState(() {
datetime = newTime;
int year = newTime.year;
print('Selected Year: $year');
});
},
use24hFormat: true,
mode: CupertinoDatePickerMode.monthYear,
),
));
}),
)),
);
}
}
here is my code. i thought mode: CupertinoDatePickerMode.monthYear
that part is point. However, I couldn’t change it to the ideal way I desired, (with only the year mentioned).
I also want to change it so that the month, week, date, and time are each listed separately.
I would like to customize it so that, instead of an infinite selection around the current year, it is centered around the present and allows selection from 1980 to -26 years.
Alternatively, it would be helpful if you could provide a reference link and tips on how to customize it according to my preferences.
2
Answers
This also works well. Just make sure to place it in the child or children section of the widget.
Through this experience, I've learned that if you can't customize the date as desired in the Cupertino DatePicker, you need to customize it through the CupertinoPicker.