如何在Tap上关闭自我OverlayEntry

时间:2018-12-12 20:59:05

标签: dart flutter

我有一个全屏显示的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: () {}),
                  ],
                ),
              ],
            ),
          ),
        ),
      );
    }

2 个答案:

答案 0 :(得分:0)

您在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;
      }

答案 1 :(得分:0)

对于那些想弄清楚如何使用FCM onMessageNavigator.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。