Skip to content

Commit 0b24b8e

Browse files
Merge pull request #28 from OctagonalStar/dev
Improve font load
2 parents 6a8124f + dec2280 commit 0b24b8e

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v?.?.?? - ????-??-?? - (??????)
4+
5+
### Improvement
6+
7+
- 优化了网页端字体加载逻辑
8+
39
## v0.1.11 - 2025-11-28 - (000111)
410

511
### Added

lib/pages/setting_page.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ class _SettingPage extends State<SettingPage> {
110110
DropdownMenuItem(value: 2, child: Text('中阿均使用备用字体')),
111111
],
112112
onChanged: (value) {
113+
if(value == 2 && kIsWeb) {
114+
ScaffoldMessenger.of(context).showSnackBar(
115+
SnackBar(content: Text("网页版加载中文字体需要较长时间,请先耐心等待"), duration: Duration(seconds: 3),),
116+
);
117+
}
113118
setting['regular']['font'] = value;
114119
Provider.of<Global>(context, listen: false).updateSetting();
115120
},

lib/vars/global.dart

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:arabic_learning/funcs/utili.dart';
22
import 'package:arabic_learning/vars/statics_var.dart';
33
import 'package:flutter/foundation.dart';
44
import 'package:path_provider/path_provider.dart' as path_provider;
5-
import 'package:flutter/services.dart' show rootBundle;
5+
import 'package:flutter/services.dart' show rootBundle, FontLoader;
66
import 'package:arabic_learning/package_replacement/storage.dart';
77
import 'dart:convert';
88
import 'package:flutter/material.dart';
@@ -15,7 +15,7 @@ class Global with ChangeNotifier {
1515
late bool updateLogRequire; //是否需要显示更新日志
1616
late bool isWideScreen; // 设备是否是宽屏幕
1717
late final SharedPreferences prefs; // 储存实例
18-
18+
bool backupFontLoaded = false;
1919
late bool modelTTSDownloaded = false;
2020

2121
/// the setting data
@@ -239,6 +239,19 @@ class Global with ChangeNotifier {
239239
if(refresh) await postInit();
240240
}
241241

242+
void loadFont() async {
243+
if(backupFontLoaded) return;
244+
backupFontLoaded = true;
245+
try{
246+
final ByteData bundle = await rootBundle.load("assets/fonts/zh/NotoSansSC-Medium.ttf");
247+
final FontLoader loader = FontLoader(StaticsVar.zhBackupFont)..addFont(Future.value(bundle));
248+
loader.load();
249+
} catch (e) {
250+
backupFontLoaded = false;
251+
}
252+
notifyListeners();
253+
}
254+
242255
Future<void> postInit() async {
243256
await loadTTS();
244257
await loadEggs();
@@ -285,24 +298,25 @@ class Global with ChangeNotifier {
285298
}
286299

287300
void updateTheme() {
288-
_themeData = ThemeData(
289-
useMaterial3: true,
290-
colorScheme: ColorScheme.fromSeed(
291-
seedColor: _themeList[settingData["regular"]["theme"]],
292-
brightness: settingData["regular"]["darkMode"] ? Brightness.dark : Brightness.light,
293-
),
294-
fontFamily: settingData["regular"]["font"] == 2 ? "NotoSansSC" : null,
295-
);
296301
if(settingData["regular"]["font"] == 2) {
297302
arFont = StaticsVar.arBackupFont;
298303
zhFont = StaticsVar.zhBackupFont;
304+
loadFont();
299305
} else if(settingData["regular"]["font"] == 1) {
300306
arFont = StaticsVar.arBackupFont;
301307
zhFont = null;
302308
} else {
303309
arFont = null;
304310
zhFont = null;
305311
}
312+
_themeData = ThemeData(
313+
useMaterial3: true,
314+
colorScheme: ColorScheme.fromSeed(
315+
seedColor: _themeList[settingData["regular"]["theme"]],
316+
brightness: settingData["regular"]["darkMode"] ? Brightness.dark : Brightness.light,
317+
),
318+
fontFamily: zhFont,
319+
);
306320
}
307321

308322
void acceptAggrement(String name) {

pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ flutter:
8282
assets:
8383
- CHANGELOG.md
8484
- assets/eggs/s.txt
85+
- assets/fonts/zh/NotoSansSC-Medium.ttf
8586
fonts:
8687
- family: Vazirmatn
8788
fonts:
8889
- asset: assets/fonts/ar/Vazirmatn-VariableFont_wght.ttf
8990
- family: NotoSansSC
90-
fonts:
91-
- asset: assets/fonts/zh/NotoSansSC-Medium.ttf
91+
# fonts:
92+
# - asset: assets/fonts/zh/NotoSansSC-Medium.ttf
9293
flutter_launcher_icons:
9394
android: "launcher_icon"
9495
ios: true

0 commit comments

Comments
 (0)