Skip to content

Commit

Permalink
fix: error using default font
Browse files Browse the repository at this point in the history
  • Loading branch information
gvenusleo committed May 17, 2023
1 parent ae37956 commit 321ca78
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
12 changes: 12 additions & 0 deletions lib/routes/feed_page/feed_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../models/feed.dart';
import '../../utils/dir.dart';
import '../../widgets/post_container.dart';
import '../../utils/parse.dart';
import '../read.dart';
Expand All @@ -20,6 +21,7 @@ class FeedPageState extends State<FeedPage> {
List<Post> postList = [];
bool onlyUnread = false;
bool onlyFavorite = false;
String? fontDir;

Future<void> getPostList() async {
await widget.feed.getAllPosts().then(
Expand Down Expand Up @@ -51,6 +53,14 @@ class FeedPageState extends State<FeedPage> {
);
}

void initFontDir() {
getFontDir().then((value) {
setState(() {
fontDir = value;
});
});
}

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -215,12 +225,14 @@ class FeedPageState extends State<FeedPage> {
mode: LaunchMode.externalApplication,
);
} else {
if (fontDir == null) return;
Navigator.push(
context,
CupertinoPageRoute(
builder: (context) => ReadPage(
post: postList[index],
fullText: widget.feed.fullText == 1,
fontDir: fontDir!,
),
),
).then((value) {
Expand Down
13 changes: 13 additions & 0 deletions lib/routes/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:url_launcher/url_launcher.dart';

import '../../widgets/post_container.dart';
import '../models/feed.dart';
import '../utils/dir.dart';
import '../utils/parse.dart';
import 'feed_page/add_feed_page.dart';
import 'feed_page/feed_page.dart';
Expand All @@ -23,6 +24,7 @@ class HomePageState extends State<HomePage> {
bool onlyUnread = false;
bool onlyFavorite = false;
Map<int, int> unreadCount = {};
String? fontDir;

Future<void> getFeedList() async {
await Feed.groupByCategory().then(
Expand Down Expand Up @@ -118,12 +120,21 @@ class HomePageState extends State<HomePage> {
}
}

void initFontDir() {
getFontDir().then((value) {
setState(() {
fontDir = value;
});
});
}

@override
void initState() {
super.initState();
getFeedList();
getPostList();
getUnreadCount();
initFontDir();
}

@override
Expand Down Expand Up @@ -315,12 +326,14 @@ class HomePageState extends State<HomePage> {
final bool fullText =
await postList[index].getFullText() == 1;
if (!mounted) return;
if (fontDir == null) return;
Navigator.push(
context,
CupertinoPageRoute(
builder: (context) => ReadPage(
post: postList[index],
fullText: fullText,
fontDir: fontDir!,
),
),
).then((value) {
Expand Down
32 changes: 4 additions & 28 deletions lib/routes/read.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import 'package:url_launcher/url_launcher.dart';
import '../models/post.dart';
import '../provider/read_page_provider.dart';
import '../provider/theme_provider.dart';
import '../utils/dir.dart';

class ReadPage extends StatefulWidget {
const ReadPage({
super.key,
required this.post,
required this.fullText,
required this.fontDir,
});
final Post post;
final bool fullText;
final String fontDir;

@override
ReadPageState createState() => ReadPageState();
Expand All @@ -29,17 +30,9 @@ class ReadPage extends StatefulWidget {
class ReadPageState extends State<ReadPage> {
int _index = 0; // 堆叠索引
String contentHtml = ''; // 内容 html
String? fontDir; // 字体路径

// 根据 url 获取 html 内容
Future<void> initData(String url) async {
if (context.read<ThemeProvider>().themeFont != '默认字体') {
getFontDir().then((value) {
setState(() {
fontDir = value;
});
});
}
if (widget.fullText && widget.post.read != 2 && widget.post.openType == 0) {
setState(() {
_index = 0;
Expand Down Expand Up @@ -81,7 +74,7 @@ class ReadPageState extends State<ReadPage> {
final String cssStr = '''
@font-face {
font-family: 'customFont';
src: url('$fontDir/${context.watch<ThemeProvider>().themeFont}');
src: url('${widget.fontDir}/${context.watch<ThemeProvider>().themeFont}');
}
body {
font-family: 'customFont', serif;
Expand Down Expand Up @@ -232,23 +225,6 @@ ${context.watch<ReadPageProvider>().customCss}
),
);
}
if (context.read<ThemeProvider>().themeFont != '默认字体' && fontDir == null) {
return const Center(
child: SizedBox(
height: 200,
width: 200,
child: Column(
children: [
CircularProgressIndicator(
strokeWidth: 3,
),
SizedBox(height: 12),
Text('加载字体'),
],
),
),
);
}
return InAppWebView(
initialData: widget.post.openType == 0
? InAppWebViewInitialData(
Expand All @@ -268,7 +244,7 @@ $contentHtml
</body>
</html>
''',
baseUrl: Uri.directory(fontDir!),
baseUrl: Uri.directory(widget.fontDir),
)
: null,
initialUrlRequest: widget.post.openType != 0
Expand Down
12 changes: 6 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ packages:
dependency: "direct main"
description:
name: dynamic_color
sha256: bbebb1b7ebed819e0ec83d4abdc2a8482d934f6a85289ffc1c6acf7589fa2aad
sha256: "74dff1435a695887ca64899b8990004f8d1232b0e84bfc4faa1fdda7c6f57cc1"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.6.3"
version: "1.6.5"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -438,10 +438,10 @@ packages:
dependency: "direct main"
description:
name: sqflite
sha256: "3a82c9a216b46b88617e3714dd74227eaca20c501c4abcc213e56db26b9caa00"
sha256: b4d6710e1200e96845747e37338ea8a819a12b51689a3bcf31eff0003b37a0b9
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.2.8+2"
version: "2.2.8+4"
sqflite_common:
dependency: transitive
description:
Expand Down Expand Up @@ -518,10 +518,10 @@ packages:
dependency: transitive
description:
name: url_launcher_android
sha256: "22f8db4a72be26e9e3a4aa3f194b1f7afbc76d20ec141f84be1d787db2155cbd"
sha256: "7aac14be5f4731b923cc697ae2d42043945076cd0dbb8806baecc92c1dc88891"
url: "https://pub.flutter-io.cn"
source: hosted
version: "6.0.31"
version: "6.0.33"
url_launcher_ios:
dependency: transitive
description:
Expand Down

0 comments on commit 321ca78

Please sign in to comment.