flutter - Error: Superclass has no constructor named 'ColorFilter.mode' in Web - Stack Overflow

I have created class called CustomColorFilter by extending ColorFilter classclass CustomColorFilter ex

I have created class called CustomColorFilter by extending ColorFilter class

class CustomColorFilter extends ColorFilter {
  const CustomColorFilter(this.color, {this.blendMode = BlendMode.srcIn}) : super.mode(color, blendMode);
  final Color color;
  final BlendMode blendMode;
}

This code working fine when i'm running with Android and iOS platform. But throwing error while running with Web platform.

Error: Superclass has no constructor named 'ColorFilter.mode'
const CustomColorFilter(this.color, {this.blendMode = BlendMode.srcIn}) : super.mode(color, blendMode);
                                                                          ^^^^^

I have created class called CustomColorFilter by extending ColorFilter class

class CustomColorFilter extends ColorFilter {
  const CustomColorFilter(this.color, {this.blendMode = BlendMode.srcIn}) : super.mode(color, blendMode);
  final Color color;
  final BlendMode blendMode;
}

This code working fine when i'm running with Android and iOS platform. But throwing error while running with Web platform.

Error: Superclass has no constructor named 'ColorFilter.mode'
const CustomColorFilter(this.color, {this.blendMode = BlendMode.srcIn}) : super.mode(color, blendMode);
                                                                          ^^^^^
Share Improve this question asked Mar 11 at 13:34 MSARKrishMSARKrish 4,1845 gold badges36 silver badges48 bronze badges 2
  • I tested it and got the same error. Interestingly, just calling the constructor directly like ColorFilter.mode(Colors.green, BlendMode.srcIn) for example works fine. I wonder if it's a bug. – Ivo Commented Mar 11 at 14:40
  • 1 @mmcdon20 dart:ui is a Flutter package, so issues should be taken to the Flutter repository. – Abion47 Commented Mar 11 at 19:02
Add a comment  | 

1 Answer 1

Reset to default 4

The reason this isn't working is because the ColorFilter type is defined differently in the native version of dart:ui versus the web version.

(flutter/ui)

class ColorFilter implements ImageFilter {
  const ColorFilter.mode(Color color, BlendMode blendMode)
    : _color = color,
      _blendMode = blendMode,
      _matrix = null,
      _type = _kTypeMode;

  ...
}

(flutter/web_ui)

class ColorFilter implements ImageFilter {
  const factory ColorFilter.mode(Color color, BlendMode blendMode) 
    = engine.EngineColorFilter.mode;

  ...
}

As you can see, in the flutter/ui version, ColorFilter.mode is a regular const constructor, but in the flutter/web_ui version, it is a factory constructor. You cannot call a factory constructor from an initializer with super because the factory constructor might return an instance of a completely different subclass. In fact, this is exactly what happens here, as ColorFilter.mode in flutter/web_ui returns an instance of EngineColorFilter which has nothing to do with your CustomColorFilter.

This is certainly an inconsistency in the Flutter SDK, so it should probably be brought up in the Flutter SDK Github Issues (not the Dart SDK repository, as dart:ui is a Flutter package). But at the same time, I'm not sure what you're trying to accomplish by creating a custom ColorFilter. Color filters are generally handled by the Flutter internal engine and their implementations are platform-specific, and as such, they aren't really meant to be extended by user code. (They should probably be marked as sealed, to be honest.)

If your goal is just to be able to access the color and blendMode properties, I'd recommend making a wrapper class instead of a subclass:

class ColorFilterWrapper {
  ColorFilterWrapper(this.color, {this.blendMode = BlendMode.srcIn}) 
    : colorFilter = ColorFilter.mode(color, blendMode);
  
  final Color color;
  final BlendMode blendMode;
  final ColorFilter colorFilter;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744791571a4593947.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信