Skip to content

Commit

Permalink
Added BoxHeightStyle and BoxWidthStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeiffer committed Sep 27, 2023
1 parent 5ef27df commit 35b0ab3
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ jobs:
- uses: actions/checkout@v3
- name: Validate
uses: peiffer-innovations/actions-flutter-validate@v1
with:
path: annotation
- name: Validate
uses: peiffer-innovations/actions-flutter-validate@v1
with:
path: codegen
- name: Validate
uses: peiffer-innovations/actions-flutter-validate@v1
with:
path: json_theme
7 changes: 7 additions & 0 deletions json_theme/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [6.3.0] - September 27th, 2023

* Added codecs for:
* `BoxHeightStyle`
* `BoxWidthStyle`


## [6.2.6+1] - September 12th, 2023

* Switching back to [json_schema](https://pub.dev/packages/json_schema)
Expand Down
78 changes: 78 additions & 0 deletions json_theme/lib/src/codec/theme_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,53 @@ class ThemeDecoder {
return result;
}

/// Decodes the [value] to a [BoxHeightStyle]. Supported values are:
/// * `includeLineSpacingBottom`
/// * `includeLineSpacingMiddle`
/// * `includeLineSpacingTop`
/// * `max`
/// * `strut`
/// * `tight`
static BoxHeightStyle? decodeBoxHeightStyle(
dynamic value, {
bool validate = false,
}) {
BoxHeightStyle? result;

if (value is BoxHeightStyle) {
result = value;
} else if (value != null) {
assert(SchemaValidator.validate(
schemaId: '$_baseSchemaUrl/box_height_style',
value: value,
validate: validate,
));

switch (value) {
case 'includeLineSpacingBottom':
result = BoxHeightStyle.includeLineSpacingBottom;
break;
case 'includeLineSpacingMiddle':
result = BoxHeightStyle.includeLineSpacingMiddle;
break;
case 'includeLineSpacingTop':
result = BoxHeightStyle.includeLineSpacingTop;
break;
case 'max':
result = BoxHeightStyle.max;
break;
case 'strut':
result = BoxHeightStyle.strut;
break;
case 'tight':
result = BoxHeightStyle.tight;
break;
}
}

return result;
}

/// Decodes the given [value] into a [BoxDecoration]. If the value is `null`
/// then `null` will be returned. Otherwise, this expects a Map like value
/// that in JSON would look like:
Expand Down Expand Up @@ -1857,6 +1904,37 @@ class ThemeDecoder {
return result;
}

/// Decodes the [value] to a [BoxWidthStyle]. Supported values are:
/// * `max`
/// * `tight`
static BoxWidthStyle? decodeBoxWidthStyle(
dynamic value, {
bool validate = false,
}) {
BoxWidthStyle? result;

if (value is BoxWidthStyle) {
result = value;
} else if (value != null) {
assert(SchemaValidator.validate(
schemaId: '$_baseSchemaUrl/box_width_style',
value: value,
validate: validate,
));

switch (value) {
case 'max':
result = BoxWidthStyle.max;
break;
case 'tight':
result = BoxWidthStyle.tight;
break;
}
}

return result;
}

/// Decodes the [value] to a [Brightness]. Supported values are:
/// * `light`
/// * `dark`
Expand Down
56 changes: 56 additions & 0 deletions json_theme/lib/src/codec/theme_encoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,42 @@ class ThemeEncoder {
return _stripDynamicNull(result);
}

/// Encodes the [value] to a `String`. Supported values are:
/// * `includeLineSpacingBottom`
/// * `includeLineSpacingMiddle`
/// * `includeLineSpacingTop`
/// * `max`
/// * `strut`
/// * `tight`
static String? encodeBoxHeightStyle(BoxHeightStyle? value) {
String? result;

if (value != null) {
switch (value) {
case BoxHeightStyle.includeLineSpacingBottom:
result = 'includeLineSpacingBottom';
break;
case BoxHeightStyle.includeLineSpacingMiddle:
result = 'includeLineSpacingMiddle';
break;
case BoxHeightStyle.includeLineSpacingTop:
result = 'includeLineSpacingTop';
break;
case BoxHeightStyle.max:
result = 'max';
break;
case BoxHeightStyle.strut:
result = 'strut';
break;
case BoxHeightStyle.tight:
result = 'tight';
break;
}
}

return result;
}

/// Encodes the given [value] into a JSON compatible map. This produces a Map
/// in the following format:
///
Expand Down Expand Up @@ -977,6 +1013,26 @@ class ThemeEncoder {
return _stripDynamicNull(result);
}

/// Encodes the [value] to a `String`. Supported values are:
/// * `max`
/// * `tight`
static String? encodeBoxWidthStyle(BoxWidthStyle? value) {
String? result;

if (value != null) {
switch (value) {
case BoxWidthStyle.max:
result = 'max';
break;
case BoxWidthStyle.tight:
result = 'tight';
break;
}
}

return result;
}

/// Encodes the given [value] to the String representation. Supported values
/// are:
/// * `dark`
Expand Down
2 changes: 2 additions & 0 deletions json_theme/lib/src/schema/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export 'schemas/enums/border_style_schema.dart';
export 'schemas/enums/bottom_navigation_bar_landscape_layout.dart';
export 'schemas/enums/bottom_navigation_bar_type_schema.dart';
export 'schemas/enums/box_fit_schema.dart';
export 'schemas/enums/box_height_style_schema.dart';
export 'schemas/enums/box_shape_schema.dart';
export 'schemas/enums/box_width_style_schema.dart';
export 'schemas/enums/brightness_schema.dart';
export 'schemas/enums/button_bar_layout_behavior_schema.dart';
export 'schemas/enums/button_text_theme_schema.dart';
Expand Down
2 changes: 2 additions & 0 deletions json_theme/lib/src/schema/schemas/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class Enums {
BottomNavigationBarLandscapeLayoutSchema.id,
BottomNavigationBarTypeSchema.id,
BoxFitSchema.id,
BoxHeightStyleSchema.id,
BoxShapeSchema.id,
BoxWidthStyleSchema.id,
BrightnessSchema.id,
ButtonBarLayoutBehaviorSchema.id,
ButtonTextThemeSchema.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:json_theme/json_theme_schemas.dart';

class BoxHeightStyleSchema {
static const id =
'https://peiffer-innovations.github.io/flutter_json_schemas/schemas/json_theme/box_height_style.json';

static final schema = {
r'$schema': 'http://json-schema.org/draft-07/schema#',
r'$id': id,
'type': 'string',
'title': 'BoxHeightStyle',
'oneOf': SchemaHelper.enumSchema([
'includeLineSpacingBottom',
'includeLineSpacingMiddle',
'includeLineSpacingTop',
'max',
'strut',
'tight',
]),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:json_theme/json_theme_schemas.dart';

class BoxWidthStyleSchema {
static const id =
'https://peiffer-innovations.github.io/flutter_json_schemas/schemas/json_theme/box_width_style.json';

static final schema = {
r'$schema': 'http://json-schema.org/draft-07/schema#',
r'$id': id,
'type': 'string',
'title': 'BoxWidthStyle',
'oneOf': SchemaHelper.enumSchema([
'max',
'tight',
]),
};
}
4 changes: 2 additions & 2 deletions json_theme/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: 'json_theme'
description: 'A library to dynamically generate a ThemeData object from a JSON file or dynamic map object'
homepage: 'https://github.com/peiffer-innovations/json_theme'
version: '6.2.6+1'
version: '6.3.0'

environment:
sdk: '>=3.0.0 <4.0.0'
Expand All @@ -24,7 +24,7 @@ dev_dependencies:
analyzer: '^6.2.0'
build: '^2.4.1'
build_runner: '^2.4.6'
code_builder: '^4.6.0'
code_builder: '^4.7.0'
flutter_lints: '^2.0.2'
flutter_test:
sdk: 'flutter'
Expand Down
94 changes: 94 additions & 0 deletions json_theme/test/json_theme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,70 @@ void main() {
expect(ThemeEncoder.encodeBoxFit(BoxFit.scaleDown), 'scaleDown');
});

test('BoxHeightStyle', () {
expect(ThemeDecoder.decodeBoxHeightStyle(null), null);
expect(ThemeEncoder.encodeBoxHeightStyle(null), null);

expect(
ThemeDecoder.decodeBoxHeightStyle(
BoxHeightStyle.max,
),
BoxHeightStyle.max,
);

expect(
ThemeDecoder.decodeBoxHeightStyle('includeLineSpacingBottom'),
BoxHeightStyle.includeLineSpacingBottom,
);
expect(
ThemeDecoder.decodeBoxHeightStyle('includeLineSpacingMiddle'),
BoxHeightStyle.includeLineSpacingMiddle,
);
expect(
ThemeDecoder.decodeBoxHeightStyle('includeLineSpacingTop'),
BoxHeightStyle.includeLineSpacingTop,
);
expect(
ThemeDecoder.decodeBoxHeightStyle('max'),
BoxHeightStyle.max,
);
expect(
ThemeDecoder.decodeBoxHeightStyle('strut'),
BoxHeightStyle.strut,
);
expect(
ThemeDecoder.decodeBoxHeightStyle('tight'),
BoxHeightStyle.tight,
);

expect(
ThemeEncoder.encodeBoxHeightStyle(
BoxHeightStyle.includeLineSpacingBottom),
'includeLineSpacingBottom',
);
expect(
ThemeEncoder.encodeBoxHeightStyle(
BoxHeightStyle.includeLineSpacingMiddle),
'includeLineSpacingMiddle',
);
expect(
ThemeEncoder.encodeBoxHeightStyle(BoxHeightStyle.includeLineSpacingTop),
'includeLineSpacingTop',
);
expect(
ThemeEncoder.encodeBoxHeightStyle(BoxHeightStyle.max),
'max',
);
expect(
ThemeEncoder.encodeBoxHeightStyle(BoxHeightStyle.strut),
'strut',
);
expect(
ThemeEncoder.encodeBoxHeightStyle(BoxHeightStyle.tight),
'tight',
);
});

test('BoxShadow', () {
expect(ThemeDecoder.decodeBoxShadow(null), null);
expect(ThemeEncoder.encodeBoxShadow(null), null);
Expand Down Expand Up @@ -1344,6 +1408,36 @@ void main() {
expect(ThemeEncoder.encodeBoxShape(BoxShape.rectangle), 'rectangle');
});

test('BoxWidthStyle', () {
expect(ThemeDecoder.decodeBoxWidthStyle(null), null);
expect(ThemeEncoder.encodeBoxWidthStyle(null), null);

expect(
ThemeDecoder.decodeBoxWidthStyle(
BoxWidthStyle.max,
),
BoxWidthStyle.max,
);

expect(
ThemeDecoder.decodeBoxWidthStyle('max'),
BoxWidthStyle.max,
);
expect(
ThemeDecoder.decodeBoxWidthStyle('tight'),
BoxWidthStyle.tight,
);

expect(
ThemeEncoder.encodeBoxWidthStyle(BoxWidthStyle.max),
'max',
);
expect(
ThemeEncoder.encodeBoxWidthStyle(BoxWidthStyle.tight),
'tight',
);
});

test('Brightness', () {
expect(ThemeDecoder.decodeBrightness(null), null);
expect(ThemeEncoder.encodeBrightness(null), null);
Expand Down

0 comments on commit 35b0ab3

Please sign in to comment.