I’m working on a date picker based on ListWheelScrollView
.
all is good but at the end of choosing the date, i wanna pass the selected date to the user of my widget.
The idea is very similar to TextFormField
onChanged
method, when the text changes it passes it back to you.
How can i make a similar onChanged(int year, int month, int day)
which let’s the user get the chosen date.
Here’s the date picker:
class ScrollWheelDatePicker extends StatelessWidget {
ScrollWheelDatePicker({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context)=> DatePickerCubit(),
child: BlocConsumer<DatePickerCubit,DatePickerStates>(
listener: (context,state){},
builder:(context,state){
var cubit = DatePickerCubit.getCubit(context);
return Row(
children: [
// every list wheel will allocate 1/3 of width and the same of height
ListWheel(
controller: cubit.controller,
onSelectedItemChanged: (int index){
print('years is changing');
cubit.changeYear(index);
},
title: 'Year',
children: cubit.years.map((e) => Text(e.toString())).toList(),
),
ListWheel(
title: 'Month',
onSelectedItemChanged: (index){
cubit.changeMonth(index);
},
children: cubit.months.map((e) => Text(e.monthNumber.toString())).toList(),
),
ListWheel(
title: 'Day',
children: [for(int i = cubit.getStartDay; i<= cubit.chosenMonth!.maxDays;i++)Text(i.toString())],
),
],
);
},
),
);
}
}
The client widget can use it like this:
Cotainer(
child: ScrollWheelDatePicker(),
)
And, the desired result:
Container(
child: ScrollWheelDatePicker(
onChanged : (year, month, day){
// user gains access to date;
}
)
)
btw: i can access the chosen date year, month .. from the state manager.