diff --git a/example/lib/app.dart b/example/lib/app.dart index 129869b..e83db05 100644 --- a/example/lib/app.dart +++ b/example/lib/app.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:infinite_lazy_2d_grid/infinite_lazy_2d_grid.dart'; +import 'package:infinite_lazy_grid/infinite_lazy_grid.dart'; import 'widgets/fps.dart'; @@ -11,7 +11,7 @@ class App extends StatefulWidget { } class _AppState extends State { - final CanvasController controller = CanvasController(debug: true); + final LazyCanvasController controller = LazyCanvasController(debug: true); @override void initState() { @@ -53,7 +53,7 @@ class _AppState extends State { ), body: Stack( children: [ - CanvasView(controller: controller, canvasBackground: SingleColorBackround(Colors.white)), + LazyCanvas(controller: controller, canvasBackground: SingleColorBackround(Colors.white)), Positioned(bottom: 64, left: 16, child: Fps()), ], ), diff --git a/example/lib/main.dart b/example/lib/main.dart index b184e69..fadf9bb 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,4 +1,4 @@ -import 'package:example/app.dart'; +import './app.dart'; import 'package:flutter/material.dart'; void main() { diff --git a/example/pubspec.lock b/example/pubspec.lock index 7596955..59f9fe2 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -75,7 +75,7 @@ packages: description: flutter source: sdk version: "0.0.0" - infinite_lazy_2d_grid: + infinite_lazy_grid: dependency: "direct main" description: path: ".." diff --git a/example/pubspec.yaml b/example/pubspec.yaml index e2d53db..97f271d 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -11,7 +11,7 @@ dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.8 - infinite_lazy_2d_grid: + infinite_lazy_grid: path: .. dev_dependencies: diff --git a/lib/core/background.dart b/lib/core/background.dart index 5f59952..0f2cef6 100644 --- a/lib/core/background.dart +++ b/lib/core/background.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +import './render.dart'; +/// Abstract definition for a [LazyCanvas] background. abstract class CanvasBackground { /// the fill color of background final Color bgColor; diff --git a/lib/core/controller/controller.dart b/lib/core/controller/controller.dart index de730e6..453dc0e 100644 --- a/lib/core/controller/controller.dart +++ b/lib/core/controller/controller.dart @@ -1,18 +1,19 @@ import 'dart:collection'; import 'package:flutter/material.dart'; -import 'package:infinite_lazy_2d_grid/utils/measure_size.dart'; +import '../../utils/measure_size.dart'; import '../spatial_hashing.dart'; import '../../utils/offset_extensions.dart'; import '../../utils/size_extensions.dart'; import '../../utils/conversions.dart'; import '../../utils/styles.dart'; +import '../render.dart'; part 'debug.dart'; part 'types.dart'; -/// Every canvas view needs one and this handles the positioning logic -class CanvasController with ChangeNotifier { +/// Controller for [LazyCanvas] +class LazyCanvasController with ChangeNotifier { int _nextId = 0; // surely we won't run out of IDs, Clueless Offset _gsTopLeftOffset = Offset.zero; Offset? _lastProcessedOffset; @@ -30,7 +31,7 @@ class CanvasController with ChangeNotifier { bool debug; - CanvasController({this.debug = false, Offset? buildCacheExtent, Size hashCellSize = const Size(100, 100)}) + LazyCanvasController({this.debug = false, Offset? buildCacheExtent, Size hashCellSize = const Size(100, 100)}) : _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 @@ -64,10 +65,12 @@ class CanvasController with ChangeNotifier { } } + /// Called when a child widget's size changes. void onChildSizeChange(int id, Size size) { _children[id]!.lastRenderedSize = size; } + /// Set the ticker provider for animations. void setTickerProvider(TickerProvider ticker) { _ticker = ticker; } @@ -139,7 +142,7 @@ class CanvasController with ChangeNotifier { notifyListeners(); } - /// Increment or decrement the scale by a delta value. + /// Increment or decrement the scale by an additive delta value. void updateScalebyDelta(double delta) { final focalPoint = Offset(canvasSize.width / 2, canvasSize.height / 2); final newScale = _scale + delta; @@ -259,6 +262,7 @@ class CanvasController with ChangeNotifier { // ==================== Animation ==================== + /// Animate the canvas to a new offset and scale Future animateToOffsetAndScale({ required Offset offset, required double scale, diff --git a/lib/core/render.dart b/lib/core/render.dart index 0781bf4..2a3ea28 100644 --- a/lib/core/render.dart +++ b/lib/core/render.dart @@ -1,23 +1,24 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; -import 'package:infinite_lazy_2d_grid/core/controller/controller.dart'; -import 'package:infinite_lazy_2d_grid/utils/offset_extensions.dart'; -import 'package:infinite_lazy_2d_grid/utils/styles.dart'; +import '../utils/offset_extensions.dart'; +import '../utils/styles.dart'; import 'background.dart'; +import 'controller/controller.dart'; /// An infinite canvas that places all the children at the specified positions. -class CanvasView extends StatefulWidget { +/// Needs a [LazyCanvasController] to control the canvas and a [CanvasBackground] to draw the background. +class LazyCanvas extends StatefulWidget { final CanvasBackground canvasBackground; - final CanvasController controller; + final LazyCanvasController controller; - const CanvasView({required this.controller, required this.canvasBackground, super.key}); + const LazyCanvas({required this.controller, required this.canvasBackground, super.key}); @override - State createState() => _CanvasViewState(); + State createState() => _LazyCanvasState(); } -class _CanvasViewState extends State with TickerProviderStateMixin { +class _LazyCanvasState extends State with TickerProviderStateMixin { @override void initState() { super.initState(); diff --git a/lib/core/spatial_hashing.dart b/lib/core/spatial_hashing.dart index 2546723..74ceb64 100644 --- a/lib/core/spatial_hashing.dart +++ b/lib/core/spatial_hashing.dart @@ -12,6 +12,8 @@ class PointData { PointData(this.point, this.data); } +/// Data structure for spatial hashing to get widgets to be built quickly +/// based on their position in a 2D grid. class SpatialHashing { final Size cellSize; final HashMap _pointData = HashMap(); diff --git a/lib/infinite_lazy_2d_grid.dart b/lib/infinite_lazy_grid.dart similarity index 100% rename from lib/infinite_lazy_2d_grid.dart rename to lib/infinite_lazy_grid.dart diff --git a/pubspec.yaml b/pubspec.yaml index 393afde..1affd9c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,4 +1,4 @@ -name: infinite_lazy_2d_grid +name: infinite_lazy_grid description: "A new Flutter package project." version: 0.0.1 homepage: diff --git a/test/canvas_view_test.dart b/test/canvas_view_test.dart index ce671f9..62e32f9 100644 --- a/test/canvas_view_test.dart +++ b/test/canvas_view_test.dart @@ -1,8 +1,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/material.dart'; -import 'package:infinite_lazy_2d_grid/core/render.dart'; -import 'package:infinite_lazy_2d_grid/core/controller/controller.dart'; -import 'package:infinite_lazy_2d_grid/core/background.dart'; +import 'package:infinite_lazy_grid/infinite_lazy_grid.dart'; class TestChild extends StatelessWidget { final int index; @@ -15,14 +13,14 @@ class TestChild extends StatelessWidget { void main() { testWidgets('CanvasView renders only visible children and reduces count on zoom out', (WidgetTester tester) async { - final controller = CanvasController(debug: true); + final controller = LazyCanvasController(debug: true); for (int i = 0; i < 10000; i++) { controller.addChild(Offset((i % 80) * 100.0, (i ~/ 80) * 100.0), (_) => TestChild(index: i)); } await tester.pumpWidget( MaterialApp( home: Scaffold( - body: CanvasView(controller: controller, canvasBackground: SingleColorBackround(Colors.white)), + body: LazyCanvas(controller: controller, canvasBackground: SingleColorBackround(Colors.white)), ), ), ); @@ -46,18 +44,18 @@ void main() { }); testWidgets('CanvasView scales as expected', (WidgetTester tester) async { - final controller = CanvasController(debug: true); + final controller = LazyCanvasController(debug: true); controller.addChild(const Offset(0, 0), (_) => TestChild(index: 0)); controller.addChild(const Offset(100, 100), (_) => TestChild(index: 1)); await tester.pumpWidget( MaterialApp( home: Scaffold( - body: CanvasView(controller: controller, canvasBackground: SingleColorBackround(Colors.white)), + body: LazyCanvas(controller: controller, canvasBackground: SingleColorBackround(Colors.white)), ), ), ); await tester.pumpAndSettle(); - final context = tester.element(find.byType(CanvasView)); + final context = tester.element(find.byType(LazyCanvas)); // Initial size check final finder = find.byType(TestChild); diff --git a/test/core/spatial_hashing_test.dart b/test/core/spatial_hashing_test.dart index 4b57f17..7ef1c3b 100644 --- a/test/core/spatial_hashing_test.dart +++ b/test/core/spatial_hashing_test.dart @@ -1,5 +1,5 @@ import 'package:flutter_test/flutter_test.dart'; -import 'package:infinite_lazy_2d_grid/core/spatial_hashing.dart'; +import 'package:infinite_lazy_grid/core/spatial_hashing.dart'; import 'dart:math'; import 'package:flutter/material.dart';