Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[flutter_svg] feat: Expose the colorMapper property in SvgPicture #9043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 18, 2025

Conversation

ricardodalarme
Copy link
Contributor

@ricardodalarme ricardodalarme commented Apr 10, 2025

Description

This pull request exposes the existing colorMapper functionality in the flutter_svg package, allowing developers to customize SVG colors during parsing.

Related Issue

Motivation

The colorMapper functionality was already present within the flutter_svg package but was not directly accessible through the SvgPicture constructors. By exposing this property, developers gain a powerful and flexible way to dynamically modify the colors of SVG assets based on custom logic. This can be useful for various scenarios, such as theming or branding.

Example Usage

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

const String svgString = '''
<svg viewBox="0 0 100 100">
  <rect width="50" height="50" fill="#FF0000" />
  <circle cx="75" cy="75" r="25" fill="#00FF00" />
</svg>
''';

class MyColorMapper extends ColorMapper {
  const MyColorMapper();

  @override
  Color substitute(
      String? id, String elementName, String attributeName, Color color) {
    if (color == const Color(0xFFFF0000)) {
      return Colors.blue;
    }
    if (color == const Color(0xFF00FF00)) {
      return Colors.yellow;
    }
    return color;
  }
}

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      body: Center(
        child: SvgPicture.string(
          svgString,
          width: 200,
          height: 200,
          colorMapper: const MyColorMapper(),
        ),
      ),
    ),
  ));
}

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@ricardodalarme ricardodalarme changed the title feat: Expose the colorMapper property in SvgPicture [flutter_svg] feat: Expose the colorMapper property in SvgPicture Apr 10, 2025
Copy link
Contributor

@domesticmouse domesticmouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests and bump version in pubspec.yaml

@ricardodalarme ricardodalarme force-pushed the feat/expose-color-mapper branch 2 times, most recently from e3649e9 to 14e87e1 Compare April 10, 2025 03:29
@ricardodalarme
Copy link
Contributor Author

@domesticmouse done!

@ricardodalarme
Copy link
Contributor Author

Hey @domesticmouse, I just updated the PR by formatting the code and updating the README to include a code excerpt in the code example. Could you please take another look?

Copy link
Contributor

@domesticmouse domesticmouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ricardodalarme ricardodalarme force-pushed the feat/expose-color-mapper branch from 7f2e219 to 63609c0 Compare April 11, 2025 15:04
Copy link
Member

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

this is going to collide with #8986

@ricardodalarme
Copy link
Contributor Author

ricardodalarme commented Apr 15, 2025

hey @jonahwilliams @domesticmouse I’ve reviewed #8986 and I believe it’s safe to merge this PR. Although flutter_svg depends on vector_graphics_compiler, I didn’t find any direct or indirect references to parseWithoutOptimizers (the function modified in the other PR) that could impact those changes.

Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonahwilliams This has two approvals; was it waiting for anything else to be autosubmited?

@ricardodalarme ricardodalarme force-pushed the feat/expose-color-mapper branch from 63609c0 to d3ef9ce Compare April 17, 2025 19:30
@ricardodalarme ricardodalarme force-pushed the feat/expose-color-mapper branch from d3ef9ce to 5186527 Compare April 18, 2025 03:21
@jonahwilliams
Copy link
Member

Nope, this one is good.

@jonahwilliams jonahwilliams added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 18, 2025
@auto-submit auto-submit bot merged commit e0892ae into flutter:main Apr 18, 2025
82 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 21, 2025
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request Apr 21, 2025
flutter/packages@2fcc403...ac21f53

