add reset option + fix some missing notifyListeners() calls

This commit is contained in:
2025-07-24 04:12:21 +00:00
parent 2894016cb7
commit 96e55a7157
2 changed files with 22 additions and 3 deletions
+17 -3
View File
@@ -26,7 +26,7 @@ class LazyCanvasController with ChangeNotifier {
final Size _hashCellSize; final Size _hashCellSize;
bool _init = false; bool _init = false;
late final SpatialHashing<int> _spatialHash; late final SpatialHashing<int> _spatialHash;
List<ChildInfo> _renderedChildren = []; List<ChildInfo> _renderedChildrenCache = [];
TickerProvider? _ticker; TickerProvider? _ticker;
bool debug; bool debug;
@@ -81,6 +81,7 @@ class LazyCanvasController with ChangeNotifier {
int addChild(Offset position, WidgetBuilder builder) { int addChild(Offset position, WidgetBuilder builder) {
_children[_nextId] = _ChildInfo(gsPosition: position, builder: builder); _children[_nextId] = _ChildInfo(gsPosition: position, builder: builder);
_spatialHash.add(position.toPoint(), _nextId); // add to spatial hash _spatialHash.add(position.toPoint(), _nextId); // add to spatial hash
notifyListeners();
return _nextId++; return _nextId++;
} }
@@ -91,12 +92,25 @@ class LazyCanvasController with ChangeNotifier {
} }
_spatialHash.remove(_children[id]!.gsPosition.toPoint()); _spatialHash.remove(_children[id]!.gsPosition.toPoint());
_children.remove(id); _children.remove(id);
notifyListeners();
}
/// Remove all children. Does not change where you are on the canvas.
void clear() {
_children.clear();
_spatialHash.clear();
_nextId = 0;
_renderedChildrenCache.clear();
_lastProcessedOffset = null;
_lastProcessedScale = null;
notifyListeners();
} }
/// Update the position of a child by its ID. /// Update the position of a child by its ID.
int updatePosition(int id, Offset newPosition) { int updatePosition(int id, Offset newPosition) {
if (_children.containsKey(id)) { if (_children.containsKey(id)) {
_children[id]!.gsPosition = newPosition; _children[id]!.gsPosition = newPosition;
notifyListeners();
return id; return id;
} else { } else {
throw _ChildNotFoundException; throw _ChildNotFoundException;
@@ -158,7 +172,7 @@ class LazyCanvasController with ChangeNotifier {
if (!_init) return []; if (!_init) return [];
if (_cleanRenderState && !forceRebuild) { if (_cleanRenderState && !forceRebuild) {
return _renderedChildren; return _renderedChildrenCache;
} }
_lastProcessedOffset = _gsTopLeftOffset; _lastProcessedOffset = _gsTopLeftOffset;
@@ -166,7 +180,7 @@ class LazyCanvasController with ChangeNotifier {
final idsToBuild = _childrenWithinBuildArea(_gsCenter, _buildExtent); final idsToBuild = _childrenWithinBuildArea(_gsCenter, _buildExtent);
return _renderedChildren = idsToBuild.map((id) { return _renderedChildrenCache = idsToBuild.map((id) {
final item = _children[id]!; final item = _children[id]!;
final ssPosition = gsToSs(item.gsPosition, _gsTopLeftOffset, _scale); final ssPosition = gsToSs(item.gsPosition, _gsTopLeftOffset, _scale);
var child = item.builder(context); var child = item.builder(context);
+5
View File
@@ -73,4 +73,9 @@ class SpatialHashing<T> {
return results; return results;
} }
void clear() {
_pointData.clear();
_cellMap.clear();
}
} }