I'm attempting to implement the showCupertinoModalPopup() form the cupertino library of Flutter for my flutter app. I'm getting the said error when I use a return showCupertinoModalPopup() from a Widget build (BuildContext context).
I intend to show a cupertino modal popup which contains the cupertinoactionsheet with options for the user to choose from and pop out after selection. While cupertinoactionsheet itself works since it is a widget, when I attempt to wrap it inside the cupertino modal popup , for a true IOS style UI, I get the said error.
Here is my code snippet :
@override
Widget build(BuildContext context) {
return showCupertinoModalPopup(
context: context,
builder: (_) => CupertinoActionSheet(
actions: <Widget>[
Container(
color: Colors.transparent,
child: CupertinoActionSheetAction(
onPressed: ({int index = 0}) {
VideosSoundSetting newType = allVideosSoundSettings[index];
widget.onTypeChanged(newType);
Navigator.pop(context);
},
child: const Text(
style: TextStyle(
fontWeight: FontWeight.normal,
color: Colors.black,
fontSize: 18),
'Enabled',
),
),
),
Container(
color: Colors.transparent,
child: CupertinoActionSheetAction(
child: const Text(
style: TextStyle(
fontWeight: FontWeight.normal,
color: Colors.black,
fontSize: 18),
'Disabled',
),
onPressed: ({int index = 1}) {
VideosSoundSetting newType = allVideosSoundSettings[index];
widget.onTypeChanged(newType);
Navigator.pop(context);
},
),
),
],
cancelButton: CupertinoActionSheetAction(
isDefaultAction: true,
onPressed: () {
Navigator.pop(context);
},
child: const KNText(
'Cancel',
),
),
),
);
}
}
I followed the example from this link and implemented on similar lines for my case. Flutter - CupertinoActionSheet / CupertinoActionSheetAction background color different on-device than in simulator
I've spent quite a lot of time figuring this out and unable the best way to do it. Assistance required.
The error is pretty clear in this case, the build
function of a Widget
needs to return another Widget
. You are returning the result of showCupertinoModalPopup()
, which according to the doc (and error message), returns a Future
.
What I think you want to do instead is not call showCupertinoModalPopup()
from a build
method, but in response to some action (like in a button's onPress
listener) or whatever other trigger you are expecting to cause the popup to open.