diff --git a/example/lib/caching_test.dart b/example/lib/caching_test.dart index c65f8b3..5b4f552 100644 --- a/example/lib/caching_test.dart +++ b/example/lib/caching_test.dart @@ -65,9 +65,7 @@ class _CachingTestAppState extends State { style: TextStyle(fontSize: 12), ), ), - Expanded( - child: LazyCanvas(controller: controller, canvasBackground: const DotGridBackround()), - ), + Expanded(child: LazyCanvas(controller: controller)), ], ), floatingActionButton: Column( diff --git a/example/lib/dynamic_widget_example.dart b/example/lib/dynamic_widget_example.dart index ac3a2d0..dae6783 100644 --- a/example/lib/dynamic_widget_example.dart +++ b/example/lib/dynamic_widget_example.dart @@ -116,9 +116,7 @@ class _DynamicWidgetExampleState extends State { ], ), ), - Expanded( - child: LazyCanvas(controller: controller, canvasBackground: const DotGridBackround()), - ), + Expanded(child: LazyCanvas(controller: controller)), ], ), ); diff --git a/example/lib/simple_example.dart b/example/lib/simple_example.dart index 8e9f251..7328ef5 100644 --- a/example/lib/simple_example.dart +++ b/example/lib/simple_example.dart @@ -78,7 +78,7 @@ class _SimpleExampleState extends State { ), body: Stack( children: [ - LazyCanvas(controller: controller, canvasBackground: SingleColorBackround(Colors.white)), + LazyCanvas(controller: controller), Positioned(bottom: 64, left: 16, child: Fps()), ], ), diff --git a/lib/core/background.dart b/lib/core/background.dart index fe58704..d7456b6 100644 --- a/lib/core/background.dart +++ b/lib/core/background.dart @@ -34,12 +34,12 @@ class SingleColorBackround extends CanvasBackground { } } -class DotGridBackround extends CanvasBackground { +class DotGridBackground extends CanvasBackground { final double size; final double spacing; final Color color; - const DotGridBackround({this.size = 2.0, this.spacing = 50.0, this.color = Colors.black12}) : super(); + const DotGridBackground({this.size = 2.0, this.spacing = 50.0, this.color = Colors.black12}) : super(); @override void paint(Canvas canvas, Offset screenOffset, Offset canvasOffset, double scale, Size canvasSize) { diff --git a/lib/core/controller/controller.dart b/lib/core/controller/controller.dart index db2e9e7..7cb590a 100644 --- a/lib/core/controller/controller.dart +++ b/lib/core/controller/controller.dart @@ -1,6 +1,7 @@ import 'dart:collection'; import 'package:flutter/material.dart'; +import 'package:infinite_lazy_grid/core/background.dart'; import '../../utils/measure_size.dart'; import '../spatial_hashing.dart'; import '../../utils/offset_extensions.dart'; @@ -28,6 +29,7 @@ class LazyCanvasController with ChangeNotifier { late BuildContext _context; bool _firstBuild = true; int? _focusChildOnInit; // if set, will focus on this child on first build + CanvasBackground background; bool debug; final Duration defaultAnimationDuration; @@ -37,6 +39,7 @@ class LazyCanvasController with ChangeNotifier { Offset? buildCacheExtent, Size hashCellSize = const Size(100, 100), this.defaultAnimationDuration = const Duration(milliseconds: 300), + this.background = const DotGridBackground(), }) : _hashCellSize = hashCellSize, _buildCacheExtent = buildCacheExtent != null ? buildCacheExtent + Offset(50, 50) : null // only top left is considered so if a widget has long width, it'll not be rendered diff --git a/lib/core/render.dart b/lib/core/render.dart index 3f723f5..4670d1b 100644 --- a/lib/core/render.dart +++ b/lib/core/render.dart @@ -9,10 +9,9 @@ import 'controller/controller.dart'; /// An infinite canvas that places all the children at the specified positions. /// Needs a [LazyCanvasController] to control the canvas and a [CanvasBackground] to draw the background. class LazyCanvas extends StatefulWidget { - final CanvasBackground canvasBackground; final LazyCanvasController controller; - const LazyCanvas({required this.controller, required this.canvasBackground, super.key}); + const LazyCanvas({required this.controller, super.key}); @override State createState() => _LazyCanvasState(); @@ -47,7 +46,7 @@ class _LazyCanvasState extends State with TickerProviderStateMixin e.child).toList(); final canvas = _CanvasRenderObject( childrenIds: childrenIds, - canvasBackground: widget.canvasBackground, + canvasBackground: widget.controller.background, ssPositions: ssPositions, scale: widget.controller.scale, gridSpaceOffset: widget.controller.offset, diff --git a/test/canvas_view_test.dart b/test/canvas_view_test.dart index e28f8a0..1d8cc3f 100644 --- a/test/canvas_view_test.dart +++ b/test/canvas_view_test.dart @@ -19,9 +19,7 @@ void main() { } await tester.pumpWidget( MaterialApp( - home: Scaffold( - body: LazyCanvas(controller: controller, canvasBackground: SingleColorBackround(Colors.white)), - ), + home: Scaffold(body: LazyCanvas(controller: controller)), ), ); await tester.pumpAndSettle(); @@ -49,9 +47,7 @@ void main() { controller.addChild(const Offset(100, 100), TestChild(index: 1)); await tester.pumpWidget( MaterialApp( - home: Scaffold( - body: LazyCanvas(controller: controller, canvasBackground: SingleColorBackround(Colors.white)), - ), + home: Scaffold(body: LazyCanvas(controller: controller)), ), ); await tester.pumpAndSettle();