diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b32280..b485fee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index ca92464..32f4e25 100644 --- a/README.md +++ b/README.md @@ -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. @@ -92,7 +91,6 @@ class _HomePageState extends State { maxWidth: MediaQuery.of(context).size.width, maxHeight: MediaQuery.of(context).size.height), designSize: Size(360, 690), - allowFontScaling: false, orientation: Orientation.portrait); return Scaffold(); } @@ -110,8 +108,6 @@ class _HomePageState extends State { 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 @@ -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( diff --git a/README_CN.md b/README_CN.md index f6fdf37..f4b1113 100644 --- a/README_CN.md +++ b/README_CN.md @@ -40,7 +40,6 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; |属性|类型|默认值|描述| |:---|:---|:---|:---| |designSize|Size|Size(360, 690)|设计稿中设备的尺寸(单位随意,但在使用过程中必须保持一致)| -|allowFontScaling|bool|false|设置字体大小是否根据系统的“字体大小”辅助选项来进行缩放| ### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放 在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致) @@ -103,7 +102,6 @@ class _HomePageState extends State { maxWidth: MediaQuery.of(context).size.width, maxHeight: MediaQuery.of(context).size.height), designSize: Size(360, 690), - allowFontScaling: false, orientation: Orientation.portrait); return Scaffold(); } @@ -119,8 +117,6 @@ class _HomePageState extends State { 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) //设备宽度 @@ -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: diff --git a/README_PT.md b/README_PT.md index 5789c16..4cf66ef 100644 --- a/README_PT.md +++ b/README_PT.md @@ -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). @@ -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( ... ), @@ -112,8 +110,6 @@ class _HomePageState extends State { 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 @@ -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: @@ -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, diff --git a/example/README.md b/example/README.md index d5a08e7..15fc9ce 100644 --- a/example/README.md +++ b/example/README.md @@ -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()); } diff --git a/example/lib/main.dart b/example/lib/main.dart index 5b618eb..cb01334 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -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'), ), ); @@ -41,7 +47,7 @@ class _HomePageState extends State { ), body: SingleChildScrollView( child: Column( - crossAxisAlignment: CrossAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ @@ -82,7 +88,8 @@ class _HomePageState extends State { 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), @@ -106,21 +113,23 @@ class _HomePageState extends State { height: 10.h, ), Text('System font scaling factor:${ScreenUtil().textScaleFactor}'), + SizedBox(height: 5), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ 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, ), ), ], @@ -138,7 +147,8 @@ class _HomePageState extends State { 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'); diff --git a/example/lib/main_zh.dart b/example/lib/main_zh.dart index 6833f74..69c7580 100644 --- a/example/lib/main_zh.dart +++ b/example/lib/main_zh.dart @@ -35,7 +35,6 @@ class _HomePageState extends State { maxWidth: MediaQuery.of(context).size.width, maxHeight: MediaQuery.of(context).size.height), designSize: Size(360, 690), - allowFontScaling: false, orientation: Orientation.portrait); printScreenInformation(); return Scaffold( @@ -67,7 +66,9 @@ class _HomePageState extends State { child: Text( '我的设计稿宽度: 180dp \n' '我的设计稿高度: 200dp', - style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(12))), + style: TextStyle( + color: Colors.white, + fontSize: ScreenUtil().setSp(12))), ), ], ), @@ -105,17 +106,18 @@ class _HomePageState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ 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, ), ), ], @@ -134,8 +136,10 @@ class _HomePageState extends State { 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'); diff --git a/lib/flutter_screenutil.dart b/lib/flutter_screenutil.dart index d21fbd2..238f36e 100644 --- a/lib/flutter_screenutil.dart +++ b/lib/flutter_screenutil.dart @@ -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'; diff --git a/lib/screen_util.dart b/lib/screen_util.dart index 9a89771..2205eb0 100644 --- a/lib/screen_util.dart +++ b/lib/screen_util.dart @@ -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); @@ -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; @@ -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; @@ -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; } diff --git a/lib/screenutil_init.dart b/lib/screenutil_init.dart index aeb2339..ca7a63f 100644 --- a/lib/screenutil_init.dart +++ b/lib/screenutil_init.dart @@ -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); @@ -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) { @@ -28,7 +23,6 @@ class ScreenUtilInit extends StatelessWidget { constraints, orientation: orientation, designSize: designSize, - allowFontScaling: allowFontScaling, ); return builder(); } diff --git a/lib/size_extension.dart b/lib/size_extension.dart index d558541..b5c9dfb 100644 --- a/lib/size_extension.dart +++ b/lib/size_extension.dart @@ -1,4 +1,4 @@ -import 'package:flutter_screenutil/screen_util.dart'; +part of flutter_screenutil; extension SizeExtension on num { ///[ScreenUtil.setWidth] @@ -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 diff --git a/pubspec.yaml b/pubspec.yaml index 482f6a6..cf71ef0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: