2

我有一个有状态的小部件W1,它调用一个无状态的小部件W2

W2onTap功能。我想在W2's onTap().

onTap:() {Alert(context: context, title:'Hi');},

我没有收到任何错误,但点击时没有显示警报。我尝试将上下文作为参数传递给,W2但我仍然没有看到任何对话框。显示对话框的正确方法是什么W2?我正在使用 rflutter_alert 包链接

谢谢

4

3 回答 3

1

你必须Alert(context: context, title:'Hi');showDialog(context: context, builder: (BuildContext context) => Alert(context: context, title:'Hi'));

这是食谱示例:

Future<void> _neverSatisfied() async {
  return showDialog<void>(
    context: context,
    barrierDismissible: false, // user must tap button!
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Rewind and remember'),
        content: SingleChildScrollView(
          child: ListBody(
            children: <Widget>[
              Text('You will never be satisfied.'),
              Text('You\’re like me. I’m never satisfied.'),
            ],
          ),
        ),
        actions: <Widget>[
          FlatButton(
            child: Text('Regret'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
  );
}

无论如何,对于您关于如何通过的问题context,如果您正在创建不需要通过的小部件Stateless,您可以从.Statefulcontextbuild(BuildContext context) {}

于 2020-01-16T04:51:03.717 回答
0

最后添加.show()解决了它。

onTap:() {Alert(context: context, title:'Hi').show();}

它清楚地记录在 rflutter_alert 包中,但我不知何故错过了它。

于 2020-01-18T05:14:27.860 回答
0

在 onTap 的函数调用中传递上下文:

onTap:(context) {Alert(context: context, title:'Hi');},
于 2020-01-16T05:44:33.770 回答