Skip to content

Commit

Permalink
Added ConstrainedBox attributes, model, widget variants
Browse files Browse the repository at this point in the history
  • Loading branch information
lesleysin committed Nov 24, 2024
1 parent a5e36b4 commit 62e81c8
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 7 deletions.
11 changes: 11 additions & 0 deletions lib/src/animations/animated_prop_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extension type AnimatedPropHelper(Map<String, dynamic> json) implements Map {
String? get parentBuilderId => json["parentBuilderId"];

Iterable<String>? get affectedProperties {
if (json.containsKey("affectedProperties")) {
return Set.from(json["affectedProperties"]);
} else {
return null;
}
}
}
1 change: 1 addition & 0 deletions lib/src/animations/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export 'animation_method.dart';
export 'animation_trigger.dart';
export 'controller_subscription.dart';
export 'animation_command.dart';
export 'animated_prop_helper.dart';
4 changes: 2 additions & 2 deletions lib/src/animations/tween_description.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ base class DuitTweenDescription<T> {
"boxConstraintsTween" => BoxConstraintsTweenDescription(
animatedPropKey: json["animatedPropKey"],
duration: AttributeValueMapper.toDuration(json["duration"]),
begin: AttributeValueMapper.toBoxConstraints(json["begin"])!,
end: AttributeValueMapper.toBoxConstraints(json["end"])!,
begin: AttributeValueMapper.toBoxConstraints(json["begin"]),
end: AttributeValueMapper.toBoxConstraints(json["end"]),
curve: AttributeValueMapper.toCurve(json["curve"]),
trigger: _triggerFromValue(json["trigger"]),
method: _methodFromValue(json["method"]),
Expand Down
47 changes: 47 additions & 0 deletions lib/src/attributes/constrained_box_attributes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import "package:duit_kernel/duit_kernel.dart";
import "package:flutter/material.dart";
import "package:flutter_duit/flutter_duit.dart";
import "package:flutter_duit/src/animations/animated_prop_helper.dart";

final class ConstrainedBoxAttributes extends AnimatedPropertyOwner
implements DuitAttributes<ConstrainedBoxAttributes> {
final BoxConstraints constraints;

ConstrainedBoxAttributes({
required super.parentBuilderId,
required super.affectedProperties,
required this.constraints,
});

factory ConstrainedBoxAttributes.fromJson(Map<String, dynamic> json) {
final props = AnimatedPropHelper(json);

return ConstrainedBoxAttributes(
parentBuilderId: props.parentBuilderId,
affectedProperties: props.affectedProperties,
constraints: AttributeValueMapper.toBoxConstraints(props["constraints"]),
);
}

@override
ConstrainedBoxAttributes copyWith(ConstrainedBoxAttributes other) {
return ConstrainedBoxAttributes(
constraints: other.constraints,
parentBuilderId: other.parentBuilderId ?? parentBuilderId,
affectedProperties: other.affectedProperties ?? affectedProperties,
);
}

@override
ReturnT dispatchInternalCall<ReturnT>(
String methodName, {
Iterable? positionalParams,
Map<String, dynamic>? namedParams,
}) {
return switch (methodName) {
"fromJson" =>
ConstrainedBoxAttributes.fromJson(positionalParams!.first) as ReturnT,
String() => throw UnimplementedError("$methodName is not implemented"),
};
}
}
1 change: 1 addition & 0 deletions lib/src/attributes/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export 'animation_attributes/animated_size_attributes.dart';
export 'animation_attributes/animated_builder_attiributes.dart';
export 'intrinsic_height_attributes.dart';
export 'rotated_box_attributes.dart';
export 'constrained_box_attributes.dart';
27 changes: 27 additions & 0 deletions lib/src/ui/models/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,33 @@ base class DuitElement<T> extends TreeElement<T> with WidgetFabric {
),
child: child,
);
case ElementType.constrainedBox:
final child = DuitElement.fromJson(json["child"], driver);

final attributes =
ViewAttribute.createAttributes<ConstrainedBoxAttributes>(
type,
attributesObject,
tag,
id: id,
);

return ConstrainedBoxUIElement<ConstrainedBoxAttributes>(
type: type,
id: id,
controlled: controlled,
attributes: attributes,
viewController: _createAndAttachController(
id,
controlled,
attributes,
serverAction,
driver,
type,
tag,
),
child: child,
);
case ElementType.empty:
return EmptyUIElement<EmptyAttributes>();
case ElementType.component:
Expand Down
18 changes: 18 additions & 0 deletions lib/src/ui/models/element_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,24 @@ final class RotatedBoxUIElement<T> extends DuitElement<T>
//</editor-fold>
}

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

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

//</editor-fold>
}

