Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A helper method that opens a native iOS flavor `CupertinoTextPicker` and `CupertinoDatePicker`
void showCupertinoPicker<T>(BuildContext context,
{bool isDatePicker = false,
List<T> options,
T selectedValue,
DateTime minDate,
DateTime lastDate,
DateTime initialDate,
Function(T) onChanged}) {
showCupertinoModalPopup(
context: context,
builder: (context) => SizedBox(
height: 240.0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
alignment: Alignment.centerRight,
decoration: BoxDecoration(
color: const Color.fromRGBO(249, 249, 247, 1.0),
border: Border(
bottom: const BorderSide(width: 0.5, color: Colors.black38),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
CupertinoButton(
padding: const EdgeInsets.symmetric(horizontal: 15.0),
child: Text(
'Cancel',
style: CupertinoTheme.of(context).textTheme.actionTextStyle.copyWith(
fontWeight: FontWeight.w600,
color: Colors.red,
),
),
onPressed: () {
onChanged(null);
Navigator.of(context).pop();
},
),
CupertinoButton(
padding: const EdgeInsets.symmetric(horizontal: 15.0),
child: Text(
'Done',
style: CupertinoTheme.of(context).textTheme.actionTextStyle.copyWith(fontWeight: FontWeight.w600),
),
onPressed: () => Navigator.of(context).pop(),
),
],
),
),
Expanded(
child: isDatePicker
? CupertinoDatePicker(
minimumDate: minDate,
maximumDate: lastDate,
initialDateTime: initialDate,
mode: CupertinoDatePickerMode.date,
onDateTimeChanged: (DateTime value) {
if (onChanged != null) onChanged(value as T);
},
)
: CupertinoPicker.builder(
backgroundColor: Colors.white,
childCount: options.length,
itemBuilder: (BuildContext context, int index) => Center(
child: Text(options[index]),
),
itemExtent: 35.0,
scrollController: selectedValue != null
? FixedExtentScrollController(initialItem: options.indexOf(options.singleWhere((val) => val == selectedValue)))
: null,
onSelectedItemChanged: (int index) => onChanged(options[index]),
),
),
],
),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment