flutter_screenutil 4.0.0-beta flutter_screenutil: ^4.0.0-beta copied to clipboard
A flutter plugin for adapting screen and font size.Guaranteed to look good on different models
flutter_screenutil #
A flutter plugin for adapting screen and font size.Let your UI display a reasonable layout on different screen sizes!
Note: This plugin is still under development, and some APIs might not be available yet.
Usage: #
Add dependency: #
Please check the latest version before installation. If there is any problem with the new version, please use the previous version
dependencies:
flutter:
sdk: flutter
# add flutter_screenutil
flutter_screenutil: ^4.0.0-beta
Add the following imports to your Dart code: #
import 'package:flutter_screenutil/flutter_screenutil.dart';
Property #
Property | Type | Default Value | Description |
---|---|---|---|
designSize | Size | Size(1080, 1920) | The size of the ui design draft can be in any unit, but it must be consistent during use |
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.
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
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);
return MaterialApp(
...
);
},
);
}
}
//fill in the screen size of the device in the design
//default value : width : 1080px , height:1920px , allowFontScaling:false
ScreenUtil.init(constraints);
//If the design is based on the size of the iPhone6 (iPhone6 750*1334)
ScreenUtil.init(constraints, designSize: Size(750, 1334));
//If you want to set the font size is scaled according to the system's "font size" assist option
ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: true);
Use: #
API #
Pass the px size of the design draft
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().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
ScreenUtil().screenHeight (dart sdk>=2.6 : 1.sh) //Device height
ScreenUtil().bottomBarHeight //Bottom safe zone distance, suitable for buttons with full screen
ScreenUtil().statusBarHeight //Status bar height , Notch will be higher Unit px
ScreenUtil().textScaleFactor //System font scaling factor
ScreenUtil().scaleWidth //Ratio of actual width dp to ui design
ScreenUtil().scaleHeight //Ratio of actual height dp to ui design
0.2.sw //0.2 times the screen width
0.5.sh //50% of screen height
Adapt screen size:
Pass the px size of the design draft((The unit is the same as the unit at initialization)):
Adapted to screen width: ScreenUtil().setWidth(540)
,
Adapted to screen height: ScreenUtil().setHeight(200)
, In general, the height is best to adapt to the width
If your dart sdk>=2.6, you can use extension functions:
example:
instead of :
Container(
width: ScreenUtil().setWidth(50),
height:ScreenUtil().setHeight(200),
)
you can use it like this:
Container(
width: 50.w,
height:200.h
)
Note
The height can also use setWidth to ensure that it is not deformed(when you want a square)
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.
//for example:
///If you want to display a square:
///rectangle
Container(
width: ScreenUtil().setWidth(375),
height: ScreenUtil().setWidth(200),
),
////If you want to display a square:
Container(
width: ScreenUtil().setWidth(300),
height: 300.w,
),
Adapter font:
//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
//for example:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'My font size is 24px on the design draft and will not change with the system.',
style: TextStyle(
color: Colors.black,
fontSize: ScreenUtil().setSp(24),
)),
Text(
'My font size is 24px on the design draft and will change with the system.',
style: TextStyle(
color: Colors.black,
fontSize: ScreenUtil()
.setSp(24, allowFontScalingSelf: true))),
],
)
Example: #
Effect: #