mc_gallery/lib/features/core/services/overlay_service.dart

44 lines
1.1 KiB
Dart

import 'package:flutter/widgets.dart';
import 'package:mc_gallery/features/core/services/logging_service.dart';
import 'package:mc_gallery/locator.dart';
class OverlayService {
OverlayService({
required LoggingService loggingService,
}) : _loggingService = loggingService;
final LoggingService _loggingService;
final Map<int, OverlayEntry> _overlayEntryMap = {};
void insertOverlayEntry(
BuildContext context, {
required String tag,
required OverlayEntry overlayEntry,
}) {
_overlayEntryMap.addEntries([MapEntry(tag.hashCode, overlayEntry)]);
Overlay.of(context, rootOverlay: true)?.insert(overlayEntry);
_loggingService.info('Overlay inserted with tag: $tag');
}
void removeOverlayEntry({
required String tag,
}) {
_overlayEntryMap[tag.hashCode]?.remove();
_overlayEntryMap.remove(tag.hashCode);
_loggingService.info('Overlay removed with tag: $tag');
}
void dispose() {
for (final overlayEntry in _overlayEntryMap.values) {
if (overlayEntry.mounted) {
overlayEntry.remove();
}
}
_overlayEntryMap.clear();
}
static OverlayService get locate => Locator.locate();
}