WidgetTrait.withParents constructor

WidgetTrait.withParents(
  1. List<ComposeTargetId> parentIds, {
  2. required TargetId targetId,
  3. String? id,
  4. List<TraitAssert> asserts = const [],
  5. List<WidgetTrait> descendants = const [],
})

Creates an instance of WidgetTrait as a descendant of parents defined represented by parentIds.

A new parent trait is created for each ID in parentIds, with the first element being the direct parent of the trait to be created here and the last element being the root trait at the top. Therefore each element in the list is descendant of the next element until reaching the last one. trait without a parent.

This constructor can be used as shorthand, to create a hierarchy of elements with the parents being blank, except of their target IDs.

Implementation

factory WidgetTrait.withParents(
  List<ComposeTargetId> parentIds, {
  required TargetId targetId,
  String? id,
  List<TraitAssert> asserts = const [],
  List<WidgetTrait> descendants = const [],
}) =>
    parentIds.fold(
      WidgetTrait(
        targetId: targetId,
        id: id,
        asserts: asserts,
        descendants: descendants,
      ),
      (previousValue, targetId) => WidgetTrait(
        id: targetId.traitId,
        targetId: targetId._toTargetId(),
        descendants: [previousValue],
      ),
    );