Skip to content

Instantly share code, notes, and snippets.

@miguelpruivo
Last active July 30, 2019 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelpruivo/a1d04a23557030414e6ff5cffb5c9abb to your computer and use it in GitHub Desktop.
Save miguelpruivo/a1d04a23557030414e6ff5cffb5c9abb to your computer and use it in GitHub Desktop.
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