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

47 lines
1.1 KiB
Dart

import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:mc_gallery/features/core/services/logging_service.dart';
import 'package:mc_gallery/locator.dart';
class OverlayService {
const OverlayService({
required LoggingService loggingService,
}) : _loggingService = loggingService;
final LoggingService _loggingService;
final Map<int, OverlayEntry> _overlayEntryMap = const {};
Future<void> playOverlayEntry({
required BuildContext context,
required OverlayEntry overlayEntry,
}) async {
try {
_overlayEntryMap[overlayEntry.hashCode] = overlayEntry;
Overlay.of(
context,
rootOverlay: true,
)!
.insert(overlayEntry);
if (overlayEntry.mounted) overlayEntry.remove();
_overlayEntryMap.remove(overlayEntry.hashCode);
} catch (error, stackTrace) {
_loggingService.handle(error, stackTrace);
}
}
void dispose() {
for (final overlayEntry in _overlayEntryMap.values) {
if (overlayEntry.mounted) {
overlayEntry.remove();
}
}
_overlayEntryMap.clear();
}
static OverlayService get locate => Locator.locate();
}