{"id":14956363,"url":"https://github.com/openflutter/flutter_screenutil","last_synced_at":"2025-05-13T23:04:01.258Z","repository":{"id":37411733,"uuid":"149636417","full_name":"OpenFlutter/flutter_screenutil","owner":"OpenFlutter","description":"Flutter screen adaptation, font adaptation, get screen information","archived":false,"fork":false,"pushed_at":"2024-08-14T01:21:16.000Z","size":3957,"stargazers_count":3967,"open_issues_count":27,"forks_count":501,"subscribers_count":57,"default_branch":"master","last_synced_at":"2025-04-30T12:17:18.792Z","etag":null,"topics":["dart","flutter","scale","screen","screenutil"],"latest_commit_sha":null,"homepage":"https://pub.dartlang.org/packages/flutter_screenutil","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OpenFlutter.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-20T16:09:28.000Z","updated_at":"2025-04-28T03:44:48.000Z","dependencies_parsed_at":"2023-02-15T15:16:56.478Z","dependency_job_id":"23d0ff74-8858-4ec3-ae30-fe23615df00c","html_url":"https://github.com/OpenFlutter/flutter_screenutil","commit_stats":{"total_commits":537,"total_committers":43,"mean_commits":"12.488372093023257","dds":0.5046554934823091,"last_synced_commit":"6982b1242ffef72c6bce5dad1146f2f000d8eddf"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenFlutter%2Fflutter_screenutil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenFlutter%2Fflutter_screenutil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenFlutter%2Fflutter_screenutil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenFlutter%2Fflutter_screenutil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OpenFlutter","download_url":"https://codeload.github.com/OpenFlutter/flutter_screenutil/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254040427,"owners_count":22004538,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","flutter","scale","screen","screenutil"],"created_at":"2024-09-24T13:12:53.469Z","updated_at":"2025-05-13T23:04:01.233Z","avatar_url":"https://github.com/OpenFlutter.png","language":"Dart","readme":"# flutter_screenutil\n\n[![Flutter Package](https://img.shields.io/pub/v/flutter_screenutil.svg)](https://pub.dev/packages/flutter_screenutil)\n[![Pub Points](https://img.shields.io/pub/points/flutter_screenutil)](https://pub.dev/packages/flutter_screenutil/score)\n[![Popularity](https://img.shields.io/pub/popularity/flutter_screenutil)](https://pub.dev/packages/flutter_screenutil/score)\n[![CodeFactor](https://www.codefactor.io/repository/github/openflutter/flutter_screenutil/badge)](https://www.codefactor.io/repository/github/openflutter/flutter_screenutil)\n\n**A flutter plugin for adapting screen and font size.Let your UI display a reasonable layout on different screen sizes!**\n\n*Note*: This plugin is still under development, and some APIs might not be available yet.\n\n[中文文档](https://github.com/OpenFlutter/flutter_screenutil/blob/master/README_CN.md)  \n\n[README em Português](https://github.com/OpenFlutter/flutter_screenutil/blob/master/README_PT.md)\n\n[github](https://github.com/OpenFlutter/flutter_screenutil)\n\n[Update log](https://github.com/OpenFlutter/flutter_screenutil/blob/master/CHANGELOG.md)\n\n## Usage\n\n### Add dependency\n\nPlease check the latest version before installation.\nIf there is any problem with the new version, please use the previous version\n\n```yaml\ndependencies:\n  flutter:\n    sdk: flutter\n  # add flutter_screenutil\n  flutter_screenutil: ^{latest version}\n```\n\n### Add the following imports to your Dart code\n\n```dart\nimport 'package:flutter_screenutil/flutter_screenutil.dart';\n```\n\n### Properties\n\n| Property          | Type             | Default Value | Description                                                                                                                                   |\n| ----------------- | ---------------- | ------------- |-----------------------------------------------------------------------------------------------------------------------------------------------|\n| designSize        | Size             | Size(360,690) | The size of the device screen in the design draft, in dp                                                                                      |\n| builder           | Function         | null          | Return widget that uses the library in a property (ex: MaterialApp's theme)                                                                   |\n| child             | Widget           | null          | A part of builder that its dependencies/properties don't use the library                                                                      |\n| rebuildFactor     | Function         | _default_     | Function that take old and new screen metrics and returns whether to rebuild or not when changes.                                             |\n| splitScreenMode   | bool             | false         | support for split screen                                                                                                                      |\n| minTextAdapt      | bool             | false         | Whether to adapt the text according to the minimum of width and height                                                                        |\n| context           | BuildContext     | null          | Get physical device data if not provided, by MediaQuery.of(context)                                                                           |\n| fontSizeResolver  | Function         | _default_     | Function that specify how font size should be adapted. Default is that font size scale with width of screen.                                  |\n| responsiveWidgets | Iterable\u003cString\u003e | null          | List/Set of widget names that should be included in rebuilding tree. (See [How flutter_screenutil marks a widget needs build](#rebuild-list)) |\n| excludeWidgets    | Iterable\u003cString\u003e | null          | List/Set of widget names that should be excluded from rebuilding tree.                                                                        |\n| enableScaleWH    | Function | null          | Support enable scale width and height.                                                                                                        |\n| enableScaleText    | Function | null          | Support enable scale text.                                                                                                                    |\n\n\n**Note : You must either provide builder, child or both.**\n\n### Rebuild list\nStarting from version 5.9.0, ScreenUtilInit won't rebuild the whole widget tree, instead it will mark widget needs build only if:\n- Widget is not a flutter widget (widgets are available in [Flutter Docs](https://docs.flutter.dev/reference/widgets))\n- Widget does not start with underscore (`_`)\n- Widget does not declare `SU` mixin\n- `responsiveWidgets` does not contains widget name\n\nIf you have a widget that uses the library and doesn't meet these options you can either add `SU` mixin or add widget name in responsiveWidgets list.\n\n### Initialize and set the fit size and font size to scale according to the system's \"font size\" accessibility option \n\nPlease set the size of the design draft before use, the width and height of the design draft.\n\n#### The first way (You should use it once in your app)\n\n```dart\nvoid main() =\u003e runApp(MyApp());\n\nclass MyApp extends StatelessWidget {\n  const MyApp({Key? key}) : super(key: key);\n\n  @override\n  Widget build(BuildContext context) {\n    //Set the fit size (Find your UI design, look at the dimensions of the device screen and fill it in,unit in dp)\n    return ScreenUtilInit(\n      designSize: const Size(360, 690),\n      minTextAdapt: true,\n      splitScreenMode: true,\n      // Use builder only if you need to use library outside ScreenUtilInit context\n      builder: (_ , child) {\n        return MaterialApp(\n          debugShowCheckedModeBanner: false,\n          title: 'First Method',\n          // You can use the library anywhere in the app even in theme\n          theme: ThemeData(\n            primarySwatch: Colors.blue,\n            textTheme: Typography.englishLike2018.apply(fontSizeFactor: 1.sp),\n          ),\n          home: child,\n        );\n      },\n      child: const HomePage(title: 'First Method'),\n    );\n  }\n}\n```\n\n#### The second way:You need a trick to support font adaptation in the textTheme of app theme\n\n**Hybrid development uses the second way**\n\nnot support this:\n\n```dart\nMaterialApp(\n  ...\n  //To support the following, you need to use the first initialization method\n  theme: ThemeData(\n    textTheme: TextTheme(\n      button: TextStyle(fontSize: 45.sp)\n    ),\n  ),\n)\n```\n\nbut you can do this:\n\n```dart\nvoid main() async {\n  // Add this line\n  await ScreenUtil.ensureScreenSize();\n  runApp(MyApp());\n}\n...\nMaterialApp(\n  ...\n  builder: (ctx, child) {\n    ScreenUtil.init(ctx);\n    return Theme(\n      data: ThemeData(\n        primarySwatch: Colors.blue,\n        textTheme: TextTheme(bodyText2: TextStyle(fontSize: 30.sp)),\n      ),\n      child: HomePage(title: 'FlutterScreenUtil Demo'),\n    );\n  },\n)\n```\n\n```dart\nclass MyApp extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      debugShowCheckedModeBanner: false,\n      title: 'Flutter_ScreenUtil',\n      theme: ThemeData(\n        primarySwatch: Colors.blue,\n      ),\n      home: HomePage(title: 'FlutterScreenUtil Demo'),\n    );\n  }\n}\n\nclass HomePage extends StatefulWidget {\n  const HomePage({Key key, this.title}) : super(key: key);\n\n  final String title;\n\n  @override\n  _HomePageState createState() =\u003e _HomePageState();\n}\n\nclass _HomePageState extends State\u003cHomePage\u003e {\n  @override\n  Widget build(BuildContext context) {\n    //Set the fit size (fill in the screen size of the device in the design) \n    //If the design is based on the size of the 360*690(dp)\n    ScreenUtil.init(context, designSize: const Size(360, 690));\n    ...\n  }\n}\n```\n\n**Note: calling ScreenUtil.init second time, any non-provided parameter will not be replaced with default value. Use ScreenUtil.configure instead**\n\n### API\n\n#### Enable or disable scale\n\n```dart\n  Widget build(BuildContext context) {\n    return ScreenUtilInit(\n      enableScaleWH: ()=\u003efalse,\n      enableScaleText: ()=\u003efalse,\n      //...\n    );\n  }\n```\n\nor\n\n```dart\nScreenUtil.enableScale(enableWH: () =\u003e false, enableText: () =\u003e false);\n```\n\n\n#### Pass the dp size of the design draft\n\n```dart\n    ScreenUtil().setWidth(540)  (dart sdk\u003e=2.6 : 540.w) //Adapted to screen width\n    ScreenUtil().setHeight(200) (dart sdk\u003e=2.6 : 200.h) //Adapted to screen height , under normal circumstances, the height still uses x.w\n    ScreenUtil().radius(200)    (dart sdk\u003e=2.6 : 200.r)    //Adapt according to the smaller of width or height\n    ScreenUtil().setSp(24)      (dart sdk\u003e=2.6 : 24.sp) //Adapter font\n    12.sm   //return min(12,12.sp)\n\n    ScreenUtil().pixelRatio       //Device pixel density\n    ScreenUtil().screenWidth   (dart sdk\u003e=2.6 : 1.sw)    //Device width\n    ScreenUtil().screenHeight  (dart sdk\u003e=2.6 : 1.sh)    //Device height\n    ScreenUtil().bottomBarHeight  //Bottom safe zone distance, suitable for buttons with full screen\n    ScreenUtil().statusBarHeight  //Status bar height , Notch will be higher\n    ScreenUtil().textScaleFactor  //System font scaling factor\n\n    ScreenUtil().scaleWidth //The ratio of actual width to UI design\n    ScreenUtil().scaleHeight //The ratio of actual height to UI design\n\n    ScreenUtil().orientation  //Screen orientation\n    0.2.sw  //0.2 times the screen width\n    0.5.sh  //50% of screen height\n    20.setVerticalSpacing  // SizedBox(height: 20 * scaleHeight)\n    20.horizontalSpace  // SizedBox(height: 20 * scaleWidth)\n    const RPadding.all(8)   // Padding.all(8.r) - take advantage of const key word\n    EdgeInsets.all(10).w    //EdgeInsets.all(10.w)\n    REdgeInsets.all(8)       // EdgeInsets.all(8.r)\n    EdgeInsets.only(left:8,right:8).r // EdgeInsets.only(left:8.r,right:8.r).\n    BoxConstraints(maxWidth: 100, minHeight: 100).w    //BoxConstraints(maxWidth: 100.w, minHeight: 100.w)\n    Radius.circular(16).w          //Radius.circular(16.w)\n    BorderRadius.all(Radius.circular(16)).w  \n```\n\n#### Adapt screen size\n\nPass the dp size of the design draft((The unit is the same as the unit at initialization))：\n\nAdapted to screen width: `ScreenUtil().setWidth(540)`,\n\nAdapted to screen height: `ScreenUtil().setHeight(200)`, In general, the height is best to adapt to the width\n\nIf your dart sdk\u003e=2.6, you can use extension functions:\n\nexample:\n\ninstead of :\n\n```dart\nContainer(\n  width: ScreenUtil().setWidth(50),\n  height:ScreenUtil().setHeight(200),\n)\n```\n\nyou can use it like this:\n\n```dart\nContainer(\n  width: 50.w,\n  height:200.h\n)\n```\n\n#### `Note`\n\nThe height can also use setWidth to ensure that it is not deformed(when you want a square)\n\nThe setHeight method is mainly to adapt to the height, which is used when you want to control the height of a screen on the UI to be the same as the actual display.\n\nGenerally speaking, 50.w!=50.h.\n\n```dart\n//for example:\n\n//If you want to display a rectangle:\nContainer(\n  width: 375.w,\n  height: 375.h,\n),\n            \n//If you want to display a square based on width:\nContainer(\n  width: 300.w,\n  height: 300.w,\n),\n\n//If you want to display a square based on height:\nContainer(\n  width: 300.h,\n  height: 300.h,\n),\n\n//If you want to display a square based on minimum(height, width):\nContainer(\n  width: 300.r,\n  height: 300.r,\n),\n```\n\n#### Adapter font\n\n``` dart\n//Incoming font size(The unit is the same as the unit at initialization)\nScreenUtil().setSp(28) \n28.sp\n\n//for example:\nColumn(\n  crossAxisAlignment: CrossAxisAlignment.start,\n  children: \u003cWidget\u003e[\n    Text(\n      '16sp, will not change with the system.',\n      style: TextStyle(\n        color: Colors.black,\n        fontSize: 16.sp,\n      ),\n      textScaleFactor: 1.0,\n    ),\n    Text(\n      '16sp,if data is not set in MediaQuery,my font size will change with the system.',\n      style: TextStyle(\n        color: Colors.black,\n        fontSize: 16.sp,\n      ),\n    ),\n  ],\n)\n```\n\n#### Setting font does not change with system font size\n\nAPP global:\n\n```dart\nMaterialApp(\n  debugShowCheckedModeBanner: false,\n  title: 'Flutter_ScreenUtil',\n  theme: ThemeData(\n    primarySwatch: Colors.blue,\n  ),\n  builder: (context, widget) {\n    return MediaQuery(\n      ///Setting font does not change with system font size\n      data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),\n      child: widget,\n    );\n  },\n  home: HomePage(title: 'FlutterScreenUtil Demo'),\n),\n```\n\nSpecified Text:\n\n```dart\nText(\"text\", textScaleFactor: 1.0)\n```\n\nSpecified Widget:\n\n```dart\nMediaQuery(\n  // If there is no context available you can wrap [MediaQuery] with [Builder]\n  data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),\n  child: AnyWidget(),\n)\n```\n\n[widget test](https://github.com/OpenFlutter/flutter_screenutil/issues/115)\n\n### Example\n\n[example demo](https://github.com/OpenFlutter/flutter_screenutil/blob/master/example/lib)\n\nTo use second method run: `flutter run --dart-define=method=2`\n\n### Effect\n\n![effect](demo_en.png)\n![tablet effect](demo_tablet_en.png)\n\n\n### Update for Version 5.9.0 (Tests)\nReported as bug in [#515](https://github.com/OpenFlutter/flutter_screenutil/issues/515)\n\n\nIn version 5.9.0, to ensure compatibility and proper functioning of your tests, it is crucial to use the method `tester.pumpAndSettle()`; when conducting widget tests that depend on animations or a settling time to complete their state.\n\nIn the previous version, this step was not strictly necessary. However, to maintain consistency in your tests and avoid unexpected errors, it's strongly recommended incorporating await tester.pumpAndSettle(); in your widget tests if you are using version 5.9.0\n\nExample usage:\n```dart\ntestWidgets('Should ensure widgets settle correctly', (WidgetTester tester) async {\nawait tester.pumpWidget(\n  const MaterialApp(\n    home: ScreenUtilInit(\n      child: MyApp(),\n    ),  \n  ),\n);\n// Insertion of recommended method to prevent failures\nawait tester.pumpAndSettle();\n// Continue with your assertions and tests\n});\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenflutter%2Fflutter_screenutil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenflutter%2Fflutter_screenutil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenflutter%2Fflutter_screenutil/lists"}