nc-photos/app/lib/snack_bar_manager.dart

66 lines
1.8 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
/// Showing snack bars
///
/// This manager helps showing a snack bar even after the context was
/// invalidated by having another widget (presumably top-level) to handle such
/// request in a decoupled way
class SnackBarManager {
factory SnackBarManager() => _inst;
SnackBarManager._();
void registerHandler(SnackBarHandler handler) {
_handlers.add(handler);
}
void unregisterHandler(SnackBarHandler handler) {
_handlers.remove(handler);
}
/// Show a snack bar if possible
///
/// If the snack bar can't be shown at this time, return null.
///
/// If [canBeReplaced] is true, this snackbar will be dismissed by the next
/// snack bar
2021-07-23 22:05:57 +02:00
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? showSnackBar(
SnackBar snackBar, {
bool canBeReplaced = false,
}) {
if (_canPrevBeReplaced) {
_prevController?.close();
}
_canPrevBeReplaced = canBeReplaced;
2021-04-10 06:28:12 +02:00
for (final h in _handlers.reversed) {
final result = h.showSnackBar(snackBar);
if (result != null) {
_prevController = result;
result.closed.whenComplete(() {
if (identical(_prevController, result)) {
_prevController = null;
}
});
2021-04-10 06:28:12 +02:00
return result;
}
}
_log.warning("[showSnackBar] No handler available");
_prevController = null;
2021-04-10 06:28:12 +02:00
return null;
}
final _handlers = <SnackBarHandler>[];
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? _prevController;
var _canPrevBeReplaced = false;
2021-04-10 06:28:12 +02:00
static final _inst = SnackBarManager._();
2022-04-01 18:53:22 +02:00
static final _log = Logger("snack_bar_manager.SnackBarManager");
2021-04-10 06:28:12 +02:00
}
abstract class SnackBarHandler {
2021-07-23 22:05:57 +02:00
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? showSnackBar(
2021-04-10 06:28:12 +02:00
SnackBar snackBar);
}