Showing
4 changed files
with
45 additions
and
24 deletions
1 | import 'dart:ui'; | 1 | import 'dart:ui'; |
2 | 2 | ||
3 | +import 'package:example/text_style.dart'; | ||
3 | import 'package:flutter/material.dart'; | 4 | import 'package:flutter/material.dart'; |
4 | import 'package:flutter_screenutil/flutter_screenutil.dart'; | 5 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
5 | 6 | ||
@@ -107,10 +108,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -107,10 +108,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
107 | )), | 108 | )), |
108 | Text( | 109 | Text( |
109 | 'My font size is 24px on the design draft and will change with the system.', | 110 | 'My font size is 24px on the design draft and will change with the system.', |
110 | - style: TextStyle( | ||
111 | - color: Colors.black, | ||
112 | - fontSize: ScreenUtil() | ||
113 | - .setSp(24, allowFontScalingSelf: true))), | 111 | + style: ts.t1), |
114 | ], | 112 | ], |
115 | ) | 113 | ) |
116 | ], | 114 | ], |
1 | +import 'package:example/text_style.dart'; | ||
1 | import 'package:flutter/material.dart'; | 2 | import 'package:flutter/material.dart'; |
2 | import 'package:flutter_screenutil/flutter_screenutil.dart'; | 3 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
3 | 4 | ||
@@ -31,7 +32,7 @@ class _MyHomePageState extends State<MyHomePage> { | @@ -31,7 +32,7 @@ class _MyHomePageState extends State<MyHomePage> { | ||
31 | Widget build(BuildContext context) { | 32 | Widget build(BuildContext context) { |
32 | //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) | 33 | //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) |
33 | 34 | ||
34 | - ScreenUtil.init(context,width: 750, height: 1334, allowFontScaling: false); | 35 | + ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false); |
35 | 36 | ||
36 | return ExampleWidget(title: 'FlutterScreenUtil示例'); | 37 | return ExampleWidget(title: 'FlutterScreenUtil示例'); |
37 | } | 38 | } |
@@ -108,16 +109,14 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -108,16 +109,14 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
108 | Column( | 109 | Column( |
109 | crossAxisAlignment: CrossAxisAlignment.start, | 110 | crossAxisAlignment: CrossAxisAlignment.start, |
110 | children: <Widget>[ | 111 | children: <Widget>[ |
111 | - Text('我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化', | ||
112 | - style: TextStyle( | ||
113 | - color: Colors.black, | ||
114 | - fontSize: 24.sp, | ||
115 | - )), | ||
116 | - Text('我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化', | ||
117 | - style: TextStyle( | ||
118 | - color: Colors.black, | ||
119 | - fontSize: ScreenUtil() | ||
120 | - .setSp(24, allowFontScalingSelf: true))), | 112 | + Text( |
113 | + '我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化', | ||
114 | + style: ts.t2, | ||
115 | + ), | ||
116 | + Text( | ||
117 | + '我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化', | ||
118 | + style: ts.t1, | ||
119 | + ), | ||
121 | ], | 120 | ], |
122 | ) | 121 | ) |
123 | ], | 122 | ], |
@@ -126,7 +125,8 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -126,7 +125,8 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
126 | floatingActionButton: FloatingActionButton( | 125 | floatingActionButton: FloatingActionButton( |
127 | child: Icon(Icons.title), | 126 | child: Icon(Icons.title), |
128 | onPressed: () { | 127 | onPressed: () { |
129 | - ScreenUtil.init(context,width: 1500, height: 1334, allowFontScaling: false); | 128 | + ScreenUtil.init(context, |
129 | + width: 1500, height: 1334, allowFontScaling: false); | ||
130 | setState(() {}); | 130 | setState(() {}); |
131 | }, | 131 | }, |
132 | ), | 132 | ), |
example/lib/text_style.dart
0 → 100644
1 | +import 'package:flutter/material.dart'; | ||
2 | +import 'package:flutter_screenutil/flutter_screenutil.dart'; | ||
3 | + | ||
4 | +class TextStyles { | ||
5 | + TextStyle t1 = TextStyle(fontSize: 24.ssp, color: Colors.black); | ||
6 | + TextStyle t2 = TextStyle(fontSize: 24.sp, color: Colors.black); | ||
7 | +} | ||
8 | + | ||
9 | +var ts = TextStyles(); | ||
10 | + | ||
11 | +class TextStyle2 { | ||
12 | + static TextStyle2 ts2; | ||
13 | + | ||
14 | + factory TextStyle2() { | ||
15 | + if (ts2 == null) { | ||
16 | + ts2 = TextStyle2(); | ||
17 | + } | ||
18 | + return ts2; | ||
19 | + } | ||
20 | + | ||
21 | + TextStyle t1 = TextStyle(fontSize: 24.ssp, color: Colors.black); | ||
22 | + TextStyle t2 = TextStyle(fontSize: 24.sp, color: Colors.black); | ||
23 | +} |
@@ -19,7 +19,7 @@ class ScreenUtil { | @@ -19,7 +19,7 @@ class ScreenUtil { | ||
19 | /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false. | 19 | /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false. |
20 | bool allowFontScaling; | 20 | bool allowFontScaling; |
21 | 21 | ||
22 | - static double _screenWidth; | 22 | + static double _screenWidth; |
23 | static double _screenHeight; | 23 | static double _screenHeight; |
24 | static double _pixelRatio; | 24 | static double _pixelRatio; |
25 | static double _statusBarHeight; | 25 | static double _statusBarHeight; |
@@ -34,8 +34,8 @@ class ScreenUtil { | @@ -34,8 +34,8 @@ class ScreenUtil { | ||
34 | 34 | ||
35 | static void init(BuildContext context, | 35 | static void init(BuildContext context, |
36 | {num width = defaultWidth, | 36 | {num width = defaultWidth, |
37 | - num height = defaultHeight, | ||
38 | - bool allowFontScaling = false}) { | 37 | + num height = defaultHeight, |
38 | + bool allowFontScaling = false}) { | ||
39 | if (_instance == null) { | 39 | if (_instance == null) { |
40 | _instance = ScreenUtil._(); | 40 | _instance = ScreenUtil._(); |
41 | } | 41 | } |
@@ -44,7 +44,7 @@ class ScreenUtil { | @@ -44,7 +44,7 @@ class ScreenUtil { | ||
44 | _instance.allowFontScaling = allowFontScaling; | 44 | _instance.allowFontScaling = allowFontScaling; |
45 | 45 | ||
46 | MediaQueryData mediaQuery = MediaQuery.of(context); | 46 | MediaQueryData mediaQuery = MediaQuery.of(context); |
47 | - _pixelRatio = mediaQuery.devicePixelRatio; | 47 | + _pixelRatio = mediaQuery.devicePixelRatio; |
48 | _screenWidth = mediaQuery.size.width; | 48 | _screenWidth = mediaQuery.size.width; |
49 | _screenHeight = mediaQuery.size.height; | 49 | _screenHeight = mediaQuery.size.height; |
50 | _statusBarHeight = mediaQuery.padding.top; | 50 | _statusBarHeight = mediaQuery.padding.top; |
@@ -117,9 +117,9 @@ class ScreenUtil { | @@ -117,9 +117,9 @@ class ScreenUtil { | ||
117 | num setSp(num fontSize, {bool allowFontScalingSelf}) => | 117 | num setSp(num fontSize, {bool allowFontScalingSelf}) => |
118 | allowFontScalingSelf == null | 118 | allowFontScalingSelf == null |
119 | ? (allowFontScaling | 119 | ? (allowFontScaling |
120 | - ? (fontSize * scaleText) | ||
121 | - : ((fontSize * scaleText) / _textScaleFactor)) | 120 | + ? (fontSize * scaleText) |
121 | + : ((fontSize * scaleText) / _textScaleFactor)) | ||
122 | : (allowFontScalingSelf | 122 | : (allowFontScalingSelf |
123 | - ? (fontSize * scaleText) | ||
124 | - : ((fontSize * scaleText) / _textScaleFactor)); | 123 | + ? (fontSize * scaleText) |
124 | + : ((fontSize * scaleText) / _textScaleFactor)); | ||
125 | } | 125 | } |
-
Please register or login to post a comment