我对Flutter 翼和dart 是新手.我想删除覆盖图.我在网上试了很多答案,但还是卡住了.请看我的代码

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

void _insertOverlay(BuildContext context) { 
    return Overlay.of(context).insert(
      OverlayEntry(builder: (context) {
        
        
        return Positioned(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          top: 0,
          left: 0,
          child: Material(
            color: const Color(0xFF0E3311).withOpacity(0.5),
            child: GestureDetector(
              onTap: (){
                print('OVERLAY ON');
                //on tap, remove or hide the overlay
              },
              child: Container( decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.redAccent), ),
            ),
          ),
        );
      }),
    );
  }

  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      appBar: AppBar(
       
        title: Text(widget.title),
      ),
      body: Center(
        
        child: Column(
          
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'Flutter application',
            ),
            Text(
              '',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: ()=>WidgetsBinding.instance.addPostFrameCallback((_) => _insertOverlay(context)),
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

我已经能够使覆盖显示出来,但我正在try 删除或隐藏它.看看我到目前为止做了什么.我做这件事已经有一段时间了.提前谢谢你

推荐答案

请try 以下代码:

import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  OverlayEntry? _overlayEntry;

  void _insertOverlay(BuildContext context) {
    _overlayEntry = OverlayEntry(builder: (context) {
      return Positioned(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        top: 0,
        left: 0,
        child: Material(
          color: const Color(0xFF0E3311).withOpacity(0.5),
          child: GestureDetector(
            onTap: () {
              print('OVERLAY ON');
              //on tap, remove or hide the overlay
              _overlayEntry?.remove();
            },
            child: Container(
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: Colors.redAccent,
              ),
            ),
          ),
        ),
      );
    });
    Overlay.of(context)?.insert(_overlayEntry!);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'Flutter application',
            ),
            Text(
              '',
              style: Theme.of(context).textTheme.headline6,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () =>
            WidgetsBinding.instance!.addPostFrameCallback((_) => _insertOverlay(context)),
        child: const Icon(Icons.add),
      ),
    );
  }
}

如果你有任何问题,请再问一遍.

快乐的编码...

Flutter相关问答推荐

无法在主屏幕视图中设置当前日期容器'

FCM通知显示两次通知(&A)

如何将Will POP示波器转换为POP数据抖动的POP示波器

未处理的异常:第1行第5列出现错误:无效的媒体类型:应为/&;.从API获取数据时在Flutter中

Riverpod 2.0-如何使用FutureOr和AutoDisposeSyncNotificationer处理api状态代码?

dotenv:未处理的异常:FileNotFoundError的实例

如何在 Flutter 中设计像 zomato 应用程序一样的搜索字段?

即使小部件比Flutter 中的屏幕更宽,也始终显示滚动条

Flutter:我应该使用哪种应用内购买服务?

在 Flutter 中解码 BLE 设备负载

显示图像的放大部分

如何从列表中更新provider变量:Flutter列表和Provider优化指南

Firestore 使用 Flutter 以奇怪的格式保存数据

更改下拉按钮的背景 colored颜色 - flutter

我想在文本结束后显示分隔线.它会在第一行、第二行或第三行结束

Flutter 应用程序在启动画面冻结(java.lang.RuntimeException:无法启动活动 ComponentInfo)

具有 BLOC 和存储库模式的 Flutter Workmanager

如何防止卡子在底部溢出?

Flutter:如何将列表转换为字符串?

更新未知文档 ID firebase\flutter 中的字段