Skip to content

Commit f4d9763

Browse files
committed
尝试添加个预设字体
1 parent fb1dcfb commit f4d9763

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

example/lib/main.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:ui';
22

3+
import 'package:example/text_style.dart';
34
import 'package:flutter/material.dart';
45
import 'package:flutter_screenutil/flutter_screenutil.dart';
56

@@ -107,10 +108,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
107108
)),
108109
Text(
109110
'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),
114112
],
115113
)
116114
],

example/lib/main_zh.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:example/text_style.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_screenutil/flutter_screenutil.dart';
34

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

34-
ScreenUtil.init(context,width: 750, height: 1334, allowFontScaling: false);
35+
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
3536

3637
return ExampleWidget(title: 'FlutterScreenUtil示例');
3738
}
@@ -108,16 +109,14 @@ class _ExampleWidgetState extends State<ExampleWidget> {
108109
Column(
109110
crossAxisAlignment: CrossAxisAlignment.start,
110111
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+
),
121120
],
122121
)
123122
],
@@ -126,7 +125,8 @@ class _ExampleWidgetState extends State<ExampleWidget> {
126125
floatingActionButton: FloatingActionButton(
127126
child: Icon(Icons.title),
128127
onPressed: () {
129-
ScreenUtil.init(context,width: 1500, height: 1334, allowFontScaling: false);
128+
ScreenUtil.init(context,
129+
width: 1500, height: 1334, allowFontScaling: false);
130130
setState(() {});
131131
},
132132
),

example/lib/text_style.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

lib/screenutil.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ScreenUtil {
1919
/// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false.
2020
bool allowFontScaling;
2121

22-
static double _screenWidth;
22+
static double _screenWidth;
2323
static double _screenHeight;
2424
static double _pixelRatio;
2525
static double _statusBarHeight;
@@ -34,8 +34,8 @@ class ScreenUtil {
3434

3535
static void init(BuildContext context,
3636
{num width = defaultWidth,
37-
num height = defaultHeight,
38-
bool allowFontScaling = false}) {
37+
num height = defaultHeight,
38+
bool allowFontScaling = false}) {
3939
if (_instance == null) {
4040
_instance = ScreenUtil._();
4141
}
@@ -44,7 +44,7 @@ class ScreenUtil {
4444
_instance.allowFontScaling = allowFontScaling;
4545

4646
MediaQueryData mediaQuery = MediaQuery.of(context);
47-
_pixelRatio = mediaQuery.devicePixelRatio;
47+
_pixelRatio = mediaQuery.devicePixelRatio;
4848
_screenWidth = mediaQuery.size.width;
4949
_screenHeight = mediaQuery.size.height;
5050
_statusBarHeight = mediaQuery.padding.top;
@@ -117,9 +117,9 @@ class ScreenUtil {
117117
num setSp(num fontSize, {bool allowFontScalingSelf}) =>
118118
allowFontScalingSelf == null
119119
? (allowFontScaling
120-
? (fontSize * scaleText)
121-
: ((fontSize * scaleText) / _textScaleFactor))
120+
? (fontSize * scaleText)
121+
: ((fontSize * scaleText) / _textScaleFactor))
122122
: (allowFontScalingSelf
123-
? (fontSize * scaleText)
124-
: ((fontSize * scaleText) / _textScaleFactor));
123+
? (fontSize * scaleText)
124+
: ((fontSize * scaleText) / _textScaleFactor));
125125
}

0 commit comments

Comments
 (0)