ParentDataWidget用法

ParentDataWidget用法.png
FrogJar(
        child: FrogSize(
          size: Size(20, 20),
          child: Text('hello'),
        ),
        decoration: BoxDecoration(color: Colors.red),
      )


class FrogSize extends ParentDataWidget {
  FrogSize({
    Key key,
    @required this.size,
    @required Widget child,
  })  : assert(child != null),
        assert(size != null),
        super(key: key, child: child);

  final Size size;

  @override
  void applyParentData(RenderObject renderObject) {
    ///Text 的 renderobject
    final FrogJarParentData parentData = renderObject.parentData;
    if (parentData.size != size) {
      parentData.size = size;
      final RenderFrogJar targetParent = renderObject.parent;
      targetParent.markNeedsLayout();
    }
  }
}

class FrogJar extends DecoratedBox {
  const FrogJar({
    Key key,
    @required Decoration decoration,
    Widget child,
  })  : assert(decoration != null),
        super(key: key, child: child, decoration: decoration);
  @override
  RenderDecoratedBox createRenderObject(BuildContext context) {
    return RenderFrogJar(
      decoration: decoration,
      position: position,
      configuration: createLocalImageConfiguration(context),
    );
  }
}

class RenderFrogJar extends RenderDecoratedBox {
  RenderFrogJar({
    @required Decoration decoration,
    DecorationPosition position = DecorationPosition.background,
    ImageConfiguration configuration = ImageConfiguration.empty,
    RenderBox child,
  }) : assert(decoration != null),
       assert(position != null),
       assert(configuration != null),
       super(
      decoration: decoration,
      position: position,
      configuration: configuration,
    );
  @override
  void setupParentData(RenderObject child) {
    ///child是Text的RenderObject
    FrogJarParentData frogJarParentData = FrogJarParentData();
    frogJarParentData.size = Size(10, 10);
    child.parentData = frogJarParentData;
  }
}

class FrogJarParentData extends ParentData {
  Size size;
}

你可能感兴趣的:(ParentDataWidget用法)