如何在Tap上关闭自己的OverlayEntry

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

我有一个全屏显示的 OverlayEntry。我想调度一个动作并在点击overlayentry按钮时关闭它

OverlayEntry _buildOverlayFeedback(BuildContext context, String tituloEvento) {
      return OverlayEntry(
        builder: (context) => Material(
          child: Container(
            width: double.infinity,
            height: double.infinity,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Column(
                  children: <Widget>[
                    ListTile(
                      leading: Icon(Icons.sentiment_dissatisfied),
                      title: Text('No me ha gustado'),
                      onTap: () {
                            // how to close myself????
                      },
                    ),
                    ListTile(
                        leading: Icon(Icons.sentiment_very_satisfied),
                        title: Text('Muy bien'),
                        onTap: () {}),
                  ],
                ),
              ],
            ),
          ),
        ),
      );
    }
dart flutter
2个回答
8
投票

您可以在

remove()
本身上调用
OverlayEntry

这可能是一种方法:

      OverlayEntry _buildOverlayFeedback(BuildContext context, String tituloEvento) {
        OverlayEntry? entry;
        entry = OverlayEntry(
          builder: (context) => Material(
            child: Container(
              width: double.infinity,
              height: double.infinity,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Column(
                    children: <Widget>[
                      ListTile(
                        leading: Icon(Icons.sentiment_dissatisfied),
                        title: Text('No me ha gustado'),
                        onTap: () {
                          entry!.remove();
                        },
                      ),
                      ListTile(
                          leading: Icon(Icons.sentiment_very_satisfied),
                          title: Text('Muy bien'),
                          onTap: () {}),
                    ],
                  ),
                ],
              ),
            ),
          ),
        );
        return entry!;
      }

2
投票

对于那些试图弄清楚如何使用 FCM

onMessage
Navigator.of(context).overlay.insert(entry)
做到这一点的人 - chemamolins 的答案有效,您只需稍微调整一下即可。这是一个类似的示例来帮助您入门:

      onMessage: (Map<String, dynamic> message) async {
        OverlayEntry entry;
        entry = OverlayEntry(builder: (context) {
          return GestureDetector(
            onTap: entry.remove,
            child: SafeArea(
              child: Align(
                alignment: Alignment.topCenter,
                child: Material(
                  type: MaterialType.transparency,
                  child: Container(
                    decoration: BoxDecoration(color: Colors.white),
                    width: double.infinity,
                    height: 60,
                    child: Column(
                      children: <Widget>[
                        Text(message['notification']['title']),
                        Text(message['notification']['body']),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          );
        });
        Navigator.of(context).overlay.insert(entry);
      },

并且

onTap
将关闭 OverlayEntry。

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