Committed by
GitHub
Merge pull request #342 from Husssam12/master
Follow package layout conventions and add some useful extensions
Showing
9 changed files
with
266 additions
and
15 deletions
@@ -203,6 +203,9 @@ class _HomePageState extends State<HomePage> { | @@ -203,6 +203,9 @@ class _HomePageState extends State<HomePage> { | ||
203 | 0.5.sh //50% of screen height | 203 | 0.5.sh //50% of screen height |
204 | 20.setVerticalSpacing // SizedBox(height: 20 * scaleHeight) | 204 | 20.setVerticalSpacing // SizedBox(height: 20 * scaleHeight) |
205 | 20.horizontalSpace // SizedBox(height: 20 * scaleWidth) | 205 | 20.horizontalSpace // SizedBox(height: 20 * scaleWidth) |
206 | + const RPadding.all(8) // Padding.all(8.r) - take advantage of const key word | ||
207 | + REdgeInsts.all(8) // EdgeInsets.all(8.r) | ||
208 | + EdgeInsets.only(left:8,right:8).r // EdgeInsets.only(left:8.r,right:8.r). | ||
206 | ``` | 209 | ``` |
207 | 210 | ||
208 | #### Adapt screen size: | 211 | #### Adapt screen size: |
@@ -161,6 +161,9 @@ class _HomePageState extends State<HomePage> { | @@ -161,6 +161,9 @@ class _HomePageState extends State<HomePage> { | ||
161 | 0.5.sh //屏幕高度的50% | 161 | 0.5.sh //屏幕高度的50% |
162 | 20.setVerticalSpacing // SizedBox(height: 20 * scaleHeight) | 162 | 20.setVerticalSpacing // SizedBox(height: 20 * scaleHeight) |
163 | 20.horizontalSpace // SizedBox(height: 20 * scaleWidth) | 163 | 20.horizontalSpace // SizedBox(height: 20 * scaleWidth) |
164 | + const RPadding.all(8) // Padding.all(8.r) - take advantage of const key word | ||
165 | + REdgeInsts.all(8) // EdgeInsets.all(8.r) | ||
166 | + EdgeInsets.only(left:8,right:8).r // EdgeInsets.only(left:8.r,right:8.r). | ||
164 | 167 | ||
165 | ``` | 168 | ``` |
166 | 169 |
@@ -153,6 +153,9 @@ class _HomePageState extends State<HomePage> { | @@ -153,6 +153,9 @@ class _HomePageState extends State<HomePage> { | ||
153 | 0.5.sh //50% altura da tela | 153 | 0.5.sh //50% altura da tela |
154 | 20.setVerticalSpacing // SizedBox(height: 20 * scaleHeight) | 154 | 20.setVerticalSpacing // SizedBox(height: 20 * scaleHeight) |
155 | 20.horizontalSpace // SizedBox(height: 20 * scaleWidth) | 155 | 20.horizontalSpace // SizedBox(height: 20 * scaleWidth) |
156 | + const RPadding.all(8) // Padding.all(8.r) - take advantage of const key word | ||
157 | + REdgeInsts.all(8) // EdgeInsets.all(8.r) | ||
158 | + EdgeInsets.only(left:8,right:8).r // EdgeInsets.only(left:8.r,right:8.r). | ||
156 | ``` | 159 | ``` |
157 | 160 | ||
158 | #### Adaptar o tamanho da tela: | 161 | #### Adaptar o tamanho da tela: |
@@ -5,14 +5,8 @@ | @@ -5,14 +5,8 @@ | ||
5 | 5 | ||
6 | library flutter_screenutil; | 6 | library flutter_screenutil; |
7 | 7 | ||
8 | -import 'dart:math'; | ||
9 | -import 'dart:ui' as ui; | ||
10 | -import 'dart:ui'; | ||
11 | - | ||
12 | -import 'package:flutter/material.dart'; | ||
13 | - | ||
14 | -part 'screen_util.dart'; | ||
15 | - | ||
16 | -part 'screenutil_init.dart'; | ||
17 | - | ||
18 | -part 'size_extension.dart'; | 8 | +export 'src/r_padding.dart'; |
9 | +export 'src/r_sizedbox.dart'; | ||
10 | +export 'src/screen_util.dart'; | ||
11 | +export 'src/screenutil_init.dart'; | ||
12 | +export 'src/size_extension.dart'; |
lib/src/r_padding.dart
0 → 100644
1 | +import 'package:flutter/material.dart'; | ||
2 | +import 'package:flutter/rendering.dart'; | ||
3 | + | ||
4 | +import 'size_extension.dart'; | ||
5 | + | ||
6 | +class RPadding extends SingleChildRenderObjectWidget { | ||
7 | + /// Creates a adapt widget that insets its child. | ||
8 | + /// | ||
9 | + /// The [padding] argument must not be null. | ||
10 | + const RPadding({ | ||
11 | + Key? key, | ||
12 | + required Widget child, | ||
13 | + required this.padding, | ||
14 | + }) : super(key: key, child: child); | ||
15 | + | ||
16 | + /// The amount of space by which to inset the child. | ||
17 | + final EdgeInsets padding; | ||
18 | + | ||
19 | + @override | ||
20 | + RenderPadding createRenderObject(BuildContext context) { | ||
21 | + return RenderPadding( | ||
22 | + padding: padding is REdgeInsets ? padding : padding.r, | ||
23 | + textDirection: Directionality.maybeOf(context), | ||
24 | + ); | ||
25 | + } | ||
26 | +} | ||
27 | + | ||
28 | +class REdgeInsets extends EdgeInsets { | ||
29 | + /// Creates adapt insets from offsets from the left, top, right, and bottom. | ||
30 | + REdgeInsets.fromLTRB(double left, double top, double right, double bottom) | ||
31 | + : super.fromLTRB(left.r, top.r, right.r, bottom.r); | ||
32 | + | ||
33 | + /// Creates adapt insets where all the offsets are `value`. | ||
34 | + /// | ||
35 | + /// {@tool snippet} | ||
36 | + /// | ||
37 | + /// Adapt height-pixel margin on all sides: | ||
38 | + /// | ||
39 | + /// ```dart | ||
40 | + /// const REdgeInsets.all(8.0) | ||
41 | + /// ``` | ||
42 | + /// {@end-tool} | ||
43 | + REdgeInsets.all(double value) : super.all(value.r); | ||
44 | + | ||
45 | + /// Creates adapt insets with symmetrical vertical and horizontal offsets. | ||
46 | + /// | ||
47 | + /// {@tool snippet} | ||
48 | + /// | ||
49 | + /// Adapt Eight pixel margin above and below, no horizontal margins: | ||
50 | + /// | ||
51 | + /// ```dart | ||
52 | + /// const REdgeInsets.symmetric(vertical: 8.0) | ||
53 | + /// ``` | ||
54 | + /// {@end-tool} | ||
55 | + REdgeInsets.symmetric({ | ||
56 | + double vertical = 0, | ||
57 | + double horizontal = 0, | ||
58 | + }) : super.symmetric(vertical: vertical.r, horizontal: horizontal.r); | ||
59 | + | ||
60 | + /// Creates adapt insets with only the given values non-zero. | ||
61 | + /// | ||
62 | + /// {@tool snippet} | ||
63 | + /// | ||
64 | + /// Adapt left margin indent of 40 pixels: | ||
65 | + /// | ||
66 | + /// ```dart | ||
67 | + /// const REdgeInsets.only(left: 40.0) | ||
68 | + /// ``` | ||
69 | + /// {@end-tool} | ||
70 | + REdgeInsets.only({ | ||
71 | + double bottom = 0, | ||
72 | + double right = 0, | ||
73 | + double left = 0, | ||
74 | + double top = 0, | ||
75 | + }) : super.only( | ||
76 | + bottom: bottom.r, | ||
77 | + right: right.r, | ||
78 | + left: left.r, | ||
79 | + top: top.r, | ||
80 | + ); | ||
81 | +} | ||
82 | + | ||
83 | +class REdgeInsetsDirectional extends EdgeInsetsDirectional { | ||
84 | + /// Creates insets where all the offsets are `value`. | ||
85 | + /// | ||
86 | + /// {@tool snippet} | ||
87 | + /// | ||
88 | + /// Adapt eight-pixel margin on all sides: | ||
89 | + /// | ||
90 | + /// ```dart | ||
91 | + /// const REdgeInsetsDirectional.all(8.0) | ||
92 | + /// ``` | ||
93 | + /// {@end-tool} | ||
94 | + REdgeInsetsDirectional.all(double value) : super.all(value.r); | ||
95 | + | ||
96 | + /// Creates insets with only the given values non-zero. | ||
97 | + /// | ||
98 | + /// {@tool snippet} | ||
99 | + /// | ||
100 | + /// Adapt margin indent of 40 pixels on the leading side: | ||
101 | + /// | ||
102 | + /// ```dart | ||
103 | + /// const REdgeInsetsDirectional.only(start: 40.0) | ||
104 | + /// ``` | ||
105 | + /// {@end-tool} | ||
106 | + REdgeInsetsDirectional.only({ | ||
107 | + double bottom = 0, | ||
108 | + double end = 0, | ||
109 | + double start = 0, | ||
110 | + double top = 0, | ||
111 | + }) : super.only( | ||
112 | + bottom: bottom.r, | ||
113 | + start: start.r, | ||
114 | + end: end.r, | ||
115 | + top: top.r, | ||
116 | + ); | ||
117 | + | ||
118 | + /// Creates adapt insets from offsets from the start, top, end, and bottom. | ||
119 | + REdgeInsetsDirectional.fromSTEB( | ||
120 | + double start, | ||
121 | + double top, | ||
122 | + double end, | ||
123 | + double bottom, | ||
124 | + ) : super.fromSTEB( | ||
125 | + start.r, | ||
126 | + top.r, | ||
127 | + end.r, | ||
128 | + bottom.r, | ||
129 | + ); | ||
130 | +} |
lib/src/r_sizedbox.dart
0 → 100644
1 | +import 'package:flutter/material.dart'; | ||
2 | +import 'package:flutter/rendering.dart'; | ||
3 | + | ||
4 | +import 'size_extension.dart'; | ||
5 | + | ||
6 | +class RSizedBox extends SizedBox { | ||
7 | + const RSizedBox({ | ||
8 | + Key? key, | ||
9 | + double? height, | ||
10 | + double? width, | ||
11 | + Widget? child, | ||
12 | + }) : _square = false, | ||
13 | + super(key: key, child: child, width: width, height: height); | ||
14 | + | ||
15 | + const RSizedBox.vertical( | ||
16 | + double? height, { | ||
17 | + Key? key, | ||
18 | + Widget? child, | ||
19 | + }) : _square = false, | ||
20 | + super(key: key, child: child, height: height); | ||
21 | + | ||
22 | + const RSizedBox.horizontal( | ||
23 | + double? width, { | ||
24 | + Key? key, | ||
25 | + Widget? child, | ||
26 | + }) : _square = false, | ||
27 | + super(key: key, child: child, width: width); | ||
28 | + | ||
29 | + const RSizedBox.square({ | ||
30 | + Key? key, | ||
31 | + double? height, | ||
32 | + double? dimension, | ||
33 | + Widget? child, | ||
34 | + }) : _square = true, | ||
35 | + super.square(key: key, child: child, dimension: dimension); | ||
36 | + | ||
37 | + RSizedBox.fromSize({ | ||
38 | + Key? key, | ||
39 | + Size? size, | ||
40 | + Widget? child, | ||
41 | + }) : _square = false, | ||
42 | + super.fromSize(key: key, child: child, size: size); | ||
43 | + | ||
44 | + @override | ||
45 | + RenderConstrainedBox createRenderObject(BuildContext context) { | ||
46 | + return RenderConstrainedBox( | ||
47 | + additionalConstraints: _additionalConstraints, | ||
48 | + ); | ||
49 | + } | ||
50 | + | ||
51 | + final bool _square; | ||
52 | + | ||
53 | + BoxConstraints get _additionalConstraints { | ||
54 | + final boxConstraints = | ||
55 | + BoxConstraints.tightFor(width: width, height: height); | ||
56 | + return _square ? boxConstraints.r : boxConstraints.hw; | ||
57 | + } | ||
58 | + | ||
59 | + @override | ||
60 | + void updateRenderObject( | ||
61 | + BuildContext context, RenderConstrainedBox renderObject) { | ||
62 | + renderObject.additionalConstraints = _additionalConstraints; | ||
63 | + } | ||
64 | +} |
@@ -2,7 +2,10 @@ | @@ -2,7 +2,10 @@ | ||
2 | * Created by 李卓原 on 2018/9/29. | 2 | * Created by 李卓原 on 2018/9/29. |
3 | * email: zhuoyuan93@gmail.com | 3 | * email: zhuoyuan93@gmail.com |
4 | */ | 4 | */ |
5 | -part of flutter_screenutil; | 5 | + |
6 | +import 'dart:math'; | ||
7 | + | ||
8 | +import 'package:flutter/material.dart'; | ||
6 | 9 | ||
7 | class ScreenUtil { | 10 | class ScreenUtil { |
8 | static const Size defaultSize = Size(360, 690); | 11 | static const Size defaultSize = Size(360, 690); |
1 | -part of flutter_screenutil; | 1 | +import 'package:flutter/cupertino.dart'; |
2 | + | ||
3 | +import 'screen_util.dart'; | ||
2 | 4 | ||
3 | class ScreenUtilInit extends StatelessWidget { | 5 | class ScreenUtilInit extends StatelessWidget { |
4 | /// A helper widget that initializes [ScreenUtil] | 6 | /// A helper widget that initializes [ScreenUtil] |
5 | - ScreenUtilInit({ | 7 | + const ScreenUtilInit({ |
6 | required this.builder, | 8 | required this.builder, |
7 | this.designSize = ScreenUtil.defaultSize, | 9 | this.designSize = ScreenUtil.defaultSize, |
8 | this.splitScreenMode = false, | 10 | this.splitScreenMode = false, |
1 | -part of flutter_screenutil; | 1 | +import 'dart:math'; |
2 | + | ||
3 | +import 'package:flutter/material.dart'; | ||
4 | + | ||
5 | +import 'screen_util.dart'; | ||
2 | 6 | ||
3 | extension SizeExtension on num { | 7 | extension SizeExtension on num { |
4 | ///[ScreenUtil.setWidth] | 8 | ///[ScreenUtil.setWidth] |
@@ -44,3 +48,48 @@ extension SizeExtension on num { | @@ -44,3 +48,48 @@ extension SizeExtension on num { | ||
44 | Widget get verticalSpacingRadius => | 48 | Widget get verticalSpacingRadius => |
45 | ScreenUtil().setVerticalSpacingRadius(this); | 49 | ScreenUtil().setVerticalSpacingRadius(this); |
46 | } | 50 | } |
51 | + | ||
52 | +extension EdgeInsetsExtension on EdgeInsets { | ||
53 | + /// Creates adapt insets using r [SizeExtension]. | ||
54 | + EdgeInsets get r => copyWith( | ||
55 | + top: top.r, | ||
56 | + bottom: bottom.r, | ||
57 | + right: right.r, | ||
58 | + left: left.r, | ||
59 | + ); | ||
60 | +} | ||
61 | + | ||
62 | +extension BorderRaduisExtension on BorderRadius { | ||
63 | + /// Creates adapt BorderRadius using r [SizeExtension]. | ||
64 | + BorderRadius get r => copyWith( | ||
65 | + bottomLeft: bottomLeft.r, | ||
66 | + bottomRight: bottomLeft.r, | ||
67 | + topLeft: topLeft.r, | ||
68 | + topRight: topRight.r, | ||
69 | + ); | ||
70 | +} | ||
71 | + | ||
72 | +extension RaduisExtension on Radius { | ||
73 | + /// Creates adapt Radius using r [SizeExtension]. | ||
74 | + Radius get r => this | ||
75 | + ..x.r | ||
76 | + ..y.r; | ||
77 | +} | ||
78 | + | ||
79 | +extension BoxConstraintsExtension on BoxConstraints { | ||
80 | + /// Creates adapt BoxConstraints using r [SizeExtension]. | ||
81 | + BoxConstraints get r => this.copyWith( | ||
82 | + maxHeight: maxHeight.r, | ||
83 | + maxWidth: maxWidth.r, | ||
84 | + minHeight: minHeight.r, | ||
85 | + minWidth: minWidth.r, | ||
86 | + ); | ||
87 | + | ||
88 | + /// Creates adapt BoxConstraints using h-w [SizeExtension]. | ||
89 | + BoxConstraints get hw => this.copyWith( | ||
90 | + maxHeight: maxHeight.h, | ||
91 | + maxWidth: maxWidth.w, | ||
92 | + minHeight: minHeight.h, | ||
93 | + minWidth: minWidth.w, | ||
94 | + ); | ||
95 | +} |
-
Please register or login to post a comment