Skip to content

Commit 6dd766b

Browse files
authored
Merge pull request #162 from conceptadev/feat/create-of-methods-for-design-tokens
Create new of method for spacing
2 parents 0779e23 + 06c8e8e commit 6dd766b

File tree

12 files changed

+2634
-2473
lines changed

12 files changed

+2634
-2473
lines changed

coverage/lcov.info

+2,419-2,424
Large diffs are not rendered by default.

example/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ class CustomMixWidget extends StatelessWidget {
1717
marginVertical(10),
1818
elevation(10),
1919
borderRadius(10),
20-
backgroundColor($md.colors.primary()),
20+
backgroundColor($md.colorScheme.primary()),
2121
text.style($button()),
22-
text.style.color($md.colors.onPrimary()),
22+
text.style.color($md.colorScheme.onPrimary()),
2323
onHover(
2424
elevation(2),
2525
padding(20),
26-
backgroundColor($md.colors.secondary()),
27-
text.style.color($md.colors.onSecondary()),
26+
backgroundColor($md.colorScheme.secondary()),
27+
text.style.color($md.colorScheme.onSecondary()),
2828
),
2929
);
3030
return Box(

lib/src/attributes/border/border_radius_dto.dart

+14-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
22

33
import '../../core/attribute.dart';
44
import '../../factory/mix_provider_data.dart';
5+
import '../../theme/tokens/radius_token.dart';
56

67
/// Represents a [Dto] Data transfer object of [BorderRadiusGeometry]
78
///
@@ -93,20 +94,24 @@ class BorderRadiusGeometryDto extends Dto<BorderRadiusGeometry>
9394

9495
@override
9596
BorderRadiusGeometry resolve(MixData mix) {
96-
const defaultRadius = Radius.zero;
97+
Radius getRadiusValue(Radius? radius) {
98+
if (radius == null) return Radius.zero;
99+
100+
return radius is RadiusRef ? mix.tokens.radiiRef(radius) : radius;
101+
}
97102

98103
return isDirectional
99104
? BorderRadiusDirectional.only(
100-
topStart: topStart ?? defaultRadius,
101-
topEnd: topEnd ?? defaultRadius,
102-
bottomStart: bottomStart ?? defaultRadius,
103-
bottomEnd: bottomEnd ?? defaultRadius,
105+
topStart: getRadiusValue(topStart),
106+
topEnd: getRadiusValue(topEnd),
107+
bottomStart: getRadiusValue(bottomStart),
108+
bottomEnd: getRadiusValue(bottomEnd),
104109
)
105110
: BorderRadius.only(
106-
topLeft: topLeft ?? defaultRadius,
107-
topRight: topRight ?? defaultRadius,
108-
bottomLeft: bottomLeft ?? defaultRadius,
109-
bottomRight: bottomRight ?? defaultRadius,
111+
topLeft: getRadiusValue(topLeft),
112+
topRight: getRadiusValue(topRight),
113+
bottomLeft: getRadiusValue(bottomLeft),
114+
bottomRight: getRadiusValue(bottomRight),
110115
);
111116
}
112117

lib/src/attributes/scalars/scalar_util.dart

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22

33
import '../../core/attribute.dart';
4+
import '../../theme/tokens/radius_token.dart';
45

56
typedef UtilityBuilder<ReturnType, ParamType> = ReturnType Function(
67
ParamType value,
@@ -686,6 +687,8 @@ class RadiusUtility<T extends StyleAttribute> extends MixUtility<T, Radius> {
686687
T circular(double radius) => _builder(Radius.circular(radius));
687688

688689
T call(double radius) => _builder(Radius.circular(radius));
690+
691+
T of(RadiusToken ref) => _builder(ref());
689692
}
690693

691694
/// Utility for setting `TextDecorationStyle` values.

lib/src/attributes/spacing/spacing_util.dart

+2
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,6 @@ class SpacingSideUtility<T extends StyleAttribute>
439439

440440
/// Applies a custom spacing value
441441
T call(double value) => builder(value);
442+
443+
T of(SpaceToken ref) => builder(ref());
442444
}

lib/src/theme/tokens/mix_token.dart

-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ class StyledTokens<T extends MixToken<V>, V> with Comparable {
6767
}
6868

6969
StyledTokens<T, V> merge(StyledTokens<T, V> other) {
70-
// TODO: This is a temporary solution, but it works for now
71-
// ovrite the keys on this _map with the keys from the other _map
72-
7370
final newMap = Map<T, V>.from(_map);
7471

7572
newMap.addAll(other._map);

lib/src/theme/tokens/token_util.dart

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
import 'package:flutter/material.dart';
22

33
import '../material/material_tokens.dart';
4+
import 'breakpoints_token.dart';
45
import 'radius_token.dart';
56
import 'space_token.dart';
67

78
const $md = MaterialTokens();
89
const $radii = RadiiTokenUtil();
910
const $space = SpaceTokenUtil();
1011
const $colors = ColorTokenUtil();
12+
const $breakpoints = BreakpointTokenUtil();
1113
const $textStyles = TextStyleTokenUtil();
1214

1315
@immutable
1416
class RadiiTokenUtil {
1517
const RadiiTokenUtil();
16-
RadiusRef small() => RadiusToken.small();
17-
RadiusRef medium() => RadiusToken.medium();
18-
RadiusRef large() => RadiusToken.large();
18+
19+
final small = RadiusToken.small;
20+
final medium = RadiusToken.medium;
21+
final large = RadiusToken.large;
1922
}
2023

2124
@immutable
2225
class SpaceTokenUtil {
2326
const SpaceTokenUtil();
24-
SpaceRef xsmall() => SpaceToken.xsmall();
25-
SpaceRef small() => SpaceToken.small();
26-
SpaceRef medium() => SpaceToken.medium();
27-
SpaceRef large() => SpaceToken.large();
28-
SpaceRef xlarge() => SpaceToken.xlarge();
29-
SpaceRef xxlarge() => SpaceToken.xxlarge();
27+
28+
final xsmall = SpaceToken.xsmall;
29+
final small = SpaceToken.small;
30+
final medium = SpaceToken.medium;
31+
final large = SpaceToken.large;
32+
final xlarge = SpaceToken.xlarge;
33+
final xxlarge = SpaceToken.xxlarge;
34+
}
35+
36+
@immutable
37+
class BreakpointTokenUtil {
38+
const BreakpointTokenUtil();
39+
40+
final xsmall = BreakpointToken.xsmall;
41+
final small = BreakpointToken.small;
42+
final medium = BreakpointToken.medium;
43+
final large = BreakpointToken.large;
3044
}
3145

3246
@immutable

test/src/theme/mix_theme_test.dart

+78-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,34 @@ import '../../helpers/testing_utils.dart';
66

77
void main() {
88
const primaryColor = ColorToken('primary');
9+
final theme = MixThemeData(
10+
colors: {
11+
primaryColor: Colors.blue,
12+
$md.colorScheme.error: Colors.redAccent,
13+
},
14+
breakpoints: {
15+
$breakpoints.small: const Breakpoint(
16+
minWidth: 0,
17+
maxWidth: 599,
18+
),
19+
},
20+
radii: {
21+
$radii.small: const Radius.circular(200),
22+
$radii.large: const Radius.circular(2000),
23+
},
24+
space: {
25+
$space.small: 30,
26+
},
27+
textStyles: {
28+
$md.textTheme.bodyLarge: const TextStyle(
29+
fontSize: 200,
30+
fontWeight: FontWeight.w300,
31+
),
32+
},
33+
);
34+
935
group('MixTheme', () {
1036
testWidgets('MixTheme.of', (tester) async {
11-
final theme = MixThemeData(
12-
colors: {
13-
primaryColor: Colors.blue,
14-
},
15-
);
16-
1737
await tester.pumpWithMixTheme(Container(), theme: theme);
1838

1939
final context = tester.element(find.byType(Container));
@@ -22,6 +42,58 @@ void main() {
2242
expect(MixTheme.maybeOf(context), theme);
2343
});
2444

45+
testWidgets(
46+
'when applied to Box via Style, it must reproduce the same values than the theme',
47+
(tester) async {
48+
const key = Key('box');
49+
50+
await tester.pumpWithMixTheme(
51+
Box(
52+
key: key,
53+
style: Style(
54+
box.color.of(primaryColor),
55+
box.borderRadius.all.of($radii.small),
56+
box.padding.horizontal.of($space.small),
57+
text.style.of($md.textTheme.bodyLarge),
58+
),
59+
child: const StyledText('Hello'),
60+
),
61+
theme: theme,
62+
);
63+
64+
final container = tester.widget<Container>(
65+
find.descendant(
66+
of: find.byKey(key),
67+
matching: find.byType(Container),
68+
),
69+
);
70+
71+
expect(
72+
container.decoration,
73+
BoxDecoration(
74+
color: theme.colors[primaryColor],
75+
borderRadius: BorderRadius.all(theme.radii[$radii.small]!),
76+
),
77+
);
78+
79+
expect(
80+
container.padding!.horizontal / 2,
81+
theme.space[$space.small],
82+
);
83+
84+
final textWidget = tester.widget<Text>(
85+
find.descendant(
86+
of: find.byKey(key),
87+
matching: find.byType(Text),
88+
),
89+
);
90+
91+
expect(
92+
textWidget.style,
93+
theme.textStyles[$md.textTheme.bodyLarge],
94+
);
95+
});
96+
2597
// maybeOf
2698
testWidgets('MixTheme.maybeOf', (tester) async {
2799
await tester.pumpMaterialApp(Container());

test/src/theme/tokens/radius_token_test.dart

+3-4
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,16 @@ void main() {
5757
});
5858

5959
group('RadiiTokenUtil', () {
60-
const radii = RadiiTokenUtil();
6160
test('small returns correct value', () {
62-
expect(radii.small(), RadiusToken.small());
61+
expect(const RadiiTokenUtil().small, RadiusToken.small);
6362
});
6463

6564
test('medium returns correct value', () {
66-
expect(radii.medium(), RadiusToken.medium());
65+
expect(const RadiiTokenUtil().medium, RadiusToken.medium);
6766
});
6867

6968
test('large returns correct value', () {
70-
expect(radii.large(), RadiusToken.large());
69+
expect(const RadiiTokenUtil().large, RadiusToken.large);
7170
});
7271
});
7372

test/src/theme/tokens/space_token_test.dart

-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import '../../../helpers/testing_utils.dart';
66
void main() {
77
group('SpaceToken tests', () {
88
test('SpaceToken.xsmall() returns correct value', () {
9-
expect(SpaceToken.xsmall(), SpaceToken.xsmall());
109
expect(const SpaceToken('mix.space.xsmall'), SpaceToken.xsmall);
1110
expect('mix.space.xsmall', SpaceToken.xsmall.name);
1211
expect('mix.space.xsmall'.hashCode, SpaceToken.xsmall.name.hashCode);
@@ -16,7 +15,6 @@ void main() {
1615
});
1716

1817
test('SpaceToken.small() returns correct value', () {
19-
expect(SpaceToken.small(), SpaceToken.small());
2018
expect(const SpaceToken('mix.space.small'), SpaceToken.small);
2119
expect('mix.space.small', SpaceToken.small.name);
2220
expect('mix.space.small'.hashCode, SpaceToken.small.name.hashCode);
@@ -26,7 +24,6 @@ void main() {
2624
});
2725

2826
test('SpaceToken.medium() returns correct value', () {
29-
expect(SpaceToken.medium(), SpaceToken.medium());
3027
expect(const SpaceToken('mix.space.medium'), SpaceToken.medium);
3128
expect('mix.space.medium', SpaceToken.medium.name);
3229
expect('mix.space.medium'.hashCode, SpaceToken.medium.name.hashCode);
@@ -36,14 +33,12 @@ void main() {
3633
});
3734

3835
test('SpaceToken.large() returns correct value', () {
39-
expect(SpaceToken.large(), SpaceToken.large());
4036
expect(const SpaceToken('mix.space.large'), SpaceToken.large);
4137
expect('mix.space.large', SpaceToken.large.name);
4238
expect('mix.space.large'.hashCode, SpaceToken.large.name.hashCode);
4339
});
4440

4541
test('SpaceToken.xlarge() returns correct value', () {
46-
expect(SpaceToken.xlarge(), SpaceToken.xlarge());
4742
expect(const SpaceToken('mix.space.xlarge'), SpaceToken.xlarge);
4843
expect('mix.space.xlarge', SpaceToken.xlarge.name);
4944
expect('mix.space.xlarge'.hashCode, SpaceToken.xlarge.name.hashCode);
@@ -53,7 +48,6 @@ void main() {
5348
});
5449

5550
test('SpaceToken.xxlarge() returns correct value', () {
56-
expect(SpaceToken.xxlarge(), SpaceToken.xxlarge());
5751
expect(const SpaceToken('mix.space.xxlarge'), SpaceToken.xxlarge);
5852
expect('mix.space.xxlarge', SpaceToken.xxlarge.name);
5953
expect('mix.space.xxlarge'.hashCode, SpaceToken.xxlarge.name.hashCode);

0 commit comments

Comments
 (0)