Skip to content

Commit

Permalink
null-safety
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmartineau committed Mar 15, 2021
1 parent aeb4c38 commit 1060a05
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 50 deletions.
2 changes: 1 addition & 1 deletion example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/ericm/fvm/versions/master"
export "FLUTTER_ROOT=/Users/ericm/fvm/versions/2.0.1"
export "FLUTTER_APPLICATION_PATH=/Users/ericm/sunny/local_plugins/flutter_screen_scaling/example"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
Expand Down
12 changes: 9 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
//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)
ScreenScaleProperties(width: 750, height: 1334, allowFontScaling: false);
ScreenScaleProperties(
width: 750, height: 1334, allowFontScaling: false, allowSubpixel: true);

return ExampleWidget(title: 'Screen Scaling Demo');
}
}

class ExampleWidget extends StatefulWidget {
const ExampleWidget({Key key, this.title}) : super(key: key);
const ExampleWidget({Key? key, required this.title}) : super(key: key);

final String title;

Expand Down Expand Up @@ -126,7 +127,12 @@ class _ExampleWidgetState extends State<ExampleWidget> {
floatingActionButton: FloatingActionButton(
child: Icon(Icons.title),
onPressed: () {
ScreenScale.init(width: 1500, height: 1334, allowFontScaling: false);
ScreenScale.init(
width: 1500,
height: 1334,
allowFontScaling: false,
allowSubpixel: true,
);
setState(() {});
},
),
Expand Down
14 changes: 9 additions & 5 deletions example/lib/main_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class MyApp extends StatelessWidget {
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
home: MyHomePage(title: "Flutter Screen Util"),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key key, this.title}) : super(key: key);
const MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

Expand All @@ -31,14 +31,15 @@ class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) {
//设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)

ScreenScaleProperties(width: 750, height: 1334, allowFontScaling: false);
ScreenScaleProperties(
width: 750, height: 1334, allowFontScaling: false, allowSubpixel: true);

return ExampleWidget(title: 'FlutterScreenUtil示例');
}
}

class ExampleWidget extends StatefulWidget {
const ExampleWidget({Key key, this.title}) : super(key: key);
const ExampleWidget({Key? key, required this.title}) : super(key: key);

final String title;

Expand Down Expand Up @@ -128,7 +129,10 @@ class _ExampleWidgetState extends State<ExampleWidget> {
child: Icon(Icons.title),
onPressed: () {
ScreenScaleProperties(
width: 1500, height: 1334, allowFontScaling: false);
width: 1500,
height: 1334,
allowFontScaling: false,
allowSubpixel: true);
setState(() {});
},
),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: flutter_screen_scale example
version: 1.0.0+1

environment:
sdk: ">=2.8.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
Expand Down
62 changes: 31 additions & 31 deletions lib/screen_scale_properties.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:math' as math;

import 'package:flutter/material.dart';

ScreenScaleProperties _instance;
ScreenScaleProperties? _instance;
const int screenDefaultWidth = 1080;
const int screenDefaultHeight = 1920;

Expand Down Expand Up @@ -65,59 +65,59 @@ class ScreenScaleProperties {
final double scaleHeight;

ScreenScaleProperties._(
{@required this.uiWidthPx,
@required this.uiHeightPx,
@required this.allowFontScaling,
@required this.allowSubpixel,
@required this.screenWidth,
@required this.screenHeight,
@required this.pixelRatio,
@required this.statusBarHeight,
@required this.bottomBarHeight,
@required this.textScaleFactor})
{required this.uiWidthPx,
required this.uiHeightPx,
required this.allowFontScaling,
required this.allowSubpixel,
required this.screenWidth,
required this.screenHeight,
required this.pixelRatio,
required this.statusBarHeight,
required this.bottomBarHeight,
required this.textScaleFactor})
: scaleWidth = screenWidth / (uiWidthPx * pixelRatio),
scaleHeight = screenHeight / (uiHeightPx * pixelRatio);

factory ScreenScaleProperties({
double width,
double height,
bool allowFontScaling,
@required bool allowSubpixel,
double maxWidth,
double? width,
double? height,
bool? allowFontScaling,
required bool allowSubpixel,
double? maxWidth,
}) {
if (maxWidth == null &&
width == null &&
height == null &&
allowFontScaling == null) {
return ScreenScale;
}
final pixelRatio = WidgetsBinding.instance.window.devicePixelRatio;
final physicalWidth = WidgetsBinding.instance.window.physicalSize.width;
final pixelRatio = WidgetsBinding.instance!.window.devicePixelRatio;
final physicalWidth = WidgetsBinding.instance!.window.physicalSize.width;
var widthCalc = maxWidth != null
? math.min(maxWidth * pixelRatio, physicalWidth)
: physicalWidth;
return _instance = ScreenScaleProperties._(
uiWidthPx: width ??
WidgetsBinding.instance.window.physicalSize.width / pixelRatio,
WidgetsBinding.instance!.window.physicalSize.width / pixelRatio,
uiHeightPx: height ??
(WidgetsBinding.instance.window.physicalSize.height / pixelRatio),
(WidgetsBinding.instance!.window.physicalSize.height / pixelRatio),
allowFontScaling: allowFontScaling ?? false,
allowSubpixel: allowSubpixel ?? allowFontScaling ?? false,
pixelRatio: WidgetsBinding.instance.window.devicePixelRatio,
pixelRatio: WidgetsBinding.instance!.window.devicePixelRatio,
screenWidth: widthCalc,
screenHeight: WidgetsBinding.instance.window.physicalSize.height,
statusBarHeight: WidgetsBinding.instance.window.padding.top,
bottomBarHeight: WidgetsBinding.instance.window.padding.bottom,
textScaleFactor: WidgetsBinding.instance.window.textScaleFactor,
screenHeight: WidgetsBinding.instance!.window.physicalSize.height,
statusBarHeight: WidgetsBinding.instance!.window.padding.top,
bottomBarHeight: WidgetsBinding.instance!.window.padding.bottom,
textScaleFactor: WidgetsBinding.instance!.window.textScaleFactor,
);
}

ScreenScaleProperties init({
double width,
double height,
bool allowFontScaling,
@required bool allowSubpixel,
double maxWidth,
double? width,
double? height,
bool? allowFontScaling,
required bool allowSubpixel,
double? maxWidth,
}) {
return ScreenScaleProperties(
width: width,
Expand Down Expand Up @@ -152,7 +152,7 @@ class ScreenScaleProperties {
///@param [fontSize] The size of the font on the UI design, in px.
///@param [allowFontScaling]
double convertFontSize(num fontSize,
{bool allowFontScaling, bool allowSubpixel}) {
{bool? allowFontScaling, bool? allowSubpixel}) {
allowSubpixel ??= this.allowSubpixel;
allowFontScaling ??= this.allowFontScaling;
var scaling = allowFontScaling
Expand Down
21 changes: 14 additions & 7 deletions lib/size_extension.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math' as math;

import 'screen_scale_properties.dart';

void sizeExtensions() {}
Expand All @@ -6,27 +8,32 @@ class Px {}

extension SizeExtension on num {
///[ScreenScale.setWidth]
double get w => ScreenScale.convertWidth(this);
double get w => ScreenScale.convertWidth(this).roundTo(3);

///[ScreenScale.setHeight]
double get h => ScreenScale.convertHeight(this);
double get h => ScreenScale.convertHeight(this).roundTo(3);

///[ScreenScale.setSp]
double get px => ScreenScale.convertFontSize(this);
double get px => ScreenScale.convertFontSize(this).roundTo(3);

///[ScreenScale.setSp]
double get ptScaled =>
ScreenScale.convertFontSize(this, allowFontScaling: true);
ScreenScale.convertFontSize(this, allowFontScaling: true).roundTo(3);

///[ScreenScale.setSp]
double get ptNoScaling =>
ScreenScale.convertFontSize(this, allowFontScaling: false);
ScreenScale.convertFontSize(this, allowFontScaling: false).roundTo(3);

///屏幕宽度的倍数
///Multiple of screen width
double get wp => ScreenScale.screenWidth * this;
double get wp => (ScreenScale.screenWidth * this).roundTo(3);

///屏幕高度的倍数
///Multiple of screen height
double get hp => ScreenScale.screenHeight * this;
double get hp => (ScreenScale.screenHeight * this).roundTo(3);

double roundTo([int? precision]) {
var mod = math.pow(10.0, precision!);
return ((this * mod).round().toDouble() / mod);
}
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: flutter_screen_scaling
description: A fork of flutter_screenutil with minor changes to the structure
version: 2.3.2
version: 3.3.2
homepage: https://github.com/SunnyFlutter/flutter_screen_scaling

environment:
sdk: ">=2.8.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
flutter:
Expand Down

0 comments on commit 1060a05

Please sign in to comment.