Showing
2 changed files
with
69 additions
and
1 deletions
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.square({ | ||
16 | + Key? key, | ||
17 | + double? height, | ||
18 | + double? dimension, | ||
19 | + Widget? child, | ||
20 | + }) : _square = true, | ||
21 | + super.square(key: key, child: child, dimension: dimension); | ||
22 | + | ||
23 | + RSizedBox.fromSize({ | ||
24 | + Key? key, | ||
25 | + Size? size, | ||
26 | + Widget? child, | ||
27 | + }) : _square = false, | ||
28 | + super.fromSize(key: key, child: child, size: size); | ||
29 | + | ||
30 | + @override | ||
31 | + RenderConstrainedBox createRenderObject(BuildContext context) { | ||
32 | + return RenderConstrainedBox( | ||
33 | + additionalConstraints: _additionalConstraints, | ||
34 | + ); | ||
35 | + } | ||
36 | + | ||
37 | + final bool _square; | ||
38 | + | ||
39 | + BoxConstraints get _additionalConstraints { | ||
40 | + final boxConstraints = | ||
41 | + BoxConstraints.tightFor(width: width, height: height); | ||
42 | + return _square ? boxConstraints.r : boxConstraints.hw; | ||
43 | + } | ||
44 | + | ||
45 | + @override | ||
46 | + void updateRenderObject( | ||
47 | + BuildContext context, RenderConstrainedBox renderObject) { | ||
48 | + renderObject.additionalConstraints = _additionalConstraints; | ||
49 | + } | ||
50 | +} |
@@ -2,7 +2,7 @@ import 'dart:math'; | @@ -2,7 +2,7 @@ import 'dart:math'; | ||
2 | 2 | ||
3 | import 'package:flutter/material.dart'; | 3 | import 'package:flutter/material.dart'; |
4 | 4 | ||
5 | -import '../flutter_screenutil.dart'; | 5 | +import 'screen_util.dart'; |
6 | 6 | ||
7 | extension SizeExtension on num { | 7 | extension SizeExtension on num { |
8 | ///[ScreenUtil.setWidth] | 8 | ///[ScreenUtil.setWidth] |
@@ -75,3 +75,21 @@ extension RaduisExtension on Radius { | @@ -75,3 +75,21 @@ extension RaduisExtension on Radius { | ||
75 | ..x.r | 75 | ..x.r |
76 | ..y.r; | 76 | ..y.r; |
77 | } | 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