Skip to content

Commit

Permalink
Make copying nested styles more concise (#386)
Browse files Browse the repository at this point in the history
* Refactor FThemeData

* Add forui_internal_gen

* Refactor styles to use forui_internal_gen

* Update documentation

* Remove redundant transform

* refactor styles

* Commit from GitHub Actions (Forui Presubmit)

* Fix failing build

* Commit from GitHub Actions (Forui Internal Gen Presubmit)

* Update CONTRIBUTING.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix failing build

---------

Co-authored-by: Pante <Pante@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 10, 2025
1 parent 58b4546 commit 470b4ad
Show file tree
Hide file tree
Showing 92 changed files with 1,277 additions and 3,849 deletions.
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ if applicable.
- [ ] I have included the relevant unit/golden tests.
- [ ] I have included the relevant samples.
- [ ] I have updated the documentation accordingly.
- [ ] I have added the required flutters_hook for widget controllers.
- [ ] I have updated the [CHANGELOG.md](../forui/CHANGELOG.md) if necessary.
5 changes: 4 additions & 1 deletion .github/workflows/docs_deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ jobs:
with:
flutter-version: 3.27.x
cache: true

- working-directory: forui
run: |
flutter pub get
dart run build_runner build --delete-conflicting-outputs
- run: flutter pub get
- run: dart run build_runner build --delete-conflicting-outputs
- run: flutter build web
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/docs_preview_deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ jobs:
with:
flutter-version: 3.27.x
cache: true
- working-directory: forui
run: |
flutter pub get
dart run build_runner build --delete-conflicting-outputs
- working-directory: ./samples
run: |
flutter pub get
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/forui_build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ jobs:
flutter-version: ${{ matrix.flutter-version }}
cache: true

- run: dart run build_runner build --delete-conflicting-outputs
working-directory: forui

- run: flutter pub get
- run: dart run build_runner build --delete-conflicting-outputs
- run: flutter build appbundle --debug --no-pub --no-tree-shake-icons
Expand All @@ -88,6 +91,9 @@ jobs:
flutter-version: ${{ matrix.flutter-version }}
cache: true

- run: dart run build_runner build --delete-conflicting-outputs
working-directory: forui

- run: flutter pub get
- run: dart run build_runner build --delete-conflicting-outputs
- run: pod repo update
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/forui_internal_gen_build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Forui Internal Gen Build

on:
workflow_dispatch: {}
push:
paths:
- .github/workflows/forui_internal_gen_build.yaml
- forui/**
pull_request:
paths:
- forui/**

jobs:
test:
name: Build & test
runs-on: ${{ matrix.os }}
defaults:
run:
working-directory: forui_internal_gen
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
flutter-version: [ 3.x ]
steps:
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2.18.0
with:
flutter-version: ${{ matrix.flutter-version }}
cache: true
- run: flutter pub get
- run: dart run build_runner build --delete-conflicting-outputs
- run: flutter analyze
55 changes: 55 additions & 0 deletions .github/workflows/forui_internal_gen_presubmit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Forui Internal Gen Presubmit

on:
workflow_dispatch: {}
pull_request:
branches: [ main ]
paths:
- .github/workflows/forui_internal_gen_presubmit.yaml
pull_request_target:
branches: [ main ]
paths:
- forui_internal_gen/**

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
prepare:
if: github.actor != 'auto-maid[bot]' && github.event.pull_request.draft == false
name: Prepare PR for review
runs-on: ubuntu-latest
defaults:
run:
working-directory: forui_internal_gen

steps:
- uses: tibdex/github-app-token@v2
id: generate-token
with:
app_id: ${{ secrets.AUTO_MAID_APP_ID }}
private_key: ${{ secrets.AUTO_MAID_PRIVATE_KEY }}

- uses: actions/checkout@v4
with:
token: ${{ steps.generate-token.outputs.token }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}

- uses: subosito/flutter-action@v2.18.0
with:
cache: true

- run: flutter pub get
- run: dart run ../tool/sort.dart forui
- run: dart run build_runner build --delete-conflicting-outputs
- run: dart format --line-length 120 .
- run: dart fix --apply
# We format the code again since the required trailing comma lint fixes causes code to be formatted differently
- run: dart format --line-length 120 .

- uses: EndBug/add-and-commit@v9
with:
pull: '--rebase --autostash'
add: '*'
44 changes: 22 additions & 22 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ This helps to:

If you're stuck or unsure about anything, feel free to ask for help in our [discord](https://discord.gg/UEky7WkXd6).

## Configuring the Development Environment

After cloning the repository, and before starting work on a PR, run the following commands in the `forui` project directory:
```shell
dart run build_runner build --delete-conflicting-outputs
````

This command generates the necessary files for the project to build successfully.

## Conventions

Expand Down Expand Up @@ -135,40 +143,32 @@ Lastly, types from 3rd party packages should not be publicly exported by Forui.
### Widget Styles

```dart
class FooStyle with Diagnosticable { // ---- (1)
part 'foo.style.dart'; // --- (1)
class FooStyle with Diagnosticable, _$FooStyleFunctions { // ---- (2) (3)
final Color color;
FooStyle({required this.color}); // ---- (2)
FooStyle({required this.color}); // ---- (3)
FooStyle.inherit({FFont font, FColorScheme scheme}): color = scheme.primary; // ---- (2)
FooStyle.inherit({FFont font, FColorScheme scheme}): color = scheme.primary; // ---- (4)
FooStyle copyWith({Color? color}) => FooStyle( // ---- (3)
FooStyle copyWith({Color? color}) => FooStyle( // ---- (5)
color: color ?? this.color,
);
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) { // ---- (4)
super.debugFillProperties(properties);
properties.add(ColorProperty<BorderRadius>('color', color));
}
@override
bool operator ==(Object other) => identical(this, other) || other is FStyle && color == other.color; // ---- (5)
@override
int get hashCode => color.hashCode; // ---- (5)
}
```

They should:
1. mix-in [Diagnosticable](https://api.flutter.dev/flutter/foundation/Diagnosticable-mixin.html).
2. provide a primary constructor, and a named constructor, `inherit(...)` , that configures itself based on
1. include a generated part file which includes `_$FooStyleFunctions`. To generate the file, run
`dart run build_runner build --delete-conflicting-outputs` in the forui/forui directory.
2. mix-in [Diagnosticable](https://api.flutter.dev/flutter/foundation/Diagnosticable-mixin.html).
3. mix-in `_$FooStyleFunctions`, which contains several utility functions.
4. provide a primary constructor, and a named constructor, `inherit(...)` , that configures itself based on
an ancestor `FTheme`.
3. provide a `copyWith(...)` method.
4. override [debugFillProperties](https://api.flutter.dev/flutter/foundation/Diagnosticable/debugFillProperties.html).
5. implement `operator ==` and `hashCode`.
5. provide a `copyWith(...)` method.
6. override [debugFillProperties](https://api.flutter.dev/flutter/foundation/Diagnosticable/debugFillProperties.html).
7. implement `operator ==` and `hashCode`.

Lastly, the order of the fields and methods should be as shown above.

Expand Down
43 changes: 19 additions & 24 deletions docs/pages/docs/themes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,26 @@ A more detailed explanation of each class can be found in the [Class Diagram](#c

## Customize Themes

It's recommended to use the predefined themes as a starting point and customize them with the
[`inherit(...)`](https://pub.dev/documentation/forui/latest/forui.theme/FThemeData/FThemeData.inherit.html) constructor
and [`copyWith(...)`](https://pub.dev/documentation/forui/latest/forui.theme/FThemeData/copyWith.html) method to suit your needs.
It's recommended to use the predefined themes as a starting point and customize them with
[`transform(...)`](https://pub.dev/documentation/forui/latest/forui.foundation/FTransformables/transform.html) and
[`copyWith(...)`](https://pub.dev/documentation/forui/latest/forui.theme/FThemeData/copyWith.html) methods to suit your
needs.

```dart filename="main.dart" {3-14, 17-23} copy
@override
Widget build(BuildContext context) {
final theme = FThemeData.inherit(
colorScheme: FThemes.zinc.light.colorScheme.copyWith(
primary: const Color(0xFF0D47A1), // dark blue
primaryForeground: const Color(0xFFFFFFFF), // white
),
typography: FThemes.zinc.light.typography.copyWith(
defaultFontFamily: 'Roboto',
).scale(sizeScalar: 0.8),
style: FThemes.zinc.light.style.copyWith(
borderRadius: BorderRadius.zero,
),
);
return FTheme(
data: theme.copyWith(
Widget build(BuildContext context) => FTheme(
data: FThemeData(
colorScheme: FThemes.zinc.light.colorScheme.copyWith(
primary: const Color(0xFF0D47A1), // dark blue
primaryForeground: const Color(0xFFFFFFFF), // white
),
typography: FThemes.zinc.light.typography.copyWith(
defaultFontFamily: 'Roboto',
).scale(sizeScalar: 0.8),
style: FThemes.zinc.light.style.copyWith(
borderRadius: BorderRadius.zero,
),
).transform((theme) => theme.copyWith(
cardStyle: theme.cardStyle.copyWith(
decoration: theme.cardStyle.decoration.copyWith(
borderRadius: const BorderRadius.all(Radius.circular(8)),
Expand All @@ -106,7 +104,6 @@ Widget build(BuildContext context) {
),
child: const FScaffold(...),
);
}
```

The example above uses `FThemes.zinc.light` theme as a starting point and customizes the following:
Expand Down Expand Up @@ -134,9 +131,8 @@ The example above uses `FThemes.zinc.light` theme as a starting point and custom
```
</Callout>

While the example may seem overwhelming, most use cases will only require customizations to the `FColorScheme`, `FTypography`, and `FStyle` class.
Sensible defaults for each Forui widget will be automatically inherited.

While the example may seem overwhelming, most use cases will only require customizations to the `FColorScheme`,
`FTypography`, and `FStyle` class. Sensible defaults for each Forui widget will be automatically inherited.

## Class Diagram

Expand All @@ -147,7 +143,6 @@ title: Forui Theme Data
classDiagram
class FThemeData {
+FThemeData(...)
+FThemeData.inherit(colorScheme)
}
class FColorScheme {
Expand Down
3 changes: 3 additions & 0 deletions forui/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ test/golden/failures/
test/**/*_test.mocks.dart
/.temp/
.i10n.txt

## Generated Dart files
**.style.dart
14 changes: 14 additions & 0 deletions forui/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## 0.10.0 (Next)

### Additions

* Add `FTransformable`.
* Add `FTransformables`.

### Changes

* Change all widget styles to use code generated functions.
* **Breaking** Change `FThemeData(...)` to automatically configure styles not passed in.
* **Breaking** Remove `FThemeData.inherit`. Use `FThemeData(...)` instead.


## 0.9.0

### Additions
Expand Down
2 changes: 0 additions & 2 deletions forui/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
include: package:flint/analysis_options.flutter.yaml
analyzer:
plugins:
- custom_lint
errors:
unused_result: ignore

Expand Down
6 changes: 6 additions & 0 deletions forui/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
targets:
$default:
builders:
forui_internal_gen:styleBuilder:
generate_for:
- lib/src/foundation/**.dart
- lib/src/theme/**.dart
- lib/src/widgets/**.dart

mockito:mockBuilder:
generate_for:
- test/**.dart
1 change: 1 addition & 0 deletions forui/lib/foundation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export 'src/foundation/form_field_properties.dart';
export 'src/foundation/notifiers.dart';
export 'src/foundation/rendering.dart' hide Alignments, RenderBoxes;
export 'src/foundation/tappable.dart' show FTappable, FTappableData;
export 'src/foundation/transform.dart';
export 'src/foundation/portal/portal.dart';
export 'src/foundation/portal/portal_shift.dart';
Loading

0 comments on commit 470b4ad

Please sign in to comment.