Skip to content

Commit

Permalink
Support for custom font
Browse files Browse the repository at this point in the history
  • Loading branch information
kawaijoe committed May 15, 2024
1 parent 42b4bbb commit 8b955a0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 21 deletions.
2 changes: 1 addition & 1 deletion forui/lib/src/theme/font/font_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FFontData {

/// Creates a [FFontData].
const FFontData({
this.fontFamily = 'Inter',
this.fontFamily = 'packages/forui/Inter',
this.fontSizeScalar = 1,
this.letterSpacingScalar = 1,
this.wordSpacingScalar = 1,
Expand Down
3 changes: 2 additions & 1 deletion forui/lib/src/theme/font/scaled_text_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import 'package:forui/forui.dart';
/// * Word spacing
extension type ScaledTextStyle._(TextStyle style) implements TextStyle {
/// Creates a [TextStyle] that inherits the properties from the given [FFontData].
ScaledTextStyle(TextStyle base, FFontData data): style = TextStyle(
ScaledTextStyle(TextStyle base, FFontData data): style = base.copyWith(
fontFamily: data.fontFamily,
fontSize: _calculateFactor(base.fontSize, data.fontSizeScalar),
letterSpacing: _calculateFactor(base.letterSpacing, data.letterSpacingScalar),
wordSpacing: _calculateFactor(base.wordSpacing, data.wordSpacingScalar),
Expand Down
15 changes: 14 additions & 1 deletion forui/lib/src/theme/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ class FTheme extends StatefulWidget {
/// The child widget.
final Widget child;

/// The text direction.
///
/// If none is provided, the text direction is inherited from the context.
final TextDirection? textDirection;

/// Creates a [FTheme].
const FTheme({
required this.data,
required this.child,
this.textDirection,
super.key,
});

Expand All @@ -37,7 +43,14 @@ class _State extends State<FTheme> {
}

@override
Widget build(BuildContext context) => _InheritedTheme(data: data, child: widget.child);
Widget build(BuildContext context) => _InheritedTheme(
data: data,
// Required for `Text` widgets.
child: Directionality(
textDirection: widget.textDirection ?? Directionality.of(context),
child: widget.child,
),
);
}

class _InheritedTheme extends InheritedWidget {
Expand Down
29 changes: 22 additions & 7 deletions forui/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,28 @@ flutter:
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware

# To add custom fonts to your package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
#
fonts:
- family: Inter
fonts:
- asset: assets/fonts/inter/Inter-Thin.ttf
weight: 100
- asset: assets/fonts/inter/Inter-ExtraLight.ttf
weight: 200
- asset: assets/fonts/inter/Inter-Light.ttf
weight: 300
- asset: assets/fonts/inter/Inter-Regular.ttf
weight: 400
- asset: assets/fonts/inter/Inter-Medium.ttf
weight: 500
- asset: assets/fonts/inter/Inter-SemiBold.ttf
weight: 600
- asset: assets/fonts/inter/Inter-Bold.ttf
weight: 700
- asset: assets/fonts/inter/Inter-ExtraBold.ttf
weight: 800
- asset: assets/fonts/inter/Inter-Black.ttf
weight: 900

# For details regarding fonts in packages, see
# https://flutter.dev/custom-fonts/#from-packages

Expand Down
21 changes: 16 additions & 5 deletions samples/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ void main() {
class Application extends StatelessWidget {
/// Creates an application widget.
const Application({super.key});

@override
Widget build(BuildContext context) => FTheme(
data: FZincTheme.light,
child: Container(),
);
Widget build(BuildContext context) => MaterialApp(
home: FTheme(
data: FZincTheme.light,
child: Scaffold(
body: Container(
alignment: Alignment.center,
// TODO: Replace with FText.
child: Text(
'Hello',
style: ScaledTextStyle(const TextStyle(fontWeight: FontWeight.w500), FTheme.of(context).font),
),
),
),
),
);
}
8 changes: 2 additions & 6 deletions samples/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ environment:
dependencies:
flutter:
sdk: flutter
# TODO; replace with published version
# TODO: replace with published version
forui:
path: ../forui
sugar: ^3.1.0
Expand All @@ -41,10 +41,6 @@ dev_dependencies:
url: https://github.com/forus-labs/cauldron.git
path: stevia_runner

dependency_overrides:
forui:
path: ../forui

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

Expand All @@ -54,7 +50,7 @@ flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
uses-material-design: false

# To add assets to your application, add an assets section, like this:
# assets:
Expand Down

0 comments on commit 8b955a0

Please sign in to comment.