Committed by
GitHub
Merge pull request #181 from Overman775/init_widget_master
Init widget
Showing
7 changed files
with
92 additions
and
58 deletions
| @@ -18,7 +18,7 @@ | @@ -18,7 +18,7 @@ | ||
| 18 | ### Add dependency: | 18 | ### Add dependency: |
| 19 | Please check the latest version before installation. | 19 | Please check the latest version before installation. |
| 20 | If there is any problem with the new version, please use the previous version | 20 | If there is any problem with the new version, please use the previous version |
| 21 | -``` | 21 | +```yaml |
| 22 | dependencies: | 22 | dependencies: |
| 23 | flutter: | 23 | flutter: |
| 24 | sdk: flutter | 24 | sdk: flutter |
| @@ -26,7 +26,7 @@ dependencies: | @@ -26,7 +26,7 @@ dependencies: | ||
| 26 | flutter_screenutil: ^4.0.0-beta1 | 26 | flutter_screenutil: ^4.0.0-beta1 |
| 27 | ``` | 27 | ``` |
| 28 | ### Add the following imports to your Dart code: | 28 | ### Add the following imports to your Dart code: |
| 29 | -``` | 29 | +```dart |
| 30 | import 'package:flutter_screenutil/flutter_screenutil.dart'; | 30 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| 31 | ``` | 31 | ``` |
| 32 | 32 | ||
| @@ -40,21 +40,19 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | @@ -40,21 +40,19 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | ||
| 40 | ### Initialize and set the fit size and font size to scale according to the system's "font size" accessibility option | 40 | ### Initialize and set the fit size and font size to scale according to the system's "font size" accessibility option |
| 41 | Please set the size of the design draft before use, the width and height of the design draft. | 41 | Please set the size of the design draft before use, the width and height of the design draft. |
| 42 | 42 | ||
| 43 | -``` | 43 | +```dart |
| 44 | void main() => runApp(MyApp()); | 44 | void main() => runApp(MyApp()); |
| 45 | 45 | ||
| 46 | class MyApp extends StatelessWidget { | 46 | class MyApp extends StatelessWidget { |
| 47 | @override | 47 | @override |
| 48 | Widget build(BuildContext context) { | 48 | Widget build(BuildContext context) { |
| 49 | - return LayoutBuilder( | ||
| 50 | - builder: (context, constraints) { | ||
| 51 | //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) | 49 | //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) |
| 52 | - ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); | ||
| 53 | - | ||
| 54 | - return MaterialApp( | 50 | + return ScreenUtilInit( |
| 51 | + designSize: Size(750, 1334), | ||
| 52 | + allowFontScaling: false, | ||
| 53 | + child: MaterialApp( | ||
| 55 | ... | 54 | ... |
| 56 | - ); | ||
| 57 | - }, | 55 | + ), |
| 58 | ); | 56 | ); |
| 59 | } | 57 | } |
| 60 | } | 58 | } |
| @@ -112,14 +110,14 @@ If your dart sdk>=2.6, you can use extension functions: | @@ -112,14 +110,14 @@ If your dart sdk>=2.6, you can use extension functions: | ||
| 112 | example: | 110 | example: |
| 113 | 111 | ||
| 114 | instead of : | 112 | instead of : |
| 115 | -``` | 113 | +```dart |
| 116 | Container( | 114 | Container( |
| 117 | width: ScreenUtil().setWidth(50), | 115 | width: ScreenUtil().setWidth(50), |
| 118 | height:ScreenUtil().setHeight(200), | 116 | height:ScreenUtil().setHeight(200), |
| 119 | ) | 117 | ) |
| 120 | ``` | 118 | ``` |
| 121 | you can use it like this: | 119 | you can use it like this: |
| 122 | -``` | 120 | +```dart |
| 123 | Container( | 121 | Container( |
| 124 | width: 50.w, | 122 | width: 50.w, |
| 125 | height:200.h | 123 | height:200.h |
| @@ -23,7 +23,7 @@ | @@ -23,7 +23,7 @@ | ||
| 23 | 23 | ||
| 24 | 安装之前请查看最新版本 | 24 | 安装之前请查看最新版本 |
| 25 | 新版本如有问题请使用上一版 | 25 | 新版本如有问题请使用上一版 |
| 26 | -``` | 26 | +```yaml |
| 27 | dependencies: | 27 | dependencies: |
| 28 | flutter: | 28 | flutter: |
| 29 | sdk: flutter | 29 | sdk: flutter |
| @@ -31,7 +31,7 @@ dependencies: | @@ -31,7 +31,7 @@ dependencies: | ||
| 31 | flutter_screenutil: ^4.0.0-beta1 | 31 | flutter_screenutil: ^4.0.0-beta1 |
| 32 | ``` | 32 | ``` |
| 33 | ### 在每个使用的地方导入包: | 33 | ### 在每个使用的地方导入包: |
| 34 | -``` | 34 | +```dart |
| 35 | import 'package:flutter_screenutil/flutter_screenutil.dart'; | 35 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| 36 | ``` | 36 | ``` |
| 37 | 37 | ||
| @@ -39,14 +39,14 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | @@ -39,14 +39,14 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | ||
| 39 | 39 | ||
| 40 | |属性|类型|默认值|描述| | 40 | |属性|类型|默认值|描述| |
| 41 | |:---|:---|:---|:---| | 41 | |:---|:---|:---|:---| |
| 42 | -|designSize|Size|Size(1080, 1920)|设计稿中设备的尺寸(单位px)| | 42 | +|designSize|Size|Size(1080, 1920)|设计稿中设备的尺寸(单位随意,但在使用过程中必须保持一致)| |
| 43 | |allowFontScaling|bool|false|设置字体大小是否根据系统的“字体大小”辅助选项来进行缩放| | 43 | |allowFontScaling|bool|false|设置字体大小是否根据系统的“字体大小”辅助选项来进行缩放| |
| 44 | 44 | ||
| 45 | ### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放 | 45 | ### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放 |
| 46 | 在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致) | 46 | 在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致) |
| 47 | 一定要进行初始化(只需设置一次),以保证在每次使用之前设置好了适配尺寸: | 47 | 一定要进行初始化(只需设置一次),以保证在每次使用之前设置好了适配尺寸: |
| 48 | 48 | ||
| 49 | -``` | 49 | +```dart |
| 50 | //填入设计稿中设备的屏幕尺寸 | 50 | //填入设计稿中设备的屏幕尺寸 |
| 51 | 51 | ||
| 52 | void main() => runApp(MyApp()); | 52 | void main() => runApp(MyApp()); |
| @@ -54,18 +54,17 @@ void main() => runApp(MyApp()); | @@ -54,18 +54,17 @@ void main() => runApp(MyApp()); | ||
| 54 | class MyApp extends StatelessWidget { | 54 | class MyApp extends StatelessWidget { |
| 55 | @override | 55 | @override |
| 56 | Widget build(BuildContext context) { | 56 | Widget build(BuildContext context) { |
| 57 | - return LayoutBuilder( | ||
| 58 | - builder: (context, constraints) { | ||
| 59 | //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) | 57 | //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) |
| 60 | - ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); | ||
| 61 | - | ||
| 62 | - return MaterialApp( | 58 | + return ScreenUtilInit( |
| 59 | + designSize: Size(750, 1334), | ||
| 60 | + allowFontScaling: false, | ||
| 61 | + child: MaterialApp( | ||
| 63 | ... | 62 | ... |
| 64 | - ); | ||
| 65 | - }, | 63 | + ), |
| 66 | ); | 64 | ); |
| 67 | } | 65 | } |
| 68 | } | 66 | } |
| 67 | + | ||
| 69 | //默认 width : 1080px , height:1920px , allowFontScaling:false | 68 | //默认 width : 1080px , height:1920px , allowFontScaling:false |
| 70 | ScreenUtil.init(constraints); | 69 | ScreenUtil.init(constraints); |
| 71 | 70 | ||
| @@ -119,7 +118,7 @@ setHeight瘜蜓閬擃漲銝, 雿UI銝撅 | @@ -119,7 +118,7 @@ setHeight瘜蜓閬擃漲銝, 雿UI銝撅 | ||
| 119 | 118 | ||
| 120 | 例如: | 119 | 例如: |
| 121 | 120 | ||
| 122 | -``` | 121 | +```dart |
| 123 | //UI上是长方形: | 122 | //UI上是长方形: |
| 124 | Container( | 123 | Container( |
| 125 | width: ScreenUtil().setWidth(375), | 124 | width: ScreenUtil().setWidth(375), |
| @@ -136,14 +135,14 @@ Container( | @@ -136,14 +135,14 @@ Container( | ||
| 136 | 如果你的dart sdk>=2.6,可以使用扩展函数: | 135 | 如果你的dart sdk>=2.6,可以使用扩展函数: |
| 137 | example: | 136 | example: |
| 138 | 不用这个: | 137 | 不用这个: |
| 139 | -``` | 138 | +```dart |
| 140 | Container( | 139 | Container( |
| 141 | width: ScreenUtil().setWidth(50), | 140 | width: ScreenUtil().setWidth(50), |
| 142 | height:ScreenUtil().setHeight(200), | 141 | height:ScreenUtil().setHeight(200), |
| 143 | ) | 142 | ) |
| 144 | ``` | 143 | ``` |
| 145 | 而是用这个: | 144 | 而是用这个: |
| 146 | -``` | 145 | +```dart |
| 147 | Container( | 146 | Container( |
| 148 | width: 50.w, | 147 | width: 50.w, |
| 149 | height:200.h | 148 | height:200.h |
| @@ -153,7 +152,7 @@ height:200.h | @@ -153,7 +152,7 @@ height:200.h | ||
| 153 | #### 适配字体: | 152 | #### 适配字体: |
| 154 | 传入设计稿的字体大小: | 153 | 传入设计稿的字体大小: |
| 155 | 154 | ||
| 156 | -``` | 155 | +```dart |
| 157 | //传入字体大小(单位和初始化时的单位保持一致),默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling) | 156 | //传入字体大小(单位和初始化时的单位保持一致),默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling) |
| 158 | ScreenUtil().setSp(28) | 157 | ScreenUtil().setSp(28) |
| 159 | 或 | 158 | 或 |
| @@ -184,12 +183,12 @@ Column( | @@ -184,12 +183,12 @@ Column( | ||
| 184 | ) | 183 | ) |
| 185 | ``` | 184 | ``` |
| 186 | 185 | ||
| 187 | -``` | 186 | + |
| 188 | [widget test](https://github.com/OpenFlutter/flutter_screenutil/issues/115) | 187 | [widget test](https://github.com/OpenFlutter/flutter_screenutil/issues/115) |
| 189 | 188 | ||
| 190 | ### 使用示例: | 189 | ### 使用示例: |
| 191 | 190 | ||
| 192 | -[example demo](https://github.com/OpenFlutter/flutter_screenutil/blob/master/example/lib/main_zh.dart) | 191 | +[example demo](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/example/lib/main_zh.dart) |
| 193 | 192 | ||
| 194 | 效果: | 193 | 效果: |
| 195 | 194 |
| @@ -19,16 +19,16 @@ | @@ -19,16 +19,16 @@ | ||
| 19 | ### Adicionando a dependência: | 19 | ### Adicionando a dependência: |
| 20 | Por favor, verifique a última versão antes da instalação. | 20 | Por favor, verifique a última versão antes da instalação. |
| 21 | Se houver algum problema com a nova versão, use a versão anterior | 21 | Se houver algum problema com a nova versão, use a versão anterior |
| 22 | -``` | 22 | +```yaml |
| 23 | dependencies: | 23 | dependencies: |
| 24 | flutter: | 24 | flutter: |
| 25 | sdk: flutter | 25 | sdk: flutter |
| 26 | # add flutter_screenutil | 26 | # add flutter_screenutil |
| 27 | - flutter_screenutil: ^4.0.0-beta1 | 27 | + flutter_screenutil: ^4.0.0-beta |
| 28 | ``` | 28 | ``` |
| 29 | 29 | ||
| 30 | ### Adicione o seguinte import em seu código Dart: | 30 | ### Adicione o seguinte import em seu código Dart: |
| 31 | -``` | 31 | +```dart |
| 32 | import 'package:flutter_screenutil/flutter_screenutil.dart'; | 32 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| 33 | ``` | 33 | ``` |
| 34 | 34 | ||
| @@ -45,12 +45,20 @@ Por favor, defina a largura e altura do protótipo de design antes de usar (em p | @@ -45,12 +45,20 @@ Por favor, defina a largura e altura do protótipo de design antes de usar (em p | ||
| 45 | Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou seja, no arquivo de entrada, defina apenas uma vez) para garantir que o tamanho de ajuste seja o mesmo antes de cada uso: | 45 | Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou seja, no arquivo de entrada, defina apenas uma vez) para garantir que o tamanho de ajuste seja o mesmo antes de cada uso: |
| 46 | 46 | ||
| 47 | ```dart | 47 | ```dart |
| 48 | -//Preencha o tamanho da tela do dispositivo no protótipo de design | ||
| 49 | -void main() { | ||
| 50 | - WidgetsFlutterBinding.ensureInitialized(); | ||
| 51 | - //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) | ||
| 52 | - ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); | ||
| 53 | - runApp(MyApp()); | 48 | +void main() => runApp(MyApp()); |
| 49 | + | ||
| 50 | +class MyApp extends StatelessWidget { | ||
| 51 | + @override | ||
| 52 | + Widget build(BuildContext context) { | ||
| 53 | + //Preencha o tamanho da tela do dispositivo no protótipo de design | ||
| 54 | + return ScreenUtilInit( | ||
| 55 | + designSize: Size(750, 1334), | ||
| 56 | + allowFontScaling: false, | ||
| 57 | + child: MaterialApp( | ||
| 58 | + ... | ||
| 59 | + ), | ||
| 60 | + ); | ||
| 61 | + } | ||
| 54 | } | 62 | } |
| 55 | 63 | ||
| 56 | //Valor padrão: width : 1080px , height:1920px , allowFontScaling:false | 64 | //Valor padrão: width : 1080px , height:1920px , allowFontScaling:false |
| @@ -6,21 +6,18 @@ void main() => runApp(MyApp()); | @@ -6,21 +6,18 @@ void main() => runApp(MyApp()); | ||
| 6 | class MyApp extends StatelessWidget { | 6 | class MyApp extends StatelessWidget { |
| 7 | @override | 7 | @override |
| 8 | Widget build(BuildContext context) { | 8 | Widget build(BuildContext context) { |
| 9 | - return LayoutBuilder( | ||
| 10 | - builder: (context, constraints) { | ||
| 11 | //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) | 9 | //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) |
| 12 | - ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); | ||
| 13 | - | ||
| 14 | - return MaterialApp( | 10 | + return ScreenUtilInit( |
| 11 | + designSize: Size(750, 1334), | ||
| 12 | + allowFontScaling: false, | ||
| 13 | + child: MaterialApp( | ||
| 15 | debugShowCheckedModeBanner: false, | 14 | debugShowCheckedModeBanner: false, |
| 16 | title: 'Flutter_ScreenUtil', | 15 | title: 'Flutter_ScreenUtil', |
| 17 | theme: ThemeData( | 16 | theme: ThemeData( |
| 18 | primarySwatch: Colors.blue, | 17 | primarySwatch: Colors.blue, |
| 19 | - textTheme: TextTheme(button: TextStyle(fontSize: 80.nsp)), | ||
| 20 | ), | 18 | ), |
| 21 | home: HomePage(title: 'FlutterScreenUtil Demo'), | 19 | home: HomePage(title: 'FlutterScreenUtil Demo'), |
| 22 | - ); | ||
| 23 | - }, | 20 | + ), |
| 24 | ); | 21 | ); |
| 25 | } | 22 | } |
| 26 | } | 23 | } |
| @@ -130,10 +127,8 @@ class _HomePageState extends State<HomePage> { | @@ -130,10 +127,8 @@ class _HomePageState extends State<HomePage> { | ||
| 130 | print('Device width dp:${1.sw}dp'); | 127 | print('Device width dp:${1.sw}dp'); |
| 131 | print('Device height dp:${1.sh}dp'); | 128 | print('Device height dp:${1.sh}dp'); |
| 132 | print('Device pixel density:${ScreenUtil().pixelRatio}'); | 129 | print('Device pixel density:${ScreenUtil().pixelRatio}'); |
| 133 | - print( | ||
| 134 | - 'Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}dp'); | ||
| 135 | - print( | ||
| 136 | - 'Status bar height dp:${ScreenUtil().statusBarHeight}dp'); | 130 | + print('Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}dp'); |
| 131 | + print('Status bar height dp:${ScreenUtil().statusBarHeight}dp'); | ||
| 137 | print('Ratio of actual width dp to UI Design:${ScreenUtil().scaleWidth}'); | 132 | print('Ratio of actual width dp to UI Design:${ScreenUtil().scaleWidth}'); |
| 138 | print('Ratio of actual height dp to UI Design:${ScreenUtil().scaleHeight}'); | 133 | print('Ratio of actual height dp to UI Design:${ScreenUtil().scaleHeight}'); |
| 139 | print('System font scaling:${ScreenUtil().textScaleFactor}'); | 134 | print('System font scaling:${ScreenUtil().textScaleFactor}'); |
| @@ -6,21 +6,18 @@ void main() => runApp(MyApp()); | @@ -6,21 +6,18 @@ void main() => runApp(MyApp()); | ||
| 6 | class MyApp extends StatelessWidget { | 6 | class MyApp extends StatelessWidget { |
| 7 | @override | 7 | @override |
| 8 | Widget build(BuildContext context) { | 8 | Widget build(BuildContext context) { |
| 9 | - return LayoutBuilder( | ||
| 10 | - builder: (context, constraints) { | ||
| 11 | //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) | 9 | //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) |
| 12 | - ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); | ||
| 13 | - | ||
| 14 | - return MaterialApp( | 10 | + return ScreenUtilInit( |
| 11 | + designSize: Size(750, 1334), | ||
| 12 | + allowFontScaling: false, | ||
| 13 | + child: MaterialApp( | ||
| 15 | debugShowCheckedModeBanner: false, | 14 | debugShowCheckedModeBanner: false, |
| 16 | title: 'Flutter_ScreenUtil', | 15 | title: 'Flutter_ScreenUtil', |
| 17 | theme: ThemeData( | 16 | theme: ThemeData( |
| 18 | primarySwatch: Colors.blue, | 17 | primarySwatch: Colors.blue, |
| 19 | - textTheme: TextTheme(button: TextStyle(fontSize: 80.nsp)), | ||
| 20 | ), | 18 | ), |
| 21 | home: HomePage(title: 'FlutterScreenUtil Demo'), | 19 | home: HomePage(title: 'FlutterScreenUtil Demo'), |
| 22 | - ); | ||
| 23 | - }, | 20 | + ), |
| 24 | ); | 21 | ); |
| 25 | } | 22 | } |
| 26 | } | 23 | } |
lib/screenutil_init.dart
0 → 100644
| 1 | +import 'package:flutter/widgets.dart'; | ||
| 2 | + | ||
| 3 | +import 'screenutil.dart'; | ||
| 4 | + | ||
| 5 | +class ScreenUtilInit extends StatelessWidget { | ||
| 6 | + /// A helper widget that initializes [ScreenUtil] | ||
| 7 | + const ScreenUtilInit({ | ||
| 8 | + @required this.child, | ||
| 9 | + this.designSize = ScreenUtil.defaultSize, | ||
| 10 | + this.allowFontScaling = false, | ||
| 11 | + Key key, | ||
| 12 | + }) : super(key: key); | ||
| 13 | + | ||
| 14 | + final Widget child; | ||
| 15 | + | ||
| 16 | + /// The [Size] of the device in the design draft, in px | ||
| 17 | + final Size designSize; | ||
| 18 | + | ||
| 19 | + /// Sets whether the font size is scaled according to the system's "font size" assist option | ||
| 20 | + final bool allowFontScaling; | ||
| 21 | + | ||
| 22 | + @override | ||
| 23 | + Widget build(BuildContext context) { | ||
| 24 | + return LayoutBuilder( | ||
| 25 | + builder: (_, BoxConstraints constraints) { | ||
| 26 | + ScreenUtil.init( | ||
| 27 | + constraints, | ||
| 28 | + designSize: designSize, | ||
| 29 | + allowFontScaling: allowFontScaling, | ||
| 30 | + ); | ||
| 31 | + | ||
| 32 | + return child; | ||
| 33 | + }, | ||
| 34 | + ); | ||
| 35 | + } | ||
| 36 | +} |
-
Please register or login to post a comment