final class EmptyUIElement<T> extends DuitElement<T> {
EmptyUIElement({
super.type = ElementType.empty,
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 @@ -41,4 +41,5 @@ final class ElementType {
static const animatedBuilder = "AnimatedBuilder";
static const intrinsicHeight = "IntrinsicHeight";
static const rotatedBox = "RotatedBox";
static const constrainedBox = "ConstrainedBox";
}
6 changes: 6 additions & 0 deletions lib/src/ui/models/type_def.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:flutter_duit/src/attributes/index.dart';
import 'package:flutter_duit/src/ui/models/element_models.dart';

//Type helpers for better type assertion

typedef ConstrainedBoxModel = ConstrainedBoxUIElement<ConstrainedBoxAttributes>;
72 changes: 72 additions & 0 deletions lib/src/ui/widgets/constrained_box.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import "package:duit_kernel/duit_kernel.dart";
import "package:flutter/material.dart";
import "package:flutter_duit/flutter_duit.dart";
import "package:flutter_duit/src/animations/index.dart";
import "package:flutter_duit/src/attributes/index.dart";

class DuitConstrainedBox extends StatelessWidget with AnimatedAttributes {
final Widget child;
final ViewAttribute<ConstrainedBoxAttributes> attributes;

const DuitConstrainedBox({
super.key,
required this.child,
required this.attributes,
});

@override
Widget build(BuildContext context) {
final attrs = mergeWithAttributes(
context,
attributes.payload,
);

return ConstrainedBox(
key: ValueKey(attributes.id),
constraints: attrs.constraints,
child: child,
);
}
}

class DuitControlledConstrainedBox extends StatefulWidget
with AnimatedAttributes {
final Widget child;
final UIElementController<ConstrainedBoxAttributes> controller;

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

@override
State<DuitControlledConstrainedBox> createState() =>
_DuitControlledConstrainedBoxState();
}

class _DuitControlledConstrainedBoxState
extends State<DuitControlledConstrainedBox>
with
ViewControllerChangeListener<DuitControlledConstrainedBox,
ConstrainedBoxAttributes> {
@override
void initState() {
attachStateToController(widget.controller);
super.initState();
}

@override
Widget build(BuildContext context) {
final attrs = widget.mergeWithAttributes(
context,
attributes,
);

return ConstrainedBox(
key: ValueKey(widget.controller.id),
constraints: attrs.constraints,
child: widget.child,
);
}
}
1 change: 1 addition & 0 deletions lib/src/ui/widgets/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ export 'overflow_box.dart';
export 'implicit_animations/index.dart';
export 'intrinsic_height.dart';
export 'rotated_box.dart';
export 'constrained_box.dart';
18 changes: 16 additions & 2 deletions lib/src/ui/widgets/widget_fabric.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_duit/src/ui/models/element.dart';
import 'package:flutter_duit/src/ui/models/element_models.dart';
import 'package:flutter_duit/src/ui/models/element_type.dart';
import 'package:flutter_duit/src/animations/animation_builder.dart';
import 'package:flutter_duit/src/ui/models/type_def.dart';

import 'index.dart';

Expand Down Expand Up @@ -496,12 +497,24 @@ mixin WidgetFabric {
attributes: it.attributes!,
child: child,
);
case ElementType.constrainedBox:
final it = model as ConstrainedBoxModel;
final child = getWidgetFromElement(it.child);

return it.controlled
? DuitControlledConstrainedBox(
controller: it.viewController!,
child: child,
)
: DuitConstrainedBox(
attributes: it.attributes!,
child: child,
);
case ElementType.empty:
return const DuitEmptyView();
case ElementType.custom:
final customWidgetModel = model as CustomUiElement;
if (customWidgetModel.tag != null) {

final children = <Widget>{};

for (var subview in customWidgetModel.subviews) {
Expand All @@ -510,7 +523,8 @@ mixin WidgetFabric {
}

final renderer = DuitRegistry.getBuildFactory(customWidgetModel.tag!);
return renderer?.call(customWidgetModel, children) ?? const DuitEmptyView();
return renderer?.call(customWidgetModel, children) ??
const DuitEmptyView();
}

return const DuitEmptyView();
Expand Down
4 changes: 1 addition & 3 deletions lib/src/utils/params_mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,7 @@ final class AttributeValueMapper {
///
/// If any of the properties are missing or invalid, they will be ignored.
/// If [constraintsMap] is `null` or empty, returns `null`.
static BoxConstraints? toBoxConstraints(dynamic json) {
if (json == null) return null;

static BoxConstraints toBoxConstraints(dynamic json) {
if (json is BoxConstraints) return json;

return BoxConstraints(
Expand Down

0 comments on commit 62e81c8

Please sign in to comment.