Skip to content

Commit

Permalink
Merge pull request OpenFlutter#535 from joker-fu/main
Browse files Browse the repository at this point in the history
support for disabling scaling. Fixes OpenFlutter#534 OpenFlutter#459
  • Loading branch information
Mounir-Bouaiche authored May 31, 2024
2 parents 1c355fc + aeca735 commit 3ed9313
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
### Properties

| Property | Type | Default Value | Description |
| ----------------- | ---------------- | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| ----------------- | ---------------- | ------------- |-----------------------------------------------------------------------------------------------------------------------------------------------|
| designSize | Size | Size(360,690) | The size of the device screen in the design draft, in dp |
| builder | Function | null | Return widget that uses the library in a property (ex: MaterialApp's theme) |
| child | Widget | null | A part of builder that its dependencies/properties don't use the library |
Expand All @@ -52,6 +52,8 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
| fontSizeResolver | Function | _default_ | Function that specify how font size should be adapted. Default is that font size scale with width of screen. |
| responsiveWidgets | Iterable<String> | null | List/Set of widget names that should be included in rebuilding tree. (See [How flutter_screenutil marks a widget needs build](#rebuild-list)) |
| excludeWidgets | Iterable<String> | null | List/Set of widget names that should be excluded from rebuilding tree. |
| enableScaleWH | Function | null | Support enable scale width and height. |
| enableScaleText | Function | null | Support enable scale text. |


**Note : You must either provide builder, child or both.**
Expand Down Expand Up @@ -184,6 +186,25 @@ class _HomePageState extends State<HomePage> {

### API

#### Enable or disable scale

```dart
Widget build(BuildContext context) {
return ScreenUtilInit(
enableScaleWH: ()=>false,
enableScaleText: ()=>false,
//...
);
}
```

or

```dart
ScreenUtil.enableScale(enableWH: () => false, enableText: () => false);
```


#### Pass the dp size of the design draft

```dart
Expand Down
20 changes: 17 additions & 3 deletions lib/src/screen_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class ScreenUtil {
static const Size defaultSize = Size(360, 690);
static ScreenUtil _instance = ScreenUtil._();

static bool Function() _enableScaleWH = () => true;
static bool Function() _enableScaleText = () => true;


/// UI设计中手机尺寸 , dp
/// Size of the phone in UI Design , dp
late Size _uiSize;
Expand All @@ -32,6 +36,16 @@ class ScreenUtil {

factory ScreenUtil() => _instance;

/// Enable scale
///
/// if the enableWH return false, the width and the height scale ratio will be 1
/// if the enableText return false, the text scale ratio will be 1
///
static void enableScale({bool Function()? enableWH, bool Function()? enableText}) {
_enableScaleWH = enableWH ?? () => true;
_enableScaleText = enableText ?? () => true;
}

/// Manually wait for window size to be initialized
///
/// `Recommended` to use before you need access window size
Expand Down Expand Up @@ -199,15 +213,15 @@ class ScreenUtil {

/// 实际尺寸与UI设计的比例
/// The ratio of actual width to UI design
double get scaleWidth => screenWidth / _uiSize.width;
double get scaleWidth => !_enableScaleWH() ? 1 : screenWidth / _uiSize.width;

/// The ratio of actual height to UI design
double get scaleHeight =>
(_splitScreenMode ? max(screenHeight, 700) : screenHeight) /
!_enableScaleWH() ? 1 : (_splitScreenMode ? max(screenHeight, 700) : screenHeight) /
_uiSize.height;

double get scaleText =>
_minTextAdapt ? min(scaleWidth, scaleHeight) : scaleWidth;
!_enableScaleText() ? 1 : (_minTextAdapt ? min(scaleWidth, scaleHeight) : scaleWidth);

/// 根据UI设计的设备宽度适配
/// 高度也可以根据这个来做适配可以保证不变形,比如你想要一个正方形的时候.
Expand Down
10 changes: 8 additions & 2 deletions lib/src/screenutil_init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class ScreenUtilInit extends StatefulWidget {
this.minTextAdapt = false,
this.useInheritedMediaQuery = false,
this.ensureScreenSize = false,
this.enableScaleWH,
this.enableScaleText,
this.responsiveWidgets,
this.excludeWidgets,
this.fontSizeResolver = FontSizeResolvers.width,
Expand All @@ -85,6 +87,8 @@ class ScreenUtilInit extends StatefulWidget {
final bool minTextAdapt;
final bool useInheritedMediaQuery;
final bool ensureScreenSize;
final bool Function()? enableScaleWH;
final bool Function()? enableScaleText;
final RebuildFactor rebuildFactor;
final FontSizeResolver fontSizeResolver;

Expand All @@ -97,8 +101,7 @@ class ScreenUtilInit extends StatefulWidget {
State<ScreenUtilInit> createState() => _ScreenUtilInitState();
}

class _ScreenUtilInitState extends State<ScreenUtilInit>
with WidgetsBindingObserver {
class _ScreenUtilInitState extends State<ScreenUtilInit> with WidgetsBindingObserver {
final _canMarkedToBuild = HashSet<String>();
final _excludedWidgets = HashSet<String>();
MediaQueryData? _mediaQueryData;
Expand All @@ -110,6 +113,9 @@ class _ScreenUtilInitState extends State<ScreenUtilInit>
if (widget.responsiveWidgets != null) {
_canMarkedToBuild.addAll(widget.responsiveWidgets!);
}

ScreenUtil.enableScale(enableWH: widget.enableScaleWH, enableText: widget.enableScaleText);

_validateSize().then(_screenSizeCompleter.complete);

super.initState();
Expand Down

0 comments on commit 3ed9313

Please sign in to comment.