mutex.dart 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'package:flutter/material.dart';
  2. import 'popover.dart';
  3. /// If multiple popovers are exclusive,
  4. /// pass the same mutex to them.
  5. class PopoverMutex {
  6. final _PopoverStateNotifier _stateNotifier = _PopoverStateNotifier();
  7. PopoverMutex();
  8. void removePopoverListener(VoidCallback listener) {
  9. _stateNotifier.removeListener(listener);
  10. }
  11. VoidCallback listenOnPopoverChanged(VoidCallback callback) {
  12. listenerCallback() {
  13. callback();
  14. }
  15. _stateNotifier.addListener(listenerCallback);
  16. return listenerCallback;
  17. }
  18. void close() => _stateNotifier.state?.close();
  19. PopoverState? get state => _stateNotifier.state;
  20. set state(PopoverState? newState) => _stateNotifier.state = newState;
  21. void removeState() {
  22. _stateNotifier.state = null;
  23. }
  24. void dispose() {
  25. _stateNotifier.dispose();
  26. }
  27. }
  28. class _PopoverStateNotifier extends ChangeNotifier {
  29. PopoverState? _state;
  30. PopoverState? get state => _state;
  31. set state(PopoverState? newState) {
  32. if (_state != null && _state != newState) {
  33. _state?.close();
  34. }
  35. _state = newState;
  36. notifyListeners();
  37. }
  38. }