我正在尝试为我的 flutter 应用程序实现 Flutter cupertino 库中的 showCupertinoModalPopup() 。当我从 Widget 构建(BuildContext 上下文)使用 return showCupertinoModalPopup() 时,我收到上述错误。
我打算显示一个库比蒂诺模式弹出窗口,其中包含 cupertinoactionsheet 包含供用户选择和弹出的选项 选择后出来。虽然 cupertinoactionsheet 本身可以工作,因为它 是一个小部件,当我尝试将其包装在库比蒂诺模式中时 popup ,对于真正的 IOS 风格的 UI,我得到 上述错误。
这是我的代码片段:
@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',
),
),
),
);
}
我按照此链接中的示例进行操作,并在我的案例中以类似的方式实现。 Flutter - CupertinoActionSheet / CupertinoActionSheetAction 背景颜色在设备上与模拟器中不同
我花了很多时间来解决这个问题,但找不到最好的方法。需要帮助。
在这种情况下,错误非常明显,
build
的Widget
函数需要返回另一个Widget
。您正在返回 showCupertinoModalPopup()
的结果,根据 doc(和错误消息),返回 Future
。
我认为您想要做的不是从 showCupertinoModalPopup()
方法调用 build
,而是响应某些操作(例如在按钮的 onPress
侦听器中)或您期望导致弹出窗口打开的任何其他触发器.