Skip to content

Commit

Permalink
5.0.0,null-safety最终版本.
Browse files Browse the repository at this point in the history
更新内容参考changelog.md
  • Loading branch information
李卓原 committed Apr 9, 2021
1 parent 20ac1de commit 7032217
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 75 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 5.0.0
-Breaking change. Use a new way to set font scaling
-Deprecated ssp and nsp

# 5.0.0-nullsafety.11
- revert 5.0.0-nullsafety.10
- fix #230
Expand Down
15 changes: 1 addition & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
|Property|Type|Default Value|Description|
|:---|:---|:---|:---|
|designSize|Size|Size(360, 690)|The size of the device in the design draft, in dp|
|allowFontScaling|bool|false|Sets whether the font size is scaled according to the system's "font size" assist option|

### Initialize and set the fit size and font size to scale according to the system's "font size" accessibility option
Please set the size of the design draft before use, the width and height of the design draft.
Expand Down Expand Up @@ -92,7 +91,6 @@ class _HomePageState extends State<HomePage> {
maxWidth: MediaQuery.of(context).size.width,
maxHeight: MediaQuery.of(context).size.height),
designSize: Size(360, 690),
allowFontScaling: false,
orientation: Orientation.portrait);
return Scaffold();
}
Expand All @@ -110,8 +108,6 @@ class _HomePageState extends State<HomePage> {
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)
ScreenUtil().pixelRatio //Device pixel density
ScreenUtil().screenWidth (dart sdk>=2.6 : 1.sw) //Device width
Expand Down Expand Up @@ -192,16 +188,7 @@ Container(
//Incoming font size(The unit is the same as the unit at initialization), fonts will not scale to respect Text Size accessibility settings
//(AllowallowFontScaling when initializing ScreenUtil)
ScreenUtil().setSp(28)
28.sp
//Incoming font size,the unit is pixel,fonts will scale to respect Text Size accessibility settings
//(If somewhere follow the global allowFontScaling setting)
ScreenUtil().setSp(24, allowFontScalingSelf: true)
28.ssp
//(If somewhere does not follow the global allowFontScaling setting)
ScreenUtil().setSp(24, allowFontScalingSelf: false)
28.nsp
28.sp
//for example:
Column(
Expand Down
10 changes: 0 additions & 10 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
|属性|类型|默认值|描述|
|:---|:---|:---|:---|
|designSize|Size|Size(360, 690)|设计稿中设备的尺寸(单位随意,但在使用过程中必须保持一致)|
|allowFontScaling|bool|false|设置字体大小是否根据系统的“字体大小”辅助选项来进行缩放|

### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放
在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致)
Expand Down Expand Up @@ -103,7 +102,6 @@ class _HomePageState extends State<HomePage> {
maxWidth: MediaQuery.of(context).size.width,
maxHeight: MediaQuery.of(context).size.height),
designSize: Size(360, 690),
allowFontScaling: false,
orientation: Orientation.portrait);
return Scaffold();
}
Expand All @@ -119,8 +117,6 @@ class _HomePageState extends State<HomePage> {
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) //适配字体(不会根据系统的“字体大小”辅助选项来进行缩放)
ScreenUtil.pixelRatio //设备的像素密度
ScreenUtil.screenWidth (sdk>=2.6 : 1.sw) //设备宽度
Expand Down Expand Up @@ -196,12 +192,6 @@ height:200.h
ScreenUtil().setSp(28)
28.sp (dart sdk>=2.6)
//传入字体大小,根据系统的“字体大小”辅助选项来进行缩放(如果某个地方不遵循全局的allowFontScaling设置)
ScreenUtil().setSp(24, allowFontScalingSelf: true)
24.ssp (dart sdk>=2.6)
//for example:
Expand Down
9 changes: 2 additions & 7 deletions README_PT.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
|:---|:---|:---|:---|
|width|double|360dp|A largura do dispositivo no protótipo de design, em dp|
|height|double|690dp|A altura do dispositivo no protótipo de design, em dp|
|allowFontScaling|bool|false|Defina caso o tamanho da fonte seja dimensionado de acordo com a opção "tamanho de fonte" na acessibilidade do sistema|

### Inicialize e defina o tamanho de ajuste e tamanho da fonte para dimensionar de acordo com a opção "tamanho de fonte" na acessibilidade do sistema
Por favor, defina a largura e altura do protótipo de design antes de usar (em pixels).
Expand All @@ -54,7 +53,6 @@ class MyApp extends StatelessWidget {
//Set the fit size (fill in the screen size of the device in the design,in dp)
return ScreenUtilInit(
designSize: Size(360, 690),
allowFontScaling: false,
builder: () => MaterialApp(
...
),
Expand Down Expand Up @@ -112,8 +110,6 @@ class _HomePageState extends State<HomePage> {
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)
ScreenUtil.pixelRatio //Device pixel density
ScreenUtil.screenWidth (sdk>=2.6 : 1.sw) //Device width
Expand Down Expand Up @@ -189,8 +185,7 @@ Container(
ScreenUtil().setSp(28)
//Tamanho da fonte informado,em pixels,a fonte irá dimensionar respeitando a opção "Tamanho de Fonte" nas configurações de acessibilidade
//(Se em algum lugar não seguir a configuração global da propriedade allowFontScaling)
ScreenUtil().setSp(24, allowFontScalingSelf: true)
ScreenUtil().setSp(24)
//Exemplo:
Expand Down Expand Up @@ -240,7 +235,7 @@ class MyApp extends StatelessWidget {
return LayoutBuilder(
builder: (context, constraints) {
//Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334)
ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false);
ScreenUtil.init(constraints, designSize: Size(750, 1334));
return MaterialApp(
debugShowCheckedModeBanner: false,
Expand Down
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
void main() {
WidgetsFlutterBinding.ensureInitialized();
//Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334)
ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: false);
ScreenUtil.init(designSize: Size(750, 1334));
runApp(MyApp());
}
Expand Down
26 changes: 18 additions & 8 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ class MyApp extends StatelessWidget {
//Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334)
return ScreenUtilInit(
designSize: Size(360, 690),
allowFontScaling: false,
builder: () => MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter_ScreenUtil',
theme: ThemeData(
primarySwatch: Colors.blue,
),
builder: (context, widget) {
return MediaQuery(
//Setting font does not change with system font size
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
child: widget,
);
},
home: HomePage(title: 'FlutterScreenUtil Demo'),
),
);
Expand All @@ -41,7 +47,7 @@ class _HomePageState extends State<HomePage> {
),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Expand Down Expand Up @@ -82,7 +88,8 @@ class _HomePageState extends State<HomePage> {
width: 100.r,
height: 100.r,
color: Colors.green,
child: Text('I am a square with a side length of 100',
child: Text(
'I am a square with a side length of 100',
style: TextStyle(
color: Colors.white,
fontSize: ScreenUtil().setSp(12),
Expand All @@ -106,21 +113,23 @@ class _HomePageState extends State<HomePage> {
height: 10.h,
),
Text('System font scaling factor:${ScreenUtil().textScaleFactor}'),
SizedBox(height: 5),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'My font size is 16sp on the design draft and will not change with the system.',
'16sp, will not change with the system.',
style: TextStyle(
color: Colors.black,
fontSize: 16.nsp,
fontSize: 16.sp,
),
textScaleFactor: 1.0,
),
Text(
'My font size is 16sp on the design draft and will change with the system.',
'16sp,if data is not set in MediaQuery,my font size will change with the system.',
style: TextStyle(
color: Colors.black,
fontSize: 16.ssp,
fontSize: 16.sp,
),
),
],
Expand All @@ -138,7 +147,8 @@ class _HomePageState extends State<HomePage> {
print('Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}dp');
print('Status bar height dp:${ScreenUtil().statusBarHeight}dp');
print('The ratio of actual width to UI design:${ScreenUtil().scaleWidth}');
print('The ratio of actual height to UI design:${ScreenUtil().scaleHeight}');
print(
'The ratio of actual height to UI design:${ScreenUtil().scaleHeight}');
print('System font scaling:${ScreenUtil().textScaleFactor}');
print('0.5 times the screen width:${0.5.sw}dp');
print('0.5 times the screen height:${0.5.sh}dp');
Expand Down
16 changes: 10 additions & 6 deletions example/lib/main_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class _HomePageState extends State<HomePage> {
maxWidth: MediaQuery.of(context).size.width,
maxHeight: MediaQuery.of(context).size.height),
designSize: Size(360, 690),
allowFontScaling: false,
orientation: Orientation.portrait);
printScreenInformation();
return Scaffold(
Expand Down Expand Up @@ -67,7 +66,9 @@ class _HomePageState extends State<HomePage> {
child: Text(
'我的设计稿宽度: 180dp \n'
'我的设计稿高度: 200dp',
style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(12))),
style: TextStyle(
color: Colors.white,
fontSize: ScreenUtil().setSp(12))),
),
],
),
Expand Down Expand Up @@ -105,17 +106,18 @@ class _HomePageState extends State<HomePage> {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'我的文字大小在设计稿上是16dp,不会随着系统的文字缩放比例变化',
'我的文字大小在设计稿上是16dp,因为设置了`textScaleFactor`,所以不会随着系统的文字缩放比例变化',
style: TextStyle(
color: Colors.black,
fontSize: 16.sp,
),
textScaleFactor: 1.0,
),
Text(
'我的文字大小在设计稿上是16dp,会随着系统的文字缩放比例变化',
style: TextStyle(
color: Colors.black,
fontSize: 16.ssp,
fontSize: 16.sp,
),
),
],
Expand All @@ -134,8 +136,10 @@ class _HomePageState extends State<HomePage> {
print('状态栏高度:${ScreenUtil().statusBarHeight}dp');
print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}');
print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}');
print('宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}');
print('高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}');
print(
'宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}');
print(
'高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}');
print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}');
print('屏幕宽度的0.5:${0.5.sw}dp');
print('屏幕高度的0.5:${0.5.sh}dp');
Expand Down
13 changes: 10 additions & 3 deletions lib/flutter_screenutil.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

library flutter_screenutil;

export 'size_extension.dart';
export 'screen_util.dart';
export 'screenutil_init.dart';
import 'dart:math';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';

part 'screen_util.dart';

part 'screenutil_init.dart';

part 'size_extension.dart';
17 changes: 2 additions & 15 deletions lib/screen_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
* Created by 李卓原 on 2018/9/29.
* email: zhuoyuan93@gmail.com
*/

import 'dart:math';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
part of flutter_screenutil;

class ScreenUtil {
static const Size defaultSize = Size(360, 690);
Expand All @@ -16,10 +12,6 @@ class ScreenUtil {
/// Size of the phone in UI Design , dp
late Size uiSize;

/// 控制字体是否要根据系统的“字体大小”辅助选项来进行缩放。默认值为false。
/// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false.
late bool allowFontScaling;

///屏幕方向
late Orientation _orientation;

Expand All @@ -40,11 +32,9 @@ class ScreenUtil {
BoxConstraints constraints, {
Orientation orientation = Orientation.portrait,
Size designSize = defaultSize,
bool allowFontScaling = false,
}) {
_instance = ScreenUtil._()
..uiSize = designSize
..allowFontScaling = allowFontScaling
.._orientation = orientation
.._screenWidth = constraints.maxWidth
.._screenHeight = constraints.maxHeight;
Expand Down Expand Up @@ -118,8 +108,5 @@ class ScreenUtil {
///- [fontSize] UI设计上字体的大小,单位dp.
///Font size adaptation method
///- [fontSize] The size of the font on the UI design, in dp.
///- [allowFontScaling]
double setSp(num fontSize, {bool? allowFontScalingSelf}) => allowFontScalingSelf == null
? (allowFontScaling ? (fontSize * scaleText) * _textScaleFactor : (fontSize * scaleText))
: (allowFontScalingSelf ? (fontSize * scaleText) * _textScaleFactor : (fontSize * scaleText));
double setSp(num fontSize) => fontSize * scaleText;
}
8 changes: 1 addition & 7 deletions lib/screenutil_init.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screen_util.dart';
part of flutter_screenutil;

class ScreenUtilInit extends StatelessWidget {
/// A helper widget that initializes [ScreenUtil]
ScreenUtilInit({
required this.builder,
this.designSize = ScreenUtil.defaultSize,
this.allowFontScaling = false,
Key? key,
}) : super(key: key);

Expand All @@ -15,9 +13,6 @@ class ScreenUtilInit extends StatelessWidget {
/// The [Size] of the device in the design draft, in dp
final Size designSize;

/// Sets whether the font size is scaled according to the system's "font size" assist option
final bool allowFontScaling;

@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (_, BoxConstraints constraints) {
Expand All @@ -28,7 +23,6 @@ class ScreenUtilInit extends StatelessWidget {
constraints,
orientation: orientation,
designSize: designSize,
allowFontScaling: allowFontScaling,
);
return builder();
}
Expand Down
9 changes: 6 additions & 3 deletions lib/size_extension.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:flutter_screenutil/screen_util.dart';
part of flutter_screenutil;

extension SizeExtension on num {
///[ScreenUtil.setWidth]
Expand All @@ -14,10 +14,13 @@ extension SizeExtension on num {
double get sp => ScreenUtil().setSp(this);

///[ScreenUtil.setSp]
double get ssp => ScreenUtil().setSp(this, allowFontScalingSelf: true);
@Deprecated('please use [sp]')
double get ssp => ScreenUtil().setSp(this);

///[ScreenUtil.setSp]
double get nsp => ScreenUtil().setSp(this, allowFontScalingSelf: false);
@Deprecated(
'please use [sp] , and set textScaleFactor: 1.0 , for example: Text("text", textScaleFactor: 1.0)')
double get nsp => ScreenUtil().setSp(this);

///屏幕宽度的倍数
///Multiple of screen width
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: 5.0.0-nullsafety.11
version: 5.0.0
homepage: https://github.com/OpenFlutter/flutter_screenutil

environment:
Expand Down

0 comments on commit 7032217

Please sign in to comment.