美文网首页
ParentDataWidget用法

ParentDataWidget用法

作者: liboxiang | 来源:发表于2020-02-15 21:09 被阅读0次
    ParentDataWidget用法.png
    FrogJar(
            child: FrogSize(
              size: Size(20, 20),
              child: Text('hello'),
            ),
            decoration: BoxDecoration(color: Colors.red),
          )
    
    
    class FrogSize extends ParentDataWidget<FrogJar> {
      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用法

          本文链接:https://www.haomeiwen.com/subject/mltufhtx.html