Skip to content

Commit

Permalink
feat: Meta widget (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
lesleysin authored Feb 24, 2024
1 parent c028781 commit d78c3fd
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/src/attributes/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export 'slider_attributes.dart';
export 'fitted_box_attributes.dart';
export 'switch_attributes.dart';
export 'subtree_attributes.dart';
export 'meta_attributes.dart';
22 changes: 22 additions & 0 deletions lib/src/attributes/meta_attributes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:duit_kernel/duit_kernel.dart';
import 'package:flutter_duit/src/ui/models/attended_model.dart';

final class MetaAttributes extends AttendedModel<Map<String, dynamic>>
implements DuitAttributes<MetaAttributes> {
MetaAttributes({
required super.value,
});

factory MetaAttributes.fromJson(Map<String, dynamic> json) {
return MetaAttributes(
value: json,
);
}

@override
MetaAttributes copyWith(MetaAttributes other) {
return MetaAttributes(
value: other.value,
);
}
}
37 changes: 37 additions & 0 deletions lib/src/ui/models/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,25 @@ base class DuitElement<T> extends TreeElement<T> with WidgetFabric {
child: child,
controlled: controlled,
);
case ElementType.meta:
final child = await DuitElement.fromJson(json["child"], driver);

return MetaUiElement(
type: type,
id: id,
attributes: attributes,
viewController: _createAndAttachController(
id,
controlled,
attributes,
serverAction,
driver,
type,
tag,
),
child: child,
controlled: controlled,
);
case ElementType.lifecycleStateListener:
final child = await DuitElement.fromJson(json["child"], driver);

Expand Down Expand Up @@ -763,6 +782,24 @@ base class DuitElement<T> extends TreeElement<T> with WidgetFabric {
}

//<editor-fold desc="Inherited models">
final class MetaUiElement<T> extends DuitElement<T>
implements SingleChildLayout {
//<editor-fold desc="Properties and ctor">
@override
DuitElement child;

MetaUiElement({
required super.type,
required super.id,
required super.controlled,
required super.viewController,
required super.attributes,
required this.child,
});

//</editor-fold>
}

final class SingleChildScrollviewUiElement<T> extends DuitElement<T>
implements SingleChildLayout {
//<editor-fold desc="Properties and ctor">
Expand Down
1 change: 1 addition & 0 deletions lib/src/ui/models/element_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ final class ElementType {
static const fittedBox = "FittedBox";
static const switchW = "Switch";
static const subtree = "Subtree";
static const meta = "Meta";
}
1 change: 1 addition & 0 deletions lib/src/ui/widgets/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export 'slider.dart';
export 'fitted_box.dart';
export 'switch.dart';
export 'component.dart';
export 'meta.dart';
32 changes: 32 additions & 0 deletions lib/src/ui/widgets/meta.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import "package:duit_kernel/duit_kernel.dart";
import "package:flutter/material.dart";
import "package:flutter_duit/flutter_duit.dart";
import "package:flutter_duit/src/attributes/index.dart";

class DuitMetaWidget extends StatefulWidget {
final Widget child;
final UIElementController controller;

const DuitMetaWidget({
super.key,
required this.child,
required this.controller,
});

@override
State<DuitMetaWidget> createState() => _DuitMetaWidgetState();
}

class _DuitMetaWidgetState extends State<DuitMetaWidget>
with ViewControllerChangeListener<DuitMetaWidget, MetaAttributes> {
@override
void initState() {
attachStateToController(widget.controller);
super.initState();
}

@override
Widget build(BuildContext context) {
return widget.child;
}
}
8 changes: 8 additions & 0 deletions lib/src/ui/widgets/widget_fabric.dart
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@ mixin WidgetFabric {
controller: it.viewController!,
child: child,
);
case ElementType.meta:
final it = model as MetaUiElement;
final child = getWidgetFromElement(it.child);

return DuitMetaWidget(
controller: it.viewController!,
child: child,
);
case ElementType.empty:
return const DuitEmptyView();
case ElementType.custom:
Expand Down
5 changes: 4 additions & 1 deletion lib/src/utils/attribute_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ final class AttributeParser implements AttributeParserBase {
ElementType.slider => SliderAttributes.fromJson(json ?? {}),
ElementType.fittedBox => FittedBoxAttributes.fromJson(json ?? {}),
ElementType.switchW => SwitchAttributes.fromJson(json ?? {}),
ElementType.subtree || ElementType.component => SubtreeAttributes.fromJson(json ?? {}),
ElementType.subtree ||
ElementType.component =>
SubtreeAttributes.fromJson(json ?? {}),
ElementType.meta => MetaAttributes.fromJson(json ?? {}),
ElementType.empty || String() => EmptyAttributes(),
};

Expand Down

0 comments on commit d78c3fd

Please sign in to comment.