refactor buildcontext as internal

This commit is contained in:
2025-07-26 22:35:25 +00:00
parent ac81c8564a
commit 307dead2c4
4 changed files with 13 additions and 6 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ class _SimpleExampleState extends State<SimpleExample> {
GestureDetector( GestureDetector(
onTap: () { onTap: () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Tapped a container'))); ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Tapped a container')));
controller.focusOnChild(context, i); controller.focusOnChild(i);
}, },
child: InfoContainer(color: Colors.primaries[i % Colors.primaries.length]), child: InfoContainer(color: Colors.primaries[i % Colors.primaries.length]),
), ),
+7 -3
View File
@@ -25,6 +25,7 @@ class LazyCanvasController with ChangeNotifier {
bool _init = false; bool _init = false;
late final SpatialHashing<int> _spatialHash; late final SpatialHashing<int> _spatialHash;
TickerProvider? _ticker; TickerProvider? _ticker;
late BuildContext _context;
bool debug; bool debug;
@@ -71,6 +72,10 @@ class LazyCanvasController with ChangeNotifier {
_ticker = ticker; _ticker = ticker;
} }
void setBuildContext(BuildContext context) {
_context = context;
}
// ==================== Child Management ==================== // ==================== Child Management ====================
/// Add a child at a given position with a widget. Returns the child ID. /// Add a child at a given position with a widget. Returns the child ID.
@@ -174,7 +179,7 @@ class LazyCanvasController with ChangeNotifier {
// ==================== Positioning Logic ==================== // ==================== Positioning Logic ====================
/// Currently rendered widgets with their position info /// Currently rendered widgets with their position info
List<ChildInfo> widgetsWithScreenPositions(BuildContext context, {bool forceRebuild = false}) { List<ChildInfo> widgetsWithScreenPositions({bool forceRebuild = false}) {
if (!_init) return []; if (!_init) return [];
final idsToBuild = _childrenWithinBuildArea(_gsCenter, _buildExtent); final idsToBuild = _childrenWithinBuildArea(_gsCenter, _buildExtent);
@@ -218,7 +223,6 @@ class LazyCanvasController with ChangeNotifier {
/// an offstage rendering will be used ( double render ) /// an offstage rendering will be used ( double render )
/// Preferred horizontal margin used for [ScalingMode.fitInViewport]. /// Preferred horizontal margin used for [ScalingMode.fitInViewport].
void focusOnChild( void focusOnChild(
BuildContext context,
int id, { int id, {
ScalingMode scalingMode = ScalingMode.keepScale, ScalingMode scalingMode = ScalingMode.keepScale,
bool animate = true, bool animate = true,
@@ -236,7 +240,7 @@ class LazyCanvasController with ChangeNotifier {
// else do an offstage render // else do an offstage render
childSize ??= childInfo.lastRenderedSize != null && !forceRedraw childSize ??= childInfo.lastRenderedSize != null && !forceRedraw
? childInfo.lastRenderedSize ? childInfo.lastRenderedSize
: measureWidgetSize(context, (_) => childInfo.widget); : measureWidgetSize(_context, (_) => childInfo.widget);
/* /*
margin is symmatric on ltrb so margin is symmatric on ltrb so
+2 -1
View File
@@ -33,6 +33,7 @@ class _LazyCanvasState extends State<LazyCanvas> with TickerProviderStateMixin<L
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
widget.controller.setBuildContext(context);
return GestureDetector( return GestureDetector(
behavior: HitTestBehavior.translucent, behavior: HitTestBehavior.translucent,
onScaleUpdate: widget.controller.onScaleUpdate, onScaleUpdate: widget.controller.onScaleUpdate,
@@ -40,7 +41,7 @@ class _LazyCanvasState extends State<LazyCanvas> with TickerProviderStateMixin<L
child: ListenableBuilder( child: ListenableBuilder(
listenable: widget.controller, listenable: widget.controller,
builder: (_, _) { builder: (_, _) {
final childrenWithPositions = widget.controller.widgetsWithScreenPositions(context); final childrenWithPositions = widget.controller.widgetsWithScreenPositions();
final ssPositions = childrenWithPositions.map((e) => e.ssPosition).toList(); final ssPositions = childrenWithPositions.map((e) => e.ssPosition).toList();
final childrenIds = childrenWithPositions.map((e) => e.id).toList(); final childrenIds = childrenWithPositions.map((e) => e.id).toList();
final children = childrenWithPositions.map((e) => e.child).toList(); final children = childrenWithPositions.map((e) => e.child).toList();
+3 -1
View File
@@ -55,7 +55,9 @@ void main() {
), ),
); );
await tester.pumpAndSettle(); await tester.pumpAndSettle();
final context = tester.element(find.byType(LazyCanvas)); final context = tester.element(find.byType(LazyCanvas));
controller.setBuildContext(context);
// Initial size check // Initial size check
final finder = find.byType(TestChild); final finder = find.byType(TestChild);
@@ -66,7 +68,7 @@ void main() {
controller.onScaleUpdate(ScaleUpdateDetails(focalPoint: const Offset(0, 0), scale: 2.0)); controller.onScaleUpdate(ScaleUpdateDetails(focalPoint: const Offset(0, 0), scale: 2.0));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(controller.scale, 2.0); expect(controller.scale, 2.0);
final ssPositions = controller.widgetsWithScreenPositions(context).map((e) => e.ssPosition).toList(); final ssPositions = controller.widgetsWithScreenPositions().map((e) => e.ssPosition).toList();
expect(ssPositions, [Offset.zero, const Offset(200, 200)]); expect(ssPositions, [Offset.zero, const Offset(200, 200)]);
}); });
} }