FlutterGen / flutter_gen

The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.
https://pub.dev/packages/flutter_gen
MIT License
1.44k stars 142 forks source link

[FR]: Fonts weight #483

Open mustafa-fahimi opened 3 months ago

mustafa-fahimi commented 3 months ago

Is there an existing issue for this?

Describe the problem

Hi. Generated fonts do not have weight and ignore given weight inside pubspec.yaml. Please add weight to fonts

Describe the solution

Add weight to fonts

Additional context

No response

Code of Conduct

AlexV525 commented 3 months ago

We need to create new classes for fonts if we want to include font-weight. It would be great if you could provide your idea about how you imagine the font-weight works.

mustafa-fahimi commented 3 months ago

After giving this some thought, I believe the best approach would be to introduce a new set of classes that represent font-weight variations. This approach would maintain the existing code structure and functionality while providing a clean and extensible way to handle font-weights.

Specifically, I propose the following implementation:

  1. Pubspec Configuration: In the pubspec.yaml file, allow users to specify the font-weight for each font file they define. For example:
flutter:
  fonts:
    - family: Roboto
      fonts:
        - asset: fonts/Roboto-Regular.ttf
          weight: 400
        - asset: fonts/Roboto-Bold.ttf
          weight: 700
        - asset: fonts/Roboto-Light.ttf
          weight: 300
  1. Font Class Generation: Instead of generating a single RobotoFont class, we would generate separate classes for each weight variant, such as RobotoRegularFont, RobotoBoldFont, and RobotoLightFont. These classes would extend a new base class, FontWeight, which would encapsulate the weight value and provide a consistent interface for accessing and using the font weight.

  2. Font Usage: In the Flutter application code, developers can then instantiate the appropriate FontWeight subclass based on the desired weight. For example:

Text(
  'Regular Text',
  style: TextStyle(fontFamily: RobotoRegularFont().family),
)

Text(
  'Bold Text',
  style: TextStyle(fontFamily: RobotoBoldFont().family),
)
  1. Dynamic Font-Weight Selection: To support dynamic font-weight selection, we could introduce a new method in the FontWeight base class, such as FontWeight.forValue(int weight), which would return an instance of the appropriate FontWeight subclass based on the provided weight value. This would allow developers to use numerical weight values (e.g., FontWeight.w500) or even create custom weight variations if needed.

This approach would provide a robust and type-safe way to handle font-weights while maintaining the existing code generation and usage patterns. It would also ensure that the font-weight information is correctly propagated from the pubspec.yaml configuration to the generated code and ultimately to the Flutter application.

Additionally, this implementation would be extensible, allowing for future enhancements such as support for font-stretch, font-style, or other font-related properties.