Flutter-如何创建显示RenderBox组件的小部件?

时间:2018-08-01 20:53:21

标签: dart flutter

RenderBox提供了覆盖paint()方法的机会。但是如何从该RenderBox构建()小部件?

1 个答案:

答案 0 :(得分:1)

可以代替StatefulWidgetStatelessWidget的子类SingleChildRenderObjectWidget(或类似的子类)

class MyWidget extends SingleChildRenderObjectWidget {
  @override
  MyRenderBox createRenderObject(BuildContext context) {
    return new MyRenderBox(title: "bar");
  }

  @override
  void updateRenderObject(BuildContext context, MyRenderBox renderObject) {
    renderObject.title = "foo";
  }
}

class MyRenderBox extends RenderBox {
  String title;

  MyRenderBox({this.title});
}

其他一些有趣的子类是LeafRenderObjectWidgetMultiChildRenderObjectWidget