@@ -2,74 +2,36 @@ import 'package:flutter/widgets.dart';
2
2
import 'package:responsive_layout/responsive_layout.dart' ;
3
3
4
4
import 'src/layout_resolver.dart' ;
5
- import 'src/utilities.dart' ;
6
-
7
5
8
6
export 'responsive_layout.dart' ;
9
7
10
- // extension LayoutMetadataCtx on BuildContext {
11
- // LayoutMetadata get layoutMetadata => watch<LayoutMetadata>();
12
- // }
13
-
14
- // typedef LayoutValue<T> = T Function(BuildContext context);
15
-
16
-
17
-
18
8
enum CommonBreakpoint {
19
9
desktop, tablet, phone, tinyHardware
20
10
}
21
11
22
-
12
+ @immutable
23
13
class CommonLayout extends LayoutResolver <CommonBreakpoint > {
24
- CommonLayout ({
25
- this .desktop = 769 ,
26
- this .tablet = 481 ,
27
- this .phone = 321
28
- }) {
29
- if (desktop <= tablet || desktop <= phone) {
30
- throw 'tablet (tablet) and/or phone (phone) size was greater than desktop size (desktop)' ;
31
- }
32
-
33
- if (tablet <= phone) {
34
- throw 'phone size (phone) was greater than tablet size (tablet)' ;
35
- }
36
- }
14
+ CommonLayout (double size, {
15
+ int desktop = 769 ,
16
+ int tablet = 481 ,
17
+ int phone = 321
18
+ }): super (size: size, breakpoints: [Breakpoint (desktop, value: CommonBreakpoint .desktop), Breakpoint (tablet, value: CommonBreakpoint .tablet), Breakpoint (phone, value: CommonBreakpoint .phone), Breakpoint (null , value: CommonBreakpoint .tinyHardware),]);
37
19
38
- final int desktop;
39
- final int tablet;
40
- final int phone;
20
+ // Helpers
41
21
42
- @override
43
- CommonBreakpoint resolveBreakpoint (double size) {
44
- if (size < phone) {
45
- return CommonBreakpoint .tinyHardware;
46
- } else if (size < tablet) {
47
- return CommonBreakpoint .phone;
48
- } else if (size < desktop) {
49
- return CommonBreakpoint .tablet;
50
- }
51
-
52
- return CommonBreakpoint .desktop;
53
- }
54
- }
55
-
56
- extension CommonLayoutValue on BuildContext {
57
- CommonLayout get commonLayout => LayoutResolverWidget .of (this ).resolver as CommonLayout ;
58
- CommonBreakpoint get breakpoint => commonLayout.resolveBreakpoint (deviceWidth);
59
-
60
- bool get isTinyHardware => breakpoint == CommonBreakpoint .tinyHardware;
22
+ bool get isTinyHardware => matchesValue (CommonBreakpoint .tinyHardware);
61
23
62
- bool get isPhoneOrSmaller => breakpoint == CommonBreakpoint .phone || breakpoint == CommonBreakpoint .tinyHardware ;
63
- bool get isPhone => breakpoint == CommonBreakpoint .phone;
64
- bool get isPhoneOrLarger => breakpoint != CommonBreakpoint .tinyHardware ;
24
+ bool get isPhoneOrSmaller => matchesValueOrSmaller ( CommonBreakpoint .phone) ;
25
+ bool get isPhone => matchesValue ( CommonBreakpoint .phone) ;
26
+ bool get isPhoneOrLarger => matchesValueOrLarger ( CommonBreakpoint .phone) ;
65
27
66
- bool get isTabletOrSmaller => breakpoint != CommonBreakpoint .desktop ;
67
- bool get isTablet => breakpoint == CommonBreakpoint .tablet;
68
- bool get isTabletOrLarger => breakpoint != CommonBreakpoint .phone && breakpoint != CommonBreakpoint .tinyHardware ;
28
+ bool get isTabletOrSmaller => matchesValueOrSmaller ( CommonBreakpoint .tablet) ;
29
+ bool get isTablet => matchesValue ( CommonBreakpoint .tablet) ;
30
+ bool get isTabletOrLarger => matchesValueOrLarger ( CommonBreakpoint .tablet) ;
69
31
70
- bool get isDesktop => breakpoint == CommonBreakpoint .desktop;
32
+ bool get isDesktop => matchesValue ( CommonBreakpoint .desktop) ;
71
33
72
- T layoutValue <T >({
34
+ T value <T >({
73
35
T ? desktop,
74
36
T ? tablet,
75
37
T ? phone,
@@ -79,27 +41,31 @@ extension CommonLayoutValue on BuildContext {
79
41
throw 'At least one breakpoint must be provided' ;
80
42
}
81
43
82
- // If it's in the exact range and has a layout supplied, try to use it,
83
- // otherwise cascade down all the available values
84
- if (desktop != null && breakpoint == CommonBreakpoint .desktop) {
44
+ // If it's in the exact range and has a layout supplied, try to use it...
45
+ if (desktop != null && breakpointValue == CommonBreakpoint .desktop) {
85
46
return desktop;
86
- } else if (tablet != null && breakpoint == CommonBreakpoint .tablet) {
47
+ } else if (tablet != null && breakpointValue == CommonBreakpoint .tablet) {
87
48
return tablet;
88
- } else if (phone != null && breakpoint == CommonBreakpoint .phone) {
49
+ } else if (phone != null && breakpointValue == CommonBreakpoint .phone) {
89
50
return phone;
90
51
} else if (tinyHardware != null ) {
91
52
return tinyHardware;
92
53
}
93
-
94
- // Get the largest non-null layout supplied
95
- if (desktop != null ) {
96
- return desktop;
97
- } else if (tablet != null ) {
98
- return tablet;
99
- } else if (phone != null ) {
100
- return phone;
101
- } else {
102
- return tinyHardware! ;
103
- }
54
+ // ... otherwise cascade down all the available values to get the largest
55
+ // non-null layout supplied
56
+ if (desktop != null ) {
57
+ return desktop;
58
+ } else if (tablet != null ) {
59
+ return tablet;
60
+ } else if (phone != null ) {
61
+ return phone;
62
+ } else {
63
+ return tinyHardware! ;
64
+ }
104
65
}
105
66
}
67
+
68
+ // Widget Utility
69
+ extension CommonResolverWidget on BuildContext {
70
+ CommonLayout get layout => LayoutResolverWidget .of (this ).resolver as CommonLayout ;
71
+ }
0 commit comments