[infra_ui][overlay] Add delegate to overlay item

This commit is contained in:
Jaylen Bian 2021-07-28 17:35:57 +08:00
parent 9508ad84e4
commit ccca15d361

View File

@ -1,6 +1,5 @@
import 'package:dartz/dartz.dart' show Tuple2;
import 'package:dartz/dartz.dart' show Tuple3;
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
/// Specifies how overlay are anchored to the SourceWidget
enum AnchorDirection {
@ -53,6 +52,10 @@ TransitionBuilder overlayManagerBuilder() {
};
}
abstract class FlowyOverlayDelegate {
void didRemove();
}
class FlowyOverlay extends StatefulWidget {
const FlowyOverlay({
Key? key,
@ -105,22 +108,26 @@ class FlowyOverlay extends StatefulWidget {
}
class FlowyOverlayState extends State<FlowyOverlay> {
List<Tuple2<Widget, String>> _overlayList = [];
List<Tuple3<Widget, String, FlowyOverlayDelegate?>> _overlayList = [];
void insert(Widget widget, String identifier) {
void insert(Widget widget, String identifier, FlowyOverlayDelegate? delegate) {
setState(() {
_overlayList.add(Tuple2(widget, identifier));
_overlayList.add(Tuple3(widget, identifier, delegate));
});
}
void remove(String identifier) {
setState(() {
_overlayList.removeWhere((ele) => ele.value2 == identifier);
final index = _overlayList.indexWhere((ele) => ele.value2 == identifier);
_overlayList.removeAt(index).value3?.didRemove();
});
}
void removeAll() {
setState(() {
for (var ele in _overlayList.reversed) {
ele.value3?.didRemove();
}
_overlayList = [];
});
}