Skip to content

Commit

Permalink
4.0.2
Browse files Browse the repository at this point in the history
close #180
  • Loading branch information
李卓原 committed Dec 21, 2020
1 parent e4b43ab commit 959f188
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 33 deletions.
9 changes: 2 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<!--
* @Author: zhuoyuan93@gmail.com
* @Date: 2018-10-16 19:43:03
* @LastEditors: zhuoyuan93@gmail.com
* @LastEditTime: 2020年6月20日 11:20:02
* @Description: Update log
-->
# 4.0.2
- add r(),adapt according to the smaller of width or height

# 4.0.1
- Modify the initialization unit to dp
Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ ScreenUtil.init(constraints, designSize: Size(360, 690), allowFontScaling: true)
```dart
ScreenUtil().setWidth(540) (dart sdk>=2.6 : 540.w) //Adapted to screen width
ScreenUtil().setHeight(200) (dart sdk>=2.6 : 200.h) //Adapted to screen height , under normal circumstances, the height still uses x.w
ScreenUtil().radius(200) (dart sdk>=2.6 : 200.r) //Adapt according to the smaller of width or height
ScreenUtil().setSp(24) (dart sdk>=2.6 : 24.sp) //Adapter font
ScreenUtil().setSp(24, allowFontScalingSelf: true) (dart sdk>=2.6 : 24.ssp) //Adapter font(fonts will scale to respect Text Size accessibility settings)
ScreenUtil().setSp(24, allowFontScalingSelf: false) (dart sdk>=2.6 : 24.nsp) //Adapter font(fonts will not scale to respect Text Size accessibility settings)
Expand Down Expand Up @@ -129,22 +130,30 @@ The height can also use setWidth to ensure that it is not deformed(when you want

setHeight method is mainly adapted in height, you want to control the height and actuality of a screen on the UIUsed when the same is displayed.

Generally speaking, 50.w!=50.h.

```dart
//for example:
///If you want to display a square:
///rectangle
///The UI may show a rectangle:
Container(
width: ScreenUtil().setWidth(375),
height: ScreenUtil().setWidth(200),
width: 375.w,
height: 375.h,
),
////If you want to display a square:
Container(
width: ScreenUtil().setWidth(300),
width: 300.w,
height: 300.w,
),
or
Container(
width: 300.r,
height: 300.r,
),
```


Expand Down
19 changes: 11 additions & 8 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ ScreenUtil.init(constraints, designSize: Size(360,690), allowFontScaling: true);
### API
#### 传入设计稿的dp尺寸
```dart
ScreenUtil().setWidth(540) (sdk>=2.6 : 540.w) //根据屏幕宽度适配尺寸
ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //根据屏幕高度适配尺寸(一般根据宽度适配即可)
ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //适配字体
ScreenUtil().setWidth(540) (sdk>=2.6 : 540.w) //根据屏幕宽度适配尺寸
ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //根据屏幕高度适配尺寸(一般根据宽度适配即可)
ScreenUtil().radius(200) (sdk>=2.6 : 200.r) //根据宽度或高度中的较小者进行调整
ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //适配字体
ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //适配字体(根据系统的“字体大小”辅助选项来进行缩放)
ScreenUtil().setSp(24, allowFontScalingSelf: false) (sdk>=2.6 : 24.nsp) //适配字体(不会根据系统的“字体大小”辅助选项来进行缩放)
Expand All @@ -110,6 +111,8 @@ ScreenUtil.init(constraints, designSize: Size(360,690), allowFontScaling: true);

根据屏幕高度适配 `height: ScreenUtil().setHeight(200)`, 一般来说,控件高度也根据宽度进行适配

一般来说,50.w!=50.h

**注意**

高度也根据setWidth来做适配可以保证不变形(当你想要一个正方形的时候)
Expand All @@ -119,16 +122,16 @@ setHeight方法主要是在高度上进行适配, 在你想控制UI上一屏的
例如:

```dart
//UI上是长方形:
//UI可能显示长方形:
Container(
width: ScreenUtil().setWidth(375),
height: ScreenUtil().setHeight(375),
width: 375.w,
height: 375.h,
),
//如果你想显示一个正方形:
Container(
width: ScreenUtil().setWidth(300),
height: ScreenUtil().setWidth(300),
width: 300.r,
height: 300.r,
),
```

