LiZhuoyuan
Committed by GitHub

Merge pull request #342 from Husssam12/master

Follow package layout conventions and add some useful extensions
... ... @@ -203,6 +203,9 @@ class _HomePageState extends State<HomePage> {
0.5.sh //50% of screen height
20.setVerticalSpacing // SizedBox(height: 20 * scaleHeight)
20.horizontalSpace // SizedBox(height: 20 * scaleWidth)
const RPadding.all(8) // Padding.all(8.r) - take advantage of const key word
REdgeInsts.all(8) // EdgeInsets.all(8.r)
EdgeInsets.only(left:8,right:8).r // EdgeInsets.only(left:8.r,right:8.r).
```
#### Adapt screen size:
... ...
... ... @@ -161,6 +161,9 @@ class _HomePageState extends State<HomePage> {
0.5.sh //屏幕高度的50%
20.setVerticalSpacing // SizedBox(height: 20 * scaleHeight)
20.horizontalSpace // SizedBox(height: 20 * scaleWidth)
const RPadding.all(8) // Padding.all(8.r) - take advantage of const key word
REdgeInsts.all(8) // EdgeInsets.all(8.r)
EdgeInsets.only(left:8,right:8).r // EdgeInsets.only(left:8.r,right:8.r).
```
... ...
... ... @@ -153,6 +153,9 @@ class _HomePageState extends State<HomePage> {
0.5.sh //50% altura da tela
20.setVerticalSpacing // SizedBox(height: 20 * scaleHeight)
20.horizontalSpace // SizedBox(height: 20 * scaleWidth)
const RPadding.all(8) // Padding.all(8.r) - take advantage of const key word
REdgeInsts.all(8) // EdgeInsets.all(8.r)
EdgeInsets.only(left:8,right:8).r // EdgeInsets.only(left:8.r,right:8.r).
```
#### Adaptar o tamanho da tela:
... ...
... ... @@ -5,14 +5,8 @@
library flutter_screenutil;
import 'dart:math';
import 'dart:ui' as ui;
import 'dart:ui';
import 'package:flutter/material.dart';
part 'screen_util.dart';
part 'screenutil_init.dart';
part 'size_extension.dart';
export 'src/r_padding.dart';
export 'src/r_sizedbox.dart';
export 'src/screen_util.dart';
export 'src/screenutil_init.dart';
export 'src/size_extension.dart';
... ...
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'size_extension.dart';
class RPadding extends SingleChildRenderObjectWidget {
/// Creates a adapt widget that insets its child.
///
/// The [padding] argument must not be null.
const RPadding({
Key? key,
required Widget child,
required this.padding,
}) : super(key: key, child: child);
/// The amount of space by which to inset the child.
final EdgeInsets padding;
@override
RenderPadding createRenderObject(BuildContext context) {
return RenderPadding(
padding: padding is REdgeInsets ? padding : padding.r,
textDirection: Directionality.maybeOf(context),
);
}
}
class REdgeInsets extends EdgeInsets {
/// Creates adapt insets from offsets from the left, top, right, and bottom.
REdgeInsets.fromLTRB(double left, double top, double right, double bottom)
: super.fromLTRB(left.r, top.r, right.r, bottom.r);
/// Creates adapt insets where all the offsets are `value`.
///
/// {@tool snippet}
///
/// Adapt height-pixel margin on all sides:
///
/// ```dart
/// const REdgeInsets.all(8.0)
/// ```
/// {@end-tool}
REdgeInsets.all(double value) : super.all(value.r);
/// Creates adapt insets with symmetrical vertical and horizontal offsets.
///
/// {@tool snippet}
///
/// Adapt Eight pixel margin above and below, no horizontal margins:
///
/// ```dart
/// const REdgeInsets.symmetric(vertical: 8.0)
/// ```
/// {@end-tool}
REdgeInsets.symmetric({
double vertical = 0,
double horizontal = 0,
}) : super.symmetric(vertical: vertical.r, horizontal: horizontal.r);
/// Creates adapt insets with only the given values non-zero.
///
/// {@tool snippet}
///
/// Adapt left margin indent of 40 pixels:
///
/// ```dart
/// const REdgeInsets.only(left: 40.0)
/// ```
/// {@end-tool}
REdgeInsets.only({
double bottom = 0,
double right = 0,
double left = 0,
double top = 0,
}) : super.only(
bottom: bottom.r,
right: right.r,
left: left.r,
top: top.r,
);
}
class REdgeInsetsDirectional extends EdgeInsetsDirectional {
/// Creates insets where all the offsets are `value`.
///
/// {@tool snippet}
///
/// Adapt eight-pixel margin on all sides:
///
/// ```dart
/// const REdgeInsetsDirectional.all(8.0)
/// ```
/// {@end-tool}
REdgeInsetsDirectional.all(double value) : super.all(value.r);
/// Creates insets with only the given values non-zero.
///
/// {@tool snippet}
///
/// Adapt margin indent of 40 pixels on the leading side:
///
/// ```dart
/// const REdgeInsetsDirectional.only(start: 40.0)
/// ```
/// {@end-tool}
REdgeInsetsDirectional.only({
double bottom = 0,
double end = 0,
double start = 0,
double top = 0,
}) : super.only(
bottom: bottom.r,
start: start.r,
end: end.r,
top: top.r,
);
/// Creates adapt insets from offsets from the start, top, end, and bottom.
REdgeInsetsDirectional.fromSTEB(
double start,
double top,
double end,
double bottom,
) : super.fromSTEB(
start.r,
top.r,
end.r,
bottom.r,
);
}
... ...
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'size_extension.dart';
class RSizedBox extends SizedBox {
const RSizedBox({
Key? key,
double? height,
double? width,
Widget? child,
}) : _square = false,
super(key: key, child: child, width: width, height: height);
const RSizedBox.vertical(
double? height, {
Key? key,
Widget? child,
}) : _square = false,
super(key: key, child: child, height: height);
const RSizedBox.horizontal(
double? width, {
Key? key,
Widget? child,
}) : _square = false,
super(key: key, child: child, width: width);
const RSizedBox.square({
Key? key,
double? height,
double? dimension,
Widget? child,
}) : _square = true,
super.square(key: key, child: child, dimension: dimension);
RSizedBox.fromSize({
Key? key,
Size? size,
Widget? child,
}) : _square = false,
super.fromSize(key: key, child: child, size: size);
@override
RenderConstrainedBox createRenderObject(BuildContext context) {
return RenderConstrainedBox(
additionalConstraints: _additionalConstraints,
);
}
final bool _square;
BoxConstraints get _additionalConstraints {
final boxConstraints =
BoxConstraints.tightFor(width: width, height: height);
return _square ? boxConstraints.r : boxConstraints.hw;
}
@override
void updateRenderObject(
BuildContext context, RenderConstrainedBox renderObject) {
renderObject.additionalConstraints = _additionalConstraints;
}
}
... ...
... ... @@ -2,7 +2,10 @@
* Created by 李卓原 on 2018/9/29.
* email: zhuoyuan93@gmail.com
*/
part of flutter_screenutil;
import 'dart:math';
import 'package:flutter/material.dart';
class ScreenUtil {
static const Size defaultSize = Size(360, 690);
... ...
part of flutter_screenutil;
import 'package:flutter/cupertino.dart';
import 'screen_util.dart';
class ScreenUtilInit extends StatelessWidget {
/// A helper widget that initializes [ScreenUtil]
ScreenUtilInit({
const ScreenUtilInit({
required this.builder,
this.designSize = ScreenUtil.defaultSize,
this.splitScreenMode = false,
... ...
part of flutter_screenutil;
import 'dart:math';
import 'package:flutter/material.dart';
import 'screen_util.dart';
extension SizeExtension on num {
///[ScreenUtil.setWidth]
... ... @@ -44,3 +48,48 @@ extension SizeExtension on num {
Widget get verticalSpacingRadius =>
ScreenUtil().setVerticalSpacingRadius(this);
}
extension EdgeInsetsExtension on EdgeInsets {
/// Creates adapt insets using r [SizeExtension].
EdgeInsets get r => copyWith(
top: top.r,
bottom: bottom.r,
right: right.r,
left: left.r,
);
}
extension BorderRaduisExtension on BorderRadius {
/// Creates adapt BorderRadius using r [SizeExtension].
BorderRadius get r => copyWith(
bottomLeft: bottomLeft.r,
bottomRight: bottomLeft.r,
topLeft: topLeft.r,
topRight: topRight.r,
);
}
extension RaduisExtension on Radius {
/// Creates adapt Radius using r [SizeExtension].
Radius get r => this
..x.r
..y.r;
}
extension BoxConstraintsExtension on BoxConstraints {
/// Creates adapt BoxConstraints using r [SizeExtension].
BoxConstraints get r => this.copyWith(
maxHeight: maxHeight.r,
maxWidth: maxWidth.r,
minHeight: minHeight.r,
minWidth: minWidth.r,
);
/// Creates adapt BoxConstraints using h-w [SizeExtension].
BoxConstraints get hw => this.copyWith(
maxHeight: maxHeight.h,
maxWidth: maxWidth.w,
minHeight: minHeight.h,
minWidth: minWidth.w,
);
}
... ...