有没有更好的方法将数据传递给没有状态的,继承的小部件的子代?

问题描述 投票:0回答:1

我有一个如下所示的课程,

class DottedLine extends StatelessWidget{
  final double circleSize, space;
  final Color color;

  DottedLine({@required this.color, this.circleSize = 1.0, this.space = 5.0});

  @override
  Widget build(BuildContext context){
    return CustomPaint(painter: _DottedLinePainter(color: color, circleSize: circleSize, space: space),);
  }

}

还有另一个类,

class _DottedLinePainter extends CustomPainter{

  _DottedLinePainter({this.color, this.circleSize, this.space});

  final double circleSize, space;
  final Color color;

  @override
  void paint(Canvas canvas, Size size){
  ...
  }
  ...
}

这里,来自[[DottedLine,我将相同的三个参数传递给_ DottedLinePainter现在,如果我想为类_ DottedLinePainter添加新参数,则必须创建它也适用于[[DottedLine ...

那么如何只能在一个地方定义参数名称?但是我不想扩展Inherited Widgets导致的情况,因此我不得不更改DottedLine StatefulWidget,这是不必要的。
flutter dart
1个回答
1
投票
class DottedLine extends StatelessWidget{ final double circleSize, space; final Color color; DottedLine({@required this.color, this.circleSize = 1.0, this.space = 5.0}); @override Widget build(BuildContext context){ return CustomPaint(painter: _DottedLinePainter(this),); } } class _DottedLinePainter extends CustomPainter{ _DottedLinePainter(this.data); final DottedLine data; @override void paint(Canvas canvas, Size size){ ... } ... }
© www.soinside.com 2019 - 2024. All rights reserved.