define a type for id and use uuids instead of existing int

This commit is contained in:
2025-07-27 20:03:13 +00:00
parent 61d0ec4e67
commit 0b4ffc1ade
8 changed files with 117 additions and 66 deletions
+9 -4
View File
@@ -12,6 +12,11 @@ class DynamicWidgetExample extends StatefulWidget {
class _DynamicWidgetExampleState extends State<DynamicWidgetExample> {
final LazyCanvasController controller = LazyCanvasController(debug: false);
// Store child IDs for later reference
late CanvasChildId selfManagedId;
late CanvasChildId externalDataId;
late CanvasChildId reactiveId;
// Simulate some dynamic data
int counter = 0;
String message = "Hello World";
@@ -25,16 +30,16 @@ class _DynamicWidgetExampleState extends State<DynamicWidgetExample> {
void _setupWidgets() {
// Approach 1: Using StatefulWidget with state inside the widget getting lost if unmounted
controller.addChild(const Offset(0, 0), SelfManagedCounterWidget(label: "Self-Managed"));
selfManagedId = controller.addChild(const Offset(0, 0), SelfManagedCounterWidget(label: "Self-Managed"));
// Approach 2: Using updateChildWidget, you need this since the children dep tree is direcly managed by the controller
controller.addChild(
externalDataId = controller.addChild(
const Offset(200, 0),
ExternalDataWidget(counter: counter, message: message, color: selectedColor),
);
// Approach 3: Using ValueListenableBuilder for reactive updates
controller.addChild(
reactiveId = controller.addChild(
const Offset(400, 0),
ValueListenableBuilder<int>(
valueListenable: _counterNotifier,
@@ -71,7 +76,7 @@ class _DynamicWidgetExampleState extends State<DynamicWidgetExample> {
// Approach 2: Update the widget when external data changes
// Use ValueKey to ensure Flutter recognizes this as a different widget
controller.updateChildWidget(
1, // Widget ID (second widget added)
externalDataId, // Use the stored widget ID
ExternalDataWidget(
// key: ValueKey('external_$counter'), // Force rebuild with unique key
counter: counter,
+4 -2
View File
@@ -12,21 +12,23 @@ class SimpleExample extends StatefulWidget {
class _SimpleExampleState extends State<SimpleExample> {
final LazyCanvasController controller = LazyCanvasController(debug: true);
final List<CanvasChildId> childIds = [];
@override
void initState() {
super.initState();
for (int i = 0; i < 10; i++) {
controller.addChild(
final id = controller.addChild(
Offset((i % 80) * 100.0, (i ~/ 80) * 100.0),
GestureDetector(
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Tapped a container')));
controller.focusOnChild(i);
controller.focusOnChild(childIds[i]);
},
child: InfoContainer(color: Colors.primaries[i % Colors.primaries.length]),
),
);
childIds.add(id);
}
controller.addChild(
const Offset(0, 200),
+40
View File
@@ -41,6 +41,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.19.1"
crypto:
dependency: transitive
description:
name: crypto
sha256: "1e445881f28f22d6140f181e07737b22f1e099a5e1ff94b0af2f9e4a463f4855"
url: "https://pub.dev"
source: hosted
version: "3.0.6"
cupertino_icons:
dependency: "direct main"
description:
@@ -57,6 +65,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.3"
fixnum:
dependency: transitive
description:
name: fixnum
sha256: b6dc7065e46c974bc7c5f143080a6764ec7a4be6da1285ececdc37be96de53be
url: "https://pub.dev"
source: hosted
version: "1.1.1"
flutter:
dependency: "direct main"
description: flutter
@@ -159,6 +175,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.10.1"
sprintf:
dependency: transitive
description:
name: sprintf
sha256: "1fc9ffe69d4df602376b52949af107d8f5703b77cda567c4d7d86a0693120f23"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
stack_trace:
dependency: transitive
description:
@@ -199,6 +223,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.7.4"
typed_data:
dependency: transitive
description:
name: typed_data
sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006
url: "https://pub.dev"
source: hosted
version: "1.4.0"
uuid:
dependency: transitive
description:
name: uuid
sha256: a5be9ef6618a7ac1e964353ef476418026db906c4facdedaa299b7a2e71690ff
url: "https://pub.dev"
source: hosted
version: "4.5.1"
vector_math:
dependency: transitive
description: