Coder Social home page Coder Social logo

Comments (1)

aloisdeniel avatar aloisdeniel commented on September 17, 2024

Here is an example of such a listener if FlutterVectorGraphicsListener would have a raw public constructor instead of _.

class OverridesFlutterVectorGraphicsListener
    extends FlutterVectorGraphicsListener {
  factory OverridesFlutterVectorGraphicsListener({
    required Map<int, int> colorOverrides,
    int id = 0,
    Locale? locale,
    TextDirection? textDirection,
    bool clipViewbox = true,
    @visibleForTesting
    DefaultPictureFactory pictureFactory = const DefaultPictureFactory(),
  }) {
    final PictureRecorder recorder = pictureFactory.createPictureRecorder();
    return OverridesFlutterVectorGraphicsListener.raw(
      id,
      pictureFactory,
      recorder,
      pictureFactory.createCanvas(recorder),
      locale,
      textDirection,
      clipViewbox,
      colorOverrides,
    );
  }

  OverridesFlutterVectorGraphicsListener.raw(
    int id,
    DefaultPictureFactory pictureFactory,
    PictureRecorder recorder,
    Canvas canvas,
    Locale? locale,
    TextDirection? textDirection,
    bool clipViewbox,
    this.colorOverrides,
  ) : super.raw(
          id,
          pictureFactory,
          recorder,
          canvas,
          locale,
          textDirection,
          clipViewbox,
        );

  final Map<int, int> colorOverrides;

  @override
  void onLinearGradient(
    double fromX,
    double fromY,
    double toX,
    double toY,
    Int32List colors,
    Float32List? offsets,
    int tileMode,
    int id,
  ) {
    final colorValues = <int>[];
    for (var color in colors) {
      final replacedColor = colorOverrides[_colorValue(color)];
      colorValues.add(replacedColor ?? color);
    }
    super.onLinearGradient(
      fromX,
      fromY,
      toX,
      toY,
      Int32List.fromList(colorValues),
      offsets,
      tileMode,
      id,
    );
  }

  @override
  void onPaintObject({
    required int color,
    required int? strokeCap,
    required int? strokeJoin,
    required int blendMode,
    required double? strokeMiterLimit,
    required double? strokeWidth,
    required int paintStyle,
    required int id,
    required int? shaderId,
  }) {
    /// If we add an override we add it to the paints instead

    final int? override = colorOverrides[_colorValue(color)];
    if (override != null) {
      return super.onPaintObject(
        id: id,
        color: override,
        strokeCap: strokeCap,
        strokeJoin: strokeJoin,
        blendMode: blendMode,
        strokeMiterLimit: strokeMiterLimit,
        strokeWidth: strokeWidth,
        paintStyle: paintStyle,
        shaderId: shaderId,
      );
    }

    return super.onPaintObject(
      id: id,
      color: color,
      strokeCap: strokeCap,
      strokeJoin: strokeJoin,
      blendMode: blendMode,
      strokeMiterLimit: strokeMiterLimit,
      strokeWidth: strokeWidth,
      paintStyle: paintStyle,
      shaderId: shaderId,
    );
  }

  final _colorData = ByteData(4);

  int _colorValue(int value) {
    _colorData.setInt32(0, value);
    return _colorData.getUint32(0);
  }
}

from vector_graphics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.