oc-front/lib/pages/catalog_item.dart
2025-02-04 17:02:49 +01:00

60 lines
2.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:localstorage/localstorage.dart';
import 'package:oc_front/core/models/workspace_local.dart';
import 'package:oc_front/core/sections/header/header.dart';
import 'package:oc_front/core/services/router.dart';
import 'package:oc_front/main.dart';
import 'package:oc_front/models/resources/resources.dart';
import 'package:oc_front/pages/abstract_page.dart';
import 'package:oc_front/pages/catalog.dart';
import 'package:oc_front/widgets/items/item.dart';
import 'package:oc_front/widgets/items/item_row.dart';
class CatalogItemFactory implements AbstractFactory {
static GlobalKey<CatalogItemPageWidgetState> key = GlobalKey<CatalogItemPageWidgetState>();
@override GlobalKey getKey() { return key; }
@override void back(BuildContext context) {
search(context, false);
}
@override String? getSearch() {
if ((localStorage.getItem("search") ?? "") == "") { return null; }
return localStorage.getItem("search")!.split(",")[0];
}
@override bool searchFill() { return false; }
@override Widget factory(GoRouterState state, List<String> args) {
var id = state.pathParameters[args.first];
try {
var item = CatalogFactory.key.currentState?.widget.items.firstWhere( (element) => element.id == id );
return CatalogItemPageWidget(item : item!);
} catch (e) {
var item = WorkspaceLocal.getItem(id ?? "", false);
if (item != null) { return CatalogItemPageWidget(item : item); }
return Container();
}
}
@override void search(BuildContext context, bool special) {
if (special) { return; } // T
var s = SearchConstants.get();
AppRouter.catalog.go(context, {});
Future.delayed(Duration(milliseconds: 10), () {
SearchConstants.set(s);
CatalogFactory().search(context, false);
});
}
}
// ignore: must_be_immutable
class CatalogItemPageWidget extends StatefulWidget {
AbstractItem item;
CatalogItemPageWidget ({ required this.item }) : super(key: CatalogItemFactory.key);
@override CatalogItemPageWidgetState createState() => CatalogItemPageWidgetState();
}
class CatalogItemPageWidgetState extends State<CatalogItemPageWidget> {
@override Widget build(BuildContext context) {
return Column( children: [
ItemRowWidget(contextWidth: getMainWidth(context), item: widget.item, readOnly: true,),
ItemWidget(item: widget.item),
]);
}
}