flutter_svg icon indicating copy to clipboard operation
flutter_svg copied to clipboard

Proposal to add `ColorMapper.fromCallback` constructor

Open jtarkowski27 opened this issue 2 years ago • 1 comments

Personally I find it a bit excessive having to write a new class extending ColorMapper just to override one method which is ColorMapper.substitute. This method is usually quite simple an can be expressed in few lines of code without any complex logic.

For cases like that I propose to add the new ColorMapper.fromCallback named constructor that would take the substitute method as a callback. Somewhat precise implementation would be something like this.

abstract class ColorMapper {
  const ColorMapper();
  
  const factory ColorMapper.fromCallback(
    SubstituteCallback substitute,
  ) = _ColorMapperCallback;

  Color substitute(
    String? id,
    String elementName,
    String attributeName,
    Color color,
  );
}

class _ColorMapperCallback implements ColorMapper {
  const _ColorMapperCallback(this._substitute);

  final Color Function(
    String? id,
    String elementName,
    String attributeName,
    Color color,
  ) _substitute;

  @override
  Color substitute(
    String? id,
    String elementName,
    String attributeName,
    Color color,
  ) =>
      _substitute(
        id,
        elementName,
        attributeName,
        color,
      );
}

jtarkowski27 avatar Oct 19 '23 18:10 jtarkowski27

I don't see why not. It could make evolving the ColorMapper interface harder but I'm not sure how much evolution it really would/will need.

dnfield avatar Oct 19 '23 20:10 dnfield