-
Ive been trying to add some QOL (quality of life) features for my team while modifying the source code of the dashbaord (in Dart), but got into a problem, whenever i try to add a widget through code it isnt displayed, moves the copied widget to the pasted position, but when i restart the app (after saving), the duplicate is stacked on the original copy. static WidgetContainerModel? copy; copy method: void copyWidget(WidgetContainerModel widget) {
copy = widget;
} paste method: void pasteWidget(WidgetContainerModel widget, Offset globalPosition) {
Offset localPosition = getLocalPosition(globalPosition);
widget.displayRect =
Rect.fromCenter(center: localPosition, width: 200, height: 200);
widget.setPreviewRect(widget.displayRect);
widget.setDraggingRect(widget.displayRect);
if (widget is NTWidgetContainerModel &&
isValidLayoutLocation(widget.cursorGlobalLocation)) {
LayoutContainerModel layoutContainer =
getLayoutAtLocation(widget.cursorGlobalLocation)!;
if (layoutContainer.willAcceptWidget(widget)) {
layoutContainer.addWidget(widget);
}
}
_widgetModels.add(widget);
refresh();
} usage: dashboardWidgets.add(
GestureDetector(
onTap: () {},
onSecondaryTapUp: (details) {
if (Settings.layoutLocked) {
return;
}
List<ContextMenuEntry> menuEntries = [
MenuHeader(
text: container.title ?? '',
disableUppercase: true,
),
const MenuDivider(),
MenuItem(
label: 'Edit Properties',
icon: Icons.edit_outlined,
onSelected: () {
container.showEditProperties(context);
},
),
...container.getContextMenuItems(),
MenuItem(
label: 'Remove',
icon: Icons.delete_outlined,
onSelected: () {
removeWidget(container);
}),
MenuItem(
label: 'Copy',
icon: Icons.copy_outlined,
onSelected: () {
copyWidget(container);
})
];
ContextMenu contextMenu = ContextMenu(
position: details.globalPosition,
borderRadius: BorderRadius.circular(5.0),
padding: const EdgeInsets.all(4.0),
entries: menuEntries,
);
showContextMenu(
context,
contextMenu: contextMenu,
transitionDuration: const Duration(milliseconds: 100),
reverseTransitionDuration: Duration.zero,
transitionsBuilder:
(context, animation, secondaryAnimation, child) {
return FadeTransition(
opacity: animation,
child: child,
);
},
);
},
child: getWidgetFromModel(container),
),
);
}
...
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {},
onSecondaryTapUp: (details) {
if (Settings.layoutLocked) {
return;
}
List<MenuItem> contextMenuEntries = [
MenuItem(
label: 'Add Widget',
icon: Icons.add,
onSelected: () => onAddWidgetPressed.call(),
),
MenuItem(
label: 'Clear Layout',
icon: Icons.clear,
onSelected: () => clearWidgets(context),
),
];
if (copy != null) {
contextMenuEntries.add(
MenuItem(
label: 'Paste',
icon: Icons.paste_outlined,
onSelected: () {
pasteWidget(copy!, details.globalPosition);
},
),
);
}
ContextMenu contextMenu = ContextMenu(
position: details.globalPosition,
borderRadius: BorderRadius.circular(5.0),
padding: const EdgeInsets.all(4.0),
entries: contextMenuEntries,
);
showContextMenu(
context,
contextMenu: contextMenu,
transitionDuration: const Duration(milliseconds: 100),
reverseTransitionDuration: Duration.zero,
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return FadeTransition(
opacity: animation,
child: child,
);
},
);
},... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Resolved, here was the fix: void pasteWidget(WidgetContainerModel? widget, Offset globalPosition) {
if (widget == null) return;
Map<String, dynamic> jsonData = widget.toJson();
jsonData['x'] = getLocalPosition(globalPosition).dx;
jsonData['y'] = getLocalPosition(globalPosition).dy;
WidgetContainerModel createdWidget = createWidgetFromJson(jsonData);
_widgetModels.add(createdWidget);
refresh();
}
WidgetContainerModel createWidgetFromJson(Map<String, dynamic> json) {
String type = json['type'];
if (json['type'] == 'List Layout') {
switch (type) {
case 'List Layout':
return ListLayoutModel.fromJson(
jsonData: json, tabGrid: this, onDragCancel: null);
default:
throw ArgumentError('Unknown type: $type');
}
} else {
return NTWidgetContainerModel.fromJson(
enabled: ntConnection.isNT4Connected,
jsonData: json,
onJsonLoadingWarning: null,
);
}
}
in short, i copy the json data from the original, and create a new model using it, while modfying the position on the axis to the position passed by the function |
Beta Was this translation helpful? Give feedback.
Resolved, here was the fix: