refactor canvas painter

This commit is contained in:
2025-07-27 01:41:17 +00:00
parent 2366102cce
commit e5f33c6212
7 changed files with 12 additions and 18 deletions
+1 -3
View File
@@ -65,9 +65,7 @@ class _CachingTestAppState extends State<CachingTestApp> {
style: TextStyle(fontSize: 12), style: TextStyle(fontSize: 12),
), ),
), ),
Expanded( Expanded(child: LazyCanvas(controller: controller)),
child: LazyCanvas(controller: controller, canvasBackground: const DotGridBackround()),
),
], ],
), ),
floatingActionButton: Column( floatingActionButton: Column(
+1 -3
View File
@@ -116,9 +116,7 @@ class _DynamicWidgetExampleState extends State<DynamicWidgetExample> {
], ],
), ),
), ),
Expanded( Expanded(child: LazyCanvas(controller: controller)),
child: LazyCanvas(controller: controller, canvasBackground: const DotGridBackround()),
),
], ],
), ),
); );
+1 -1
View File
@@ -78,7 +78,7 @@ class _SimpleExampleState extends State<SimpleExample> {
), ),
body: Stack( body: Stack(
children: [ children: [
LazyCanvas(controller: controller, canvasBackground: SingleColorBackround(Colors.white)), LazyCanvas(controller: controller),
Positioned(bottom: 64, left: 16, child: Fps()), Positioned(bottom: 64, left: 16, child: Fps()),
], ],
), ),
+2 -2
View File
@@ -34,12 +34,12 @@ class SingleColorBackround extends CanvasBackground {
} }
} }
class DotGridBackround extends CanvasBackground { class DotGridBackground extends CanvasBackground {
final double size; final double size;
final double spacing; final double spacing;
final Color color; 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 @override
void paint(Canvas canvas, Offset screenOffset, Offset canvasOffset, double scale, Size canvasSize) { void paint(Canvas canvas, Offset screenOffset, Offset canvasOffset, double scale, Size canvasSize) {
+3
View File
@@ -1,6 +1,7 @@
import 'dart:collection'; import 'dart:collection';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:infinite_lazy_grid/core/background.dart';
import '../../utils/measure_size.dart'; import '../../utils/measure_size.dart';
import '../spatial_hashing.dart'; import '../spatial_hashing.dart';
import '../../utils/offset_extensions.dart'; import '../../utils/offset_extensions.dart';
@@ -28,6 +29,7 @@ class LazyCanvasController with ChangeNotifier {
late BuildContext _context; late BuildContext _context;
bool _firstBuild = true; bool _firstBuild = true;
int? _focusChildOnInit; // if set, will focus on this child on first build int? _focusChildOnInit; // if set, will focus on this child on first build
CanvasBackground background;
bool debug; bool debug;
final Duration defaultAnimationDuration; final Duration defaultAnimationDuration;
@@ -37,6 +39,7 @@ class LazyCanvasController with ChangeNotifier {
Offset? buildCacheExtent, Offset? buildCacheExtent,
Size hashCellSize = const Size(100, 100), Size hashCellSize = const Size(100, 100),
this.defaultAnimationDuration = const Duration(milliseconds: 300), this.defaultAnimationDuration = const Duration(milliseconds: 300),
this.background = const DotGridBackground(),
}) : _hashCellSize = hashCellSize, }) : _hashCellSize = hashCellSize,
_buildCacheExtent = buildCacheExtent != null ? buildCacheExtent + Offset(50, 50) : null _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 // only top left is considered so if a widget has long width, it'll not be rendered
+2 -3
View File
@@ -9,10 +9,9 @@ import 'controller/controller.dart';
/// An infinite canvas that places all the children at the specified positions. /// 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. /// Needs a [LazyCanvasController] to control the canvas and a [CanvasBackground] to draw the background.
class LazyCanvas extends StatefulWidget { class LazyCanvas extends StatefulWidget {
final CanvasBackground canvasBackground;
final LazyCanvasController controller; final LazyCanvasController controller;
const LazyCanvas({required this.controller, required this.canvasBackground, super.key}); const LazyCanvas({required this.controller, super.key});
@override @override
State<LazyCanvas> createState() => _LazyCanvasState(); State<LazyCanvas> createState() => _LazyCanvasState();
@@ -47,7 +46,7 @@ class _LazyCanvasState extends State<LazyCanvas> with TickerProviderStateMixin<L
final children = childrenWithPositions.map((e) => e.child).toList(); final children = childrenWithPositions.map((e) => e.child).toList();
final canvas = _CanvasRenderObject( final canvas = _CanvasRenderObject(
childrenIds: childrenIds, childrenIds: childrenIds,
canvasBackground: widget.canvasBackground, canvasBackground: widget.controller.background,
ssPositions: ssPositions, ssPositions: ssPositions,
scale: widget.controller.scale, scale: widget.controller.scale,
gridSpaceOffset: widget.controller.offset, gridSpaceOffset: widget.controller.offset,
+2 -6
View File
@@ -19,9 +19,7 @@ void main() {
} }
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
home: Scaffold( home: Scaffold(body: LazyCanvas(controller: controller)),
body: LazyCanvas(controller: controller, canvasBackground: SingleColorBackround(Colors.white)),
),
), ),
); );
await tester.pumpAndSettle(); await tester.pumpAndSettle();
@@ -49,9 +47,7 @@ void main() {
controller.addChild(const Offset(100, 100), TestChild(index: 1)); controller.addChild(const Offset(100, 100), TestChild(index: 1));
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
home: Scaffold( home: Scaffold(body: LazyCanvas(controller: controller)),
body: LazyCanvas(controller: controller, canvasBackground: SingleColorBackround(Colors.white)),
),
), ),
); );
await tester.pumpAndSettle(); await tester.pumpAndSettle();