2025-04-20 [email protected] Roll Flutter from
3ed38e2 to cfb887c (17 revisions) (flutter/packages#9118)
2025-04-19 [email protected] [various] Scrubs pre-SDK-21 Android
code (flutter/packages#9112)
2025-04-18 [email protected] Roll Flutter from
ecabb1a to 3ed38e2 (23 revisions) (flutter/packages#9114)
2025-04-18 [email protected] [flutter_svg] feat: Expose the
`colorMapper` property in `SvgPicture` (flutter/packages#9043)
2025-04-18 [email protected] [tool] Add initial file-based command
skipping (flutter/packages#8928)
2025-04-18 [email protected] [pigeon] Convert test plugins to SPM
(flutter/packages#9105)
2025-04-18 [email protected]
[webview_flutter] Adds support to control overscrolling
(flutter/packages#8451)
2025-04-17 [email protected] [in_app_purchase] add
Storefront.countryCode() and AppStore.sync() (flutter/packages#8900)
2025-04-17 [email protected]
[webview_flutter_wkwebview] Expose the allowsLinkPreview property in
WKWebView for iOS (flutter/packages#5029)
2025-04-17 [email protected]
[webview_flutter_android][webview_flutter_wkwebview] Adds platform
implementations to set over-scroll mode (flutter/packages#9101)
2025-04-17 [email protected]
[shared_preferences] Update AGP to 8.9.1 (flutter/packages#9106)
2025-04-17 [email protected] [pigeon] Adds
Kotlin lint tests to example code and fix lints (flutter/packages#9034)
2025-04-17 [email protected]
[video_player_avfoundation] enable more than 30 fps
(flutter/packages#7466)
2025-04-17 [email protected] Roll Flutter from
aef4718 to ecabb1a (25 revisions) (flutter/packages#9104)
2025-04-16 [email protected] [pigeon] Unify iOS and macOS test
plugins (flutter/packages#9100)
2025-04-16 [email protected] Roll Flutter from
db68c95 to aef4718 (7 revisions) (flutter/packages#9098)
2025-04-16 [email protected]
[webview_flutter_platform_interface] Adds method to set overscroll mode
(flutter/packages#9099)
2025-04-16 [email protected] Update `CODEOWNERS`
(flutter/packages#8984)
2025-04-16 [email protected] [google_sign_is] Update iOS SDK to
8.0 (flutter/packages#9081)
2025-04-16 [email protected] [camera_avfoundation]
Implementation swift migration (flutter/packages#8988)
2025-04-16 [email protected] [go_router]
Adds `caseSensitive` to `GoRoute` (flutter/packages#8992)
2025-04-16 [email protected] Manual roll Flutter from
30e53b0 to db68c95 (98 revisions) (flutter/packages#9092)
2025-04-15 [email protected] [tool] Run config-only build for
iOS/macOS native-test (flutter/packages#9080)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
CodixNinja pushed a commit to CodixNinja/flutter that referenced this pull request May 15, 2025
flutter/packages@2fcc403...ac21f53

2025-04-20 [email protected] Roll Flutter from
409a8ac to cd51fa3 (17 revisions) (flutter/packages#9118)
2025-04-19 [email protected] [various] Scrubs pre-SDK-21 Android
code (flutter/packages#9112)
2025-04-18 [email protected] Roll Flutter from
d0741df to 409a8ac (23 revisions) (flutter/packages#9114)
2025-04-18 [email protected] [flutter_svg] feat: Expose the
`colorMapper` property in `SvgPicture` (flutter/packages#9043)
2025-04-18 [email protected] [tool] Add initial file-based command
skipping (flutter/packages#8928)
2025-04-18 [email protected] [pigeon] Convert test plugins to SPM
(flutter/packages#9105)
2025-04-18 [email protected]
[webview_flutter] Adds support to control overscrolling
(flutter/packages#8451)
2025-04-17 [email protected] [in_app_purchase] add
Storefront.countryCode() and AppStore.sync() (flutter/packages#8900)
2025-04-17 [email protected]
[webview_flutter_wkwebview] Expose the allowsLinkPreview property in
WKWebView for iOS (flutter/packages#5029)
2025-04-17 [email protected]
[webview_flutter_android][webview_flutter_wkwebview] Adds platform
implementations to set over-scroll mode (flutter/packages#9101)
2025-04-17 [email protected]
[shared_preferences] Update AGP to 8.9.1 (flutter/packages#9106)
2025-04-17 [email protected] [pigeon] Adds
Kotlin lint tests to example code and fix lints (flutter/packages#9034)
2025-04-17 [email protected]
[video_player_avfoundation] enable more than 30 fps
(flutter/packages#7466)
2025-04-17 [email protected] Roll Flutter from
9616f9c to d0741df (25 revisions) (flutter/packages#9104)
2025-04-16 [email protected] [pigeon] Unify iOS and macOS test
plugins (flutter/packages#9100)
2025-04-16 [email protected] Roll Flutter from
a7ce7ff to 9616f9c (7 revisions) (flutter/packages#9098)
2025-04-16 [email protected]
[webview_flutter_platform_interface] Adds method to set overscroll mode
(flutter/packages#9099)
2025-04-16 [email protected] Update `CODEOWNERS`
(flutter/packages#8984)
2025-04-16 [email protected] [google_sign_is] Update iOS SDK to
8.0 (flutter/packages#9081)
2025-04-16 [email protected] [camera_avfoundation]
Implementation swift migration (flutter/packages#8988)
2025-04-16 [email protected] [go_router]
Adds `caseSensitive` to `GoRoute` (flutter/packages#8992)
2025-04-16 [email protected] Manual roll Flutter from
f2d54fd to a7ce7ff (98 revisions) (flutter/packages#9092)
2025-04-15 [email protected] [tool] Run config-only build for
iOS/macOS native-test (flutter/packages#9080)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: flutter_svg
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants