“Future<dynamic>”类型的值无法从“build”方法返回,因为它的返回类型为“Widget

问题描述 投票:0回答:1

我正在尝试为我的 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 背景颜色在设备上与模拟器中不同

我花了很多时间来解决这个问题,但找不到最好的方法。需要帮助。

flutter dart flutter-cupertino cupertinopicker cupertino-widgets
1个回答
0
投票

在这种情况下,错误非常明显,

build
Widget
函数需要返回另一个
Widget
。您正在返回
showCupertinoModalPopup()
的结果,根据 doc(和错误消息),返回
Future
。 我认为您想要做的不是从
showCupertinoModalPopup()
方法调用
build
,而是响应某些操作(例如在按钮的
onPress
侦听器中)或您期望导致弹出窗口打开的任何其他触发器.

© www.soinside.com 2019 - 2024. All rights reserved.