Showing
1 changed file
with
144 additions
and
100 deletions
| @@ -4,10 +4,10 @@ | @@ -4,10 +4,10 @@ | ||
| 4 | [](https://pub.dev/packages/flutter_screenutil/score) | 4 | [](https://pub.dev/packages/flutter_screenutil/score) |
| 5 | [](https://pub.dev/packages/flutter_screenutil/score) | 5 | [](https://pub.dev/packages/flutter_screenutil/score) |
| 6 | 6 | ||
| 7 | -**flutter 屏幕适配方案,让你的UI在不同尺寸的屏幕上都能显示合理的布局!** | 7 | +**flutter 屏幕适配方案,用于调整屏幕和字体大小的flutter插件,让你的UI在不同尺寸的屏幕上都能显示合理的布局!** |
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | -*注意*:此插件仍处于开发阶段,某些API可能尚未推出。 | 10 | +*注意*:此插件仍处于开发阶段,某些API可能尚不可用。 |
| 11 | 11 | ||
| 12 | [README of English](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/README.md) | 12 | [README of English](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/README.md) |
| 13 | 13 | ||
| @@ -24,7 +24,7 @@ | @@ -24,7 +24,7 @@ | ||
| 24 | ### 安装依赖: | 24 | ### 安装依赖: |
| 25 | 25 | ||
| 26 | 安装之前请查看最新版本 | 26 | 安装之前请查看最新版本 |
| 27 | -新版本如有问题请使用上一版 | 27 | +新版本如有问题请使用以前的版本 |
| 28 | ```yaml | 28 | ```yaml |
| 29 | dependencies: | 29 | dependencies: |
| 30 | flutter: | 30 | flutter: |
| @@ -51,12 +51,13 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | @@ -51,12 +51,13 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | ||
| 51 | | child | Widget | null | builder的一部分,其依赖项属性不使用该库 | | 51 | | child | Widget | null | builder的一部分,其依赖项属性不使用该库 | |
| 52 | | rebuildFactor | Function | *default* | 返回屏幕指标更改时是否重建。 | | 52 | | rebuildFactor | Function | *default* | 返回屏幕指标更改时是否重建。 | |
| 53 | 53 | ||
| 54 | +注意:builder和child中必须填写至少一项 | ||
| 54 | 55 | ||
| 55 | ### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放 | 56 | ### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放 |
| 56 | 在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致) | 57 | 在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致) |
| 57 | 一定要进行初始化(只需设置一次),以保证在每次使用之前设置好了适配尺寸: | 58 | 一定要进行初始化(只需设置一次),以保证在每次使用之前设置好了适配尺寸: |
| 58 | 59 | ||
| 59 | -#### 方式一: | 60 | +#### 方式一(您必须在app中使用它一次): |
| 60 | ```dart | 61 | ```dart |
| 61 | void main() => runApp(MyApp()); | 62 | void main() => runApp(MyApp()); |
| 62 | 63 | ||
| @@ -65,39 +66,67 @@ class MyApp extends StatelessWidget { | @@ -65,39 +66,67 @@ class MyApp extends StatelessWidget { | ||
| 65 | Widget build(BuildContext context) { | 66 | Widget build(BuildContext context) { |
| 66 | //填入设计稿中设备的屏幕尺寸,单位dp | 67 | //填入设计稿中设备的屏幕尺寸,单位dp |
| 67 | return ScreenUtilInit( | 68 | return ScreenUtilInit( |
| 68 | - designSize: Size(360, 690), | 69 | + designSize: const Size(360, 690), |
| 69 | minTextAdapt: true, | 70 | minTextAdapt: true, |
| 70 | splitScreenMode: true, | 71 | splitScreenMode: true, |
| 71 | - builder: () => | ||
| 72 | - MaterialApp( | ||
| 73 | - //... other code | ||
| 74 | - builder: (context, widget) { | ||
| 75 | - //add this line | ||
| 76 | - ScreenUtil.setContext(context); | ||
| 77 | - return MediaQuery( | ||
| 78 | - //Setting font does not change with system font size | ||
| 79 | - data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), | ||
| 80 | - child: widget!, | ||
| 81 | - ); | ||
| 82 | - }, | ||
| 83 | - theme: ThemeData( | ||
| 84 | - textTheme: TextTheme( | ||
| 85 | - //要支持下面这个需要使用第一种初始化方式 | ||
| 86 | - button: TextStyle(fontSize: 45.sp) | ||
| 87 | - ), | ||
| 88 | - ), | 72 | + builder: (context , child) { |
| 73 | + return MaterialApp( | ||
| 74 | + debugShowCheckedModeBanner: false, | ||
| 75 | + title: 'First Method', | ||
| 76 | + // You can use the library anywhere in the app even in theme | ||
| 77 | + theme: ThemeData( | ||
| 78 | + primarySwatch: Colors.blue, | ||
| 79 | + textTheme: Typography.englishLike2018.apply(fontSizeFactor: 1.sp), | ||
| 89 | ), | 80 | ), |
| 81 | + home: child, | ||
| 82 | + ); | ||
| 83 | + }, | ||
| 84 | + child: const HomePage(title: 'First Method'), | ||
| 90 | ); | 85 | ); |
| 91 | } | 86 | } |
| 92 | } | 87 | } |
| 93 | ``` | 88 | ``` |
| 94 | 89 | ||
| 95 | -#### 方式二: 不支持在MaterialApp的theme的textTheme中使用字体适配 | 90 | +#### 方式二: 你需要一个技巧来支持文字自适应主题 |
| 96 | 91 | ||
| 97 | -**如非必要,建议使用第二种** | ||
| 98 | -**混合开发使用第二种方式** | ||
| 99 | -**ScreenUtil.init只需在home或者根路由(即第一个flutter页面)中调用一次即可。** | 92 | +**混合开发使用方式二** |
| 93 | + | ||
| 94 | +不支持这样做: | ||
| 95 | +```dart | ||
| 96 | +MaterialApp( | ||
| 97 | + ... | ||
| 98 | + //如果你想这样做,你应该选择方式一 | ||
| 99 | + theme: ThemeData( | ||
| 100 | + textTheme: TextTheme( | ||
| 101 | + button: TextStyle(fontSize: 45.sp) | ||
| 102 | + ), | ||
| 103 | + ), | ||
| 104 | +) | ||
| 105 | +``` | ||
| 106 | + | ||
| 107 | +正确的方法应当是这样: | ||
| 108 | +```dart | ||
| 109 | +void main() async { | ||
| 110 | + // Add this line | ||
| 111 | + await ScreenUtil.ensureScreenSize(); | ||
| 112 | + runApp(MyApp()); | ||
| 113 | +} | ||
| 114 | +... | ||
| 115 | +MaterialApp( | ||
| 116 | + ... | ||
| 117 | + builder: (ctx, child) { | ||
| 118 | + ScreenUtil.init(ctx); | ||
| 119 | + return Theme( | ||
| 120 | + data: ThemeData( | ||
| 121 | + primarySwatch: Colors.blue, | ||
| 122 | + textTheme: TextTheme(bodyText2: TextStyle(fontSize: 30.sp)), | ||
| 123 | + ), | ||
| 124 | + child: HomePage(title: 'FlutterScreenUtil Demo'), | ||
| 125 | + ); | ||
| 126 | + }, | ||
| 127 | +) | ||
| 100 | ``` | 128 | ``` |
| 129 | +```dart | ||
| 101 | class MyApp extends StatelessWidget { | 130 | class MyApp extends StatelessWidget { |
| 102 | @override | 131 | @override |
| 103 | Widget build(BuildContext context) { | 132 | Widget build(BuildContext context) { |
| @@ -125,15 +154,8 @@ class _HomePageState extends State<HomePage> { | @@ -125,15 +154,8 @@ class _HomePageState extends State<HomePage> { | ||
| 125 | @override | 154 | @override |
| 126 | Widget build(BuildContext context) { | 155 | Widget build(BuildContext context) { |
| 127 | //设置尺寸(填写设计中设备的屏幕尺寸)如果设计基于360dp * 690dp的屏幕 | 156 | //设置尺寸(填写设计中设备的屏幕尺寸)如果设计基于360dp * 690dp的屏幕 |
| 128 | - ScreenUtil.init( | ||
| 129 | - BoxConstraints( | ||
| 130 | - maxWidth: MediaQuery.of(context).size.width, | ||
| 131 | - maxHeight: MediaQuery.of(context).size.height), | ||
| 132 | - designSize: Size(360, 690), | ||
| 133 | - context: context, | ||
| 134 | - minTextAdapt: true, | ||
| 135 | - orientation: Orientation.portrait); | ||
| 136 | - return Scaffold(); | 157 | + ScreenUtil.init(context, designSize: const Size(360, 690)); |
| 158 | + ... | ||
| 137 | } | 159 | } |
| 138 | } | 160 | } |
| 139 | ``` | 161 | ``` |
| @@ -143,15 +165,15 @@ class _HomePageState extends State<HomePage> { | @@ -143,15 +165,15 @@ class _HomePageState extends State<HomePage> { | ||
| 143 | ### API | 165 | ### API |
| 144 | #### 传入设计稿的dp尺寸 | 166 | #### 传入设计稿的dp尺寸 |
| 145 | ```dart | 167 | ```dart |
| 146 | - ScreenUtil().setWidth(540) (sdk>=2.6 : 540.w) //根据屏幕宽度适配尺寸 | ||
| 147 | - ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //根据屏幕高度适配尺寸(一般根据宽度适配即可) | ||
| 148 | - ScreenUtil().radius(200) (sdk>=2.6 : 200.r) //根据宽度或高度中的较小者进行调整 | ||
| 149 | - ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //适配字体 | 168 | + ScreenUtil().setWidth(540) (dart sdk>=2.6 : 540.w) //根据屏幕宽度适配尺寸 |
| 169 | + ScreenUtil().setHeight(200) (dart sdk>=2.6 : 200.h) //根据屏幕高度适配尺寸(一般根据宽度适配即可) | ||
| 170 | + ScreenUtil().radius(200) (dart sdk>=2.6 : 200.r) //根据宽度或高度中的较小者进行调整 | ||
| 171 | + ScreenUtil().setSp(24) (dart sdk>=2.6 : 24.sp) //适配字体 | ||
| 150 | 12.sm // 取12和12.sp中的最小值 | 172 | 12.sm // 取12和12.sp中的最小值 |
| 151 | 173 | ||
| 152 | ScreenUtil.pixelRatio //设备的像素密度 | 174 | ScreenUtil.pixelRatio //设备的像素密度 |
| 153 | - ScreenUtil.screenWidth (sdk>=2.6 : 1.sw) //设备宽度 | ||
| 154 | - ScreenUtil.screenHeight (sdk>=2.6 : 1.sh) //设备高度 | 175 | + ScreenUtil.screenWidth (dart sdk>=2.6 : 1.sw) //设备宽度 |
| 176 | + ScreenUtil.screenHeight (dart sdk>=2.6 : 1.sh) //设备高度 | ||
| 155 | ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的 | 177 | ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的 |
| 156 | ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高 | 178 | ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高 |
| 157 | ScreenUtil.textScaleFactor //系统字体缩放比例 | 179 | ScreenUtil.textScaleFactor //系统字体缩放比例 |
| @@ -165,7 +187,7 @@ class _HomePageState extends State<HomePage> { | @@ -165,7 +187,7 @@ class _HomePageState extends State<HomePage> { | ||
| 165 | 0.5.sh //屏幕高度的50% | 187 | 0.5.sh //屏幕高度的50% |
| 166 | 20.setVerticalSpacing // SizedBox(height: 20 * scaleHeight) | 188 | 20.setVerticalSpacing // SizedBox(height: 20 * scaleHeight) |
| 167 | 20.horizontalSpace // SizedBox(height: 20 * scaleWidth) | 189 | 20.horizontalSpace // SizedBox(height: 20 * scaleWidth) |
| 168 | - const RPadding.all(8) // Padding.all(8.r) - take advantage of const key word | 190 | + const RPadding.all(8) // Padding.all(8.r) - 获取到const的优点 |
| 169 | REdgeInsts.all(8) // EdgeInsets.all(8.r) | 191 | REdgeInsts.all(8) // EdgeInsets.all(8.r) |
| 170 | EdgeInsets.only(left:8,right:8).r // EdgeInsets.only(left:8.r,right:8.r). | 192 | EdgeInsets.only(left:8,right:8).r // EdgeInsets.only(left:8.r,right:8.r). |
| 171 | 193 | ||
| @@ -180,85 +202,96 @@ class _HomePageState extends State<HomePage> { | @@ -180,85 +202,96 @@ class _HomePageState extends State<HomePage> { | ||
| 180 | 202 | ||
| 181 | 根据屏幕高度适配 `height: ScreenUtil().setHeight(200)`, 一般来说,控件高度也根据宽度进行适配 | 203 | 根据屏幕高度适配 `height: ScreenUtil().setHeight(200)`, 一般来说,控件高度也根据宽度进行适配 |
| 182 | 204 | ||
| 183 | -一般来说,50.w!=50.h | 205 | +如果您的 dart sdk>=2.6,则可以使用一些特殊的函数: |
| 206 | + | ||
| 207 | +例子: | ||
| 208 | + | ||
| 209 | +你不应当这样做: | ||
| 210 | +```dart | ||
| 211 | +Container( | ||
| 212 | + width: ScreenUtil().setWidth(50), | ||
| 213 | + height:ScreenUtil().setHeight(200), | ||
| 214 | +) | ||
| 215 | +``` | ||
| 216 | +正确的方法应当是这样使用: | ||
| 217 | +```dart | ||
| 218 | +Container( | ||
| 219 | + width: 50.w, | ||
| 220 | + height:200.h | ||
| 221 | +) | ||
| 222 | +``` | ||
| 184 | 223 | ||
| 185 | -**注意** | 224 | +`注意` |
| 186 | 225 | ||
| 187 | 高度也根据setWidth来做适配可以保证不变形(当你想要一个正方形的时候) | 226 | 高度也根据setWidth来做适配可以保证不变形(当你想要一个正方形的时候) |
| 188 | 227 | ||
| 189 | setHeight方法主要是在高度上进行适配, 在你想控制UI上一屏的高度与实际中显示一样时使用. | 228 | setHeight方法主要是在高度上进行适配, 在你想控制UI上一屏的高度与实际中显示一样时使用. |
| 190 | 229 | ||
| 230 | +一般来说,50.w!=50.h | ||
| 231 | + | ||
| 191 | 例如: | 232 | 例如: |
| 192 | 233 | ||
| 193 | ```dart | 234 | ```dart |
| 194 | -//UI可能显示长方形: | 235 | +//如果你想显示一个矩形: |
| 195 | Container( | 236 | Container( |
| 196 | - width: 375.w, | ||
| 197 | - height: 375.h, | ||
| 198 | - ), | ||
| 199 | - | ||
| 200 | -//如果你想显示一个正方形: | 237 | + width: 375.w, |
| 238 | + height: 375.h, | ||
| 239 | +), | ||
| 240 | + | ||
| 241 | +//如果你想基于宽显示一个正方形: | ||
| 201 | Container( | 242 | Container( |
| 202 | - width: 300.r, | ||
| 203 | - height: 300.r, | ||
| 204 | - ), | ||
| 205 | -``` | 243 | + width: 300.w, |
| 244 | + height: 300.w, | ||
| 245 | +), | ||
| 206 | 246 | ||
| 207 | -如果你的dart sdk>=2.6,可以使用扩展函数: | ||
| 208 | -example: | ||
| 209 | -不用这个: | ||
| 210 | -```dart | 247 | +//如果你想基于高显示一个正方形: |
| 211 | Container( | 248 | Container( |
| 212 | -width: ScreenUtil().setWidth(50), | ||
| 213 | -height:ScreenUtil().setHeight(200), | ||
| 214 | -) | ||
| 215 | -``` | ||
| 216 | -而是用这个: | ||
| 217 | -```dart | 249 | + width: 300.h, |
| 250 | + height: 300.h, | ||
| 251 | +), | ||
| 252 | + | ||
| 253 | +//如果你想基于高或宽中的较小值显示一个正方形: | ||
| 218 | Container( | 254 | Container( |
| 219 | -width: 50.w, | ||
| 220 | -height:200.h | ||
| 221 | -) | 255 | + width: 300.r, |
| 256 | + height: 300.r, | ||
| 257 | +), | ||
| 222 | ``` | 258 | ``` |
| 223 | 259 | ||
| 224 | -#### 适配字体: | ||
| 225 | -传入设计稿的字体大小: | 260 | +**适配字体** |
| 226 | 261 | ||
| 227 | -```dart | ||
| 228 | -//传入字体大小(单位和初始化时的单位保持一致) | ||
| 229 | -ScreenUtil().setSp(28) | ||
| 230 | -或 | ||
| 231 | -28.sp (dart sdk>=2.6) | ||
| 232 | - | ||
| 233 | -//for example: | 262 | +```dart |
| 263 | +//输入字体大小(单位与初始化时的单位相同) | ||
| 264 | +ScreenUtil().setSp(28) | ||
| 265 | +28.sp | ||
| 234 | 266 | ||
| 267 | +//例子: | ||
| 235 | Column( | 268 | Column( |
| 236 | - crossAxisAlignment: CrossAxisAlignment.start, | ||
| 237 | - children: <Widget>[ | ||
| 238 | - Text( | ||
| 239 | - '我的文字大小在设计稿上是16dp,因为设置了`textScaleFactor`,所以不会随着系统的文字缩放比例变化', | ||
| 240 | - style: TextStyle( | ||
| 241 | - color: Colors.black, | ||
| 242 | - fontSize: 16.sp, | ||
| 243 | - ), | ||
| 244 | - textScaleFactor: 1.0, | ||
| 245 | - ), | ||
| 246 | - Text( | ||
| 247 | - '我的文字大小在设计稿上是16dp,会随着系统的文字缩放比例变化', | ||
| 248 | - style: TextStyle( | ||
| 249 | - color: Colors.black, | ||
| 250 | - fontSize: 16.sp, | ||
| 251 | - ), | ||
| 252 | - ), | ||
| 253 | - ], | ||
| 254 | - ) | 269 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 270 | + children: <Widget>[ | ||
| 271 | + Text( | ||
| 272 | + '16sp, 因为设置了`textScaleFactor`,不会随系统变化.', | ||
| 273 | + style: TextStyle( | ||
| 274 | + color: Colors.black, | ||
| 275 | + fontSize: 16.sp, | ||
| 276 | + ), | ||
| 277 | + textScaleFactor: 1.0, | ||
| 278 | + ), | ||
| 279 | + Text( | ||
| 280 | + '16sp,如果未设置,我的字体大小将随系统而变化.', | ||
| 281 | + style: TextStyle( | ||
| 282 | + color: Colors.black, | ||
| 283 | + fontSize: 16.sp, | ||
| 284 | + ), | ||
| 285 | + ), | ||
| 286 | + ], | ||
| 287 | +) | ||
| 255 | ``` | 288 | ``` |
| 256 | 289 | ||
| 257 | #### 设置字体不随系统字体大小进行改变 | 290 | #### 设置字体不随系统字体大小进行改变 |
| 258 | 291 | ||
| 259 | APP全局: | 292 | APP全局: |
| 260 | ```dart | 293 | ```dart |
| 261 | - MaterialApp( | 294 | + MaterialApp( |
| 262 | debugShowCheckedModeBanner: false, | 295 | debugShowCheckedModeBanner: false, |
| 263 | title: 'Flutter_ScreenUtil', | 296 | title: 'Flutter_ScreenUtil', |
| 264 | theme: ThemeData( | 297 | theme: ThemeData( |
| @@ -279,12 +312,23 @@ APP全局: | @@ -279,12 +312,23 @@ APP全局: | ||
| 279 | Text("text", textScaleFactor: 1.0) | 312 | Text("text", textScaleFactor: 1.0) |
| 280 | ``` | 313 | ``` |
| 281 | 314 | ||
| 282 | -[widget test](https://github.com/OpenFlutter/flutter_screenutil/issues/115) | 315 | +指定的小部件: |
| 316 | +```dart | ||
| 317 | +MediaQuery( | ||
| 318 | + // 如果这里context不可用,你可以新建一个 [Builder] 将 [MediaQuery] 放入其中 | ||
| 319 | + data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), | ||
| 320 | + child: AnyWidget(), | ||
| 321 | +) | ||
| 322 | +``` | ||
| 323 | + | ||
| 324 | +[小部件测试](https://github.com/OpenFlutter/flutter_screenutil/issues/115) | ||
| 283 | 325 | ||
| 284 | ### 使用示例: | 326 | ### 使用示例: |
| 285 | 327 | ||
| 286 | -[example demo](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/example/lib/main_zh.dart) | ||
| 287 | - | 328 | +[示例演示](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/example/lib/main_zh.dart) |
| 329 | + | ||
| 330 | +要使用第二种方法,请运行:`flutter run --dart-define=method=2` | ||
| 331 | + | ||
| 288 | 效果: | 332 | 效果: |
| 289 | 333 | ||
| 290 |  | 334 |  |
-
Please register or login to post a comment