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