improve keying logic to prevent superflous builds
This commit is contained in:
@@ -131,7 +131,11 @@ class LazyCanvasController with ChangeNotifier {
|
||||
CanvasChildId _addChildInternal(Offset position, Widget widget, {Size? childSize, CanvasChildId? id}) {
|
||||
assert(!useIdsFromArgs || useIdsFromArgs && id != null);
|
||||
id ??= _uuid.v4();
|
||||
_children[id] = _ChildInfo(gsPosition: position, widget: widget, lastRenderedSize: childSize);
|
||||
_children[id] = _ChildInfo(
|
||||
gsPosition: position,
|
||||
widget: Container(key: ValueKey<String>(id), child: widget),
|
||||
lastRenderedSize: childSize,
|
||||
);
|
||||
_spatialHash.add(position.toPoint(), id); // add to spatial hash
|
||||
return id;
|
||||
}
|
||||
@@ -177,10 +181,7 @@ class LazyCanvasController with ChangeNotifier {
|
||||
/// Update a child's widget.
|
||||
void updateChildWidget(CanvasChildId id, Widget newWidget) {
|
||||
if (_children.containsKey(id)) {
|
||||
final child = _children[id]!;
|
||||
// Create a new _ChildInfo with the new widget
|
||||
_children[id] = _ChildInfo(gsPosition: child.gsPosition, widget: newWidget)
|
||||
..lastRenderedSize = child.lastRenderedSize;
|
||||
_children[id]!.widget = Container(key: ValueKey<String>(id), child: newWidget);
|
||||
markDirty();
|
||||
} else {
|
||||
throw _ChildNotFoundException;
|
||||
@@ -271,7 +272,7 @@ class LazyCanvasController with ChangeNotifier {
|
||||
final item = _children[id]!;
|
||||
final ssPosition = gsToSs(item.gsPosition, _gsTopLeftOffset, _scale);
|
||||
var child = item.widget;
|
||||
if (debug) child = _Debug(id: id, gs: item.gsPosition, ss: ssPosition, child: child);
|
||||
if (debug) child = _Debug(key: ValueKey<String>(id), id: id, gs: item.gsPosition, ss: ssPosition, child: child);
|
||||
return ChildInfo(id: id, gsPosition: item.gsPosition, ssPosition: ssPosition, child: child);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ class _Debug extends StatelessWidget {
|
||||
final Offset gs, ss;
|
||||
final Widget child;
|
||||
|
||||
const _Debug({required this.id, required this.gs, required this.ss, required this.child});
|
||||
const _Debug({required this.id, required this.gs, required this.ss, required this.child, required super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -9,7 +9,7 @@ final _ChildNotFoundException = Exception('Child with the given ID does not exis
|
||||
class _ChildInfo {
|
||||
Offset gsPosition;
|
||||
Size? lastRenderedSize;
|
||||
final Widget widget;
|
||||
Widget widget;
|
||||
|
||||
_ChildInfo({required this.gsPosition, required this.widget, this.lastRenderedSize});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user