Is a good practice pass Widgets as class argument in flutter?

时间:2019-01-18 18:29:37

标签: dart flutter

I mean create a class like this:

class HighLightAnimationState extends State<HighLightAnimation> {
  HighLightAnimationState(Card this.child, this._elevation, this._boxShadow);
  final Card child;
  final double _elevation;
  final double _boxShadow;

  @override
  Widget build(BuildContext context) {
    return this.child;
  }

}

class HighLightAnimation extends StatefulWidget {
  HighLightAnimation(Card this.child, [this._elevation = 1.0, this._boxShadow = 0.0]);
  final Card child;
  final double _elevation;
  final double _boxShadow;

  @override
  createState() => new HighLightAnimationState(this.child, this._elevation, this._boxShadow);
}

It remarks on Card Widget and indicates "Don't type annotate initializing formals" When I google it, I went redirected to https://www.dartlang.org/guides/language/effective-dart/usage, so, that's why I wanna know if the thing that I am doing is right.

1 个答案:

答案 0 :(得分:1)

It's OK to pass widgets to constructors, of course. Remove the type Card from Card this.child. That type is not wrong, just unnecessary, that's why you are getting the warning.

It should be:

HighLightAnimationState(this.child, this._elevation, this._boxShadow);

HighLightAnimation(this.child, [this._elevation = 1.0, this._boxShadow = 0.0]);