getChoiceValue<T> function
Return a choice of one value out of a given list.
The list is presented as a dialog and the user clicks on the item label and the item
of type T
is returned.
The context
is used by the showDialog to give it context. It will switch between Material and Cupertino mode
based on the host platform.
The dialog is given the title title
and shows the labels
. When one is clicked the item
at the appropriate index
of items
is returned.
If the user closes the dialog with selecting a choice a value of null
is returned.
Implementation
Future<T> getChoiceValue<T>(BuildContext context,{@required String title,@required List<String>labels,@required List<T> values}) async {
assert(labels.length == values.length);
if (isMaterial(context)) {
List<SimpleDialogOption> list = [];
for(int i=0;i < labels.length;i++) {
list.add(SimpleDialogOption(onPressed:() {Navigator.pop(context,values[i]);},child:Text(labels[i])));
}
return await showDialog<T>(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: Text(title),
children: list
);
}
);
} else {
List<CupertinoActionSheetAction> list = [];
for(int i=0;i < labels.length;i++) {
list.add(CupertinoActionSheetAction(onPressed:() {Navigator.pop(context,values[i]);},child:Text(labels[i]),isDefaultAction: i == 0));
}
return await showCupertinoModalPopup<T>(
context: context,
builder: (BuildContext context) {
return CupertinoActionSheet(
title: Text(title),
actions: list
);
});
}
}