Expand Down
11 changes: 7 additions & 4 deletions README_PT.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ ScreenUtil.init(constraints, designSize: Size(360,690), allowFontScaling: true);
```dart
ScreenUtil().setWidth(540) (sdk>=2.6 : 540.w) //Adapted to screen width
ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //Adapted to screen height
ScreenUtil().radius(200) (dart sdk>=2.6 : 200.r) //Adapt according to the smaller of width or height
ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //Adapter font
ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //Adapter font(fonts will scale to respect Text Size accessibility settings)
ScreenUtil().setSp(24, allowFontScalingSelf: false) (sdk>=2.6 : 24.nsp) //Adapter font(fonts will not scale to respect Text Size accessibility settings)
Expand Down Expand Up @@ -129,19 +130,21 @@ Altura também é adaptada de acordo com o setWidth para garantir que não tenha

O método setHeight é a principal forma de adaptar a altura, se quiser controlar a altura e a realidade de uma tela na UiUsed quando a mesma for exibida.

Generally speaking, 50.w!=50.h.

```dart
//Exemplo:
//Retângulo
Container(
width: ScreenUtil().setWidth(375),
height: ScreenUtil().setHeight(200),
width: 375.w,
height: 200.w,
...
),
////Se quiser exibir um quadrado:
Container(
width: ScreenUtil().setWidth(300),
height: ScreenUtil().setWidth(300),
width: 300.r,
height: 300.r,
),
```
Expand Down
25 changes: 20 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ class _HomePageState extends State<HomePage> {
),
],
),
Container(
padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
width: 100.r,
height: 100.r,
color: Colors.green,
child: Text('I am a square with a side length of 100',
style: TextStyle(
color: Colors.white,
fontSize: ScreenUtil().setSp(12),
),
),
),
Text('Device width:${ScreenUtil().screenWidth}dp'),
Text('Device height:${ScreenUtil().screenHeight}dp'),
Text('Device pixel density:${ScreenUtil().pixelRatio}'),
Expand All @@ -99,11 +111,14 @@ class _HomePageState extends State<HomePage> {
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'My font size is 16sp on the design draft and will not change with the system.',
style: TextStyle(
color: Colors.black,
fontSize: 16.sp,
Container(
height: 32.h,
child: Text(
'My font size is 16sp on the design draft and will not change with the system.',
style: TextStyle(
color: Colors.black,
fontSize: 16.sp,
),
),
),
Text(
Expand Down
13 changes: 13 additions & 0 deletions example/lib/main_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,21 @@ class _HomePageState extends State<HomePage> {
'我的设计稿高度: 200dp',
style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(12))),
),

],
),
Container(
padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
width: 100.r,
height: 100.r,
color: Colors.green,
child: Text('我是正方形,边长是100',
style: TextStyle(
color: Colors.white,
fontSize: ScreenUtil().setSp(12),
),
),
),
Text('设备宽度:${ScreenUtil().screenWidth}dp'),
Text('设备高度:${ScreenUtil().screenHeight}dp'),
Text('设备的像素密度:${ScreenUtil().pixelRatio}'),
Expand Down
18 changes: 14 additions & 4 deletions lib/screenutil.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* email: zhuoyuan93@gmail.com
*/

import 'dart:math';

import 'package:flutter/material.dart';

class ScreenUtil {
Expand Down Expand Up @@ -84,7 +86,7 @@ class ScreenUtil {
/// /// The ratio of actual height to UI design
double get scaleHeight => _screenHeight / uiSize.height;

double get scaleText => scaleWidth;
double get scaleText => min(scaleWidth, scaleHeight);

/// 根据UI设计的设备宽度适配
/// 高度也可以根据这个来做适配可以保证不变形,比如你先要一个正方形的时候.
Expand All @@ -103,14 +105,22 @@ class ScreenUtil {
/// does not match the current style effect, or if there is a difference in shape.
double setHeight(num height) => height * scaleHeight;

///Adapt according to the smaller of width or height
num radius(num r) => r * scaleText;

///字体大小适配方法
///- [fontSize] UI设计上字体的大小,单位px.
///Font size adaptation method
///- [fontSize] The size of the font on the UI design, in px.
///- [allowFontScaling]
double setSp(num fontSize, {bool allowFontScalingSelf}) => allowFontScalingSelf == null
? (allowFontScaling ? (fontSize * scaleText) : (fontSize * scaleText / _textScaleFactor))
: (allowFontScalingSelf ? (fontSize * scaleText) : (fontSize * scaleText / _textScaleFactor));
double setSp(num fontSize, {bool allowFontScalingSelf}) =>
allowFontScalingSelf == null
? (allowFontScaling
? (fontSize * scaleText)
: (fontSize * scaleText / _textScaleFactor))
: (allowFontScalingSelf
? (fontSize * scaleText)
: (fontSize * scaleText / _textScaleFactor));
}

enum UnitType {
Expand Down
3 changes: 3 additions & 0 deletions lib/size_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ extension SizeExtension on num {
///[ScreenUtil.setHeight]
double get h => ScreenUtil().setHeight(this);

///[ScreenUtil.radius]
double get r => ScreenUtil().radius(this);

///[ScreenUtil.setSp]
double get sp => ScreenUtil().setSp(this);

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_screenutil
description: A flutter plugin for adapting screen and font size.Guaranteed to look good on different models
version: 4.0.1
version: 4.0.2
homepage: https://github.com/OpenFlutter/flutter_screenutil

environment:
Expand Down

0 comments on commit 959f188

Please sign in to comment.