Skip to content

transitive font dependencies #119562

Closed
Closed

Description

@subzero911

I create a design kit package with preset fonts. I follow this instruction: https://docs.flutter.dev/cookbook/design/package-fonts

I wrote down all my fonts in package's pubspec:
image

I also generated text constants with fluttergen:
image

Then I use it in flutter. But in app I see default 'Roboto' fonts everywhere.
It's stated in the cookbook: https://docs.flutter.dev/cookbook/design/package-fonts#declare-the-font-assets - "please declare all the package fonts".
Indeed, if I declare all the package fonts in the head app project, it finds it:
image
But that's not what I want! I don't want to force user to duplicate all the fonts in his pubspec. I already declared fonts in my package.
I want him to just download the package and use fonts (using generated font constants).

I believe it's an omitment of Dart: it can't transitively see fonts declared in the inner package until I explicitly add them in the pubspec.

If it's impossible, I suggest using some shorthand syntax like

fonts:
  import packages/mts_granat # imports all fonts from its pubspec

Activity

  1. added
    in triagePresently being triaged by the triage team
    on Jan 31, 2023
  2. huycozy commented on Jan 31, 2023

    @huycozy
    Member

    I don't want to force user to duplicate all the fonts in his pubspec. I already declared fonts in my package.

    @subzero911 As my understanding, the guideline from the docs doesn't work for you and you are declaring fonts twice (in package and project pubspec.yaml files), right?

    Can you provide a completed and minimal reproducible code sample so that we may verify this? Also, please provide the output of flutter doctor -v as well.

    Thank you!

  3. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jan 31, 2023
  4. subzero911 commented on Jan 31, 2023

    @subzero911
    Author

    As my understanding, the guideline from the docs doesn't work for you and you are declaring fonts twice (in package and project pubspec.yaml files), right?

    Not exactly. The guideline describes the situation when I just added fonts into the package. And it offers to reference them in a project's pubspec with packages/awesome_pkg/... prefix.
    In my case, I declare fonts in a package pubspec.
    Expected behaviour: project can use these fonts if I included a package, because fonts are already declared in it.
    Actual behaviour: project "can't see" these fonts until I declare these fonts in a project's pubspec too.

    Proposal: add a simple way to declare package fonts in a project with 1 line of code:

    fonts:
      import packages/mts_granat
  5. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jan 31, 2023
  6. subzero911 commented on Jan 31, 2023

    @subzero911
    Author

    I prepared a minimal example and commented it. Check please:
    https://github.com/subzero911/transitive_fonts_demo

    flutter doctor
    PS C:\Users\subze> flutter doctor -v
    [√] Flutter (Channel stable, 3.7.0, on Microsoft Windows [Version 10.0.19045.2486], locale ru-RU)
        • Flutter version 3.7.0 on channel stable at C:\flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision b06b8b2710 (8 days ago), 2023-01-23 16:55:55 -0800
        • Engine revision b24591ed32
        • Dart version 2.19.0
        • DevTools version 2.20.1
    
    [X] Windows Version (Unable to confirm if installed Windows version is 10 or greater)
    
    [√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at D:\Android_SDK
        • Platform android-33, build-tools 31.0.0
        • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)
        • All Android licenses accepted.
    
    [√] Chrome - develop for the web
        • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
    
    [√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.5)
        • Visual Studio at D:\Program Files (x86)\Microsoft Visual Studio\2019\Community
        • Visual Studio Community 2019 version 16.11.31729.503
        • Windows 10 SDK version 10.0.20348.0
    
    [√] Android Studio (version 2021.1)
        • Android Studio at C:\Program Files\Android\Android Studio
        • Flutter plugin can be installed from:
           https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
           https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)
    
    [√] IntelliJ IDEA Community Edition (version 2020.1)
        • IntelliJ at D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.3
        • Flutter plugin version 45.1.3
        • Dart plugin version 201.7223.43
    
    [√] Connected device (3 available)
        • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19045.2486]
        • Chrome (web)      • chrome  • web-javascript • Google Chrome 109.0.5414.120
        • Edge (web)        • edge    • web-javascript • Microsoft Edge 99.0.1150.55
    
    [√] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
  7. huycozy commented on Feb 1, 2023

    @huycozy
    Member

    @subzero911
    What I mean is similar to what you are describing (you are having to declare fonts twice on both package and project to be able to use fonts)

    However, it works as expected when adding package attribute to TextStyle, like this:

    Text(
                'Font example',
                style: TextStyle(
                  // font from the package
                  fontFamily: 'Ginger',
                  fontSize: 44,
                  package: 'awesome_fonts_collection'
                ),
              )
    

    Also, you don't need to declare font in project's pubspec.yaml anymore. Just try this and see the result.

  8. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 1, 2023
  9. subzero911 commented on Feb 1, 2023

    @subzero911
    Author

    Wow, it actually works! Thank you, Mr. @huycozy, sir, I think it solved my problem.
    I honestly didn't know about this package: parameter. It isn't mentioned in TextStyle docs neither in any articles.

  10. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 1, 2023
  11. huycozy commented on Feb 1, 2023

    @huycozy
    Member

    Glad it works for you. You may want to check the TextStyle document and scroll to Custom Fonts section.

    Closing the issue as solved.

  12. added
    r: solvedIssue is closed as solved
    and removed
    in triagePresently being triaged by the triage team
    on Feb 1, 2023
  13. subzero911 commented on Feb 1, 2023

    @subzero911
    Author

    It exists in a "Custom Fonts" section but does not exists in a Class properties list.

    Okay, I also see that package parameter exists in a ThemeData and TextTheme too.
    And it applies the custom fonts to all the children TextStyles. That should be enough for app styling.
    And it's not documented as well for some reason.

    @huycozy I recommend to reopen an issue with a need_documentation label.

  14. subzero911 commented on Feb 1, 2023

    @subzero911
    Author

    And I also found, that ThemeData copyWith does not contain the package parameter.
    So if I declared themes in a separate package, I cannot add a fonts package link to it in my app. Because I cannot do: lightTheme: externalThemeLight.copyWith(package: 'awesome_fonts')

  15. huycozy commented on Feb 2, 2023

    @huycozy
    Member

    @subzero911 I see, there is no package parameter in Class properties from the document. To help the issue can be transparent and clear, would you mind filing another issue for this?

  16. github-actions commented on Mar 3, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  17. locked as resolved and limited conversation to collaborators on Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    r: solvedIssue is closed as solved

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions