Compare commits
2 Commits
d512a77f5f
...
61922cbcd8
| Author | SHA1 | Date | |
|---|---|---|---|
| 61922cbcd8 | |||
| 47b6fb9937 |
@@ -1,6 +1,6 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
<application
|
<application
|
||||||
android:label="blog_app"
|
android:label="拾光记"
|
||||||
android:name="${applicationName}"
|
android:name="${applicationName}"
|
||||||
android:icon="@mipmap/ic_launcher">
|
android:icon="@mipmap/ic_launcher">
|
||||||
<activity
|
<activity
|
||||||
|
|||||||
@@ -35,6 +35,15 @@ Future<List<Blog>> queryBlogByConditionApi(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<List<BlogSearch>> searchBlog(String keyword) {
|
||||||
|
return HttpUtil().get<List<BlogSearch>>(
|
||||||
|
"/blog/search",
|
||||||
|
queryParameters: {"keyword": keyword},
|
||||||
|
converter:
|
||||||
|
(data) => convertListResponse<BlogSearch>(data, BlogSearch.fromJson),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Future<Blog> queryBlogByIdApi(int id) {
|
Future<Blog> queryBlogByIdApi(int id) {
|
||||||
return HttpUtil().get<Blog>(
|
return HttpUtil().get<Blog>(
|
||||||
"/blog/$id/content",
|
"/blog/$id/content",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/// 应用信息
|
/// 应用信息
|
||||||
class AppConfig {
|
class AppConfig {
|
||||||
// static const String baseApiUrl = "https://cxx0822.iepose.cn/blog-api";
|
static const String baseApiUrl = "https://cxx0822.iepose.cn/blog-api";
|
||||||
static const String baseApiUrl = "http://192.168.1.4:8082";
|
// static const String baseApiUrl = "http://192.168.1.4:8082";
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,21 @@
|
|||||||
import 'package:blog_app/pages/home_page.dart';
|
import 'package:blog_app/pages/home_page.dart';
|
||||||
|
import 'package:blog_app/provider/blog.dart';
|
||||||
import 'package:blog_app/utils/sp_utils.dart';
|
import 'package:blog_app/utils/sp_utils.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
await SPUtil.init();
|
await SPUtil.init();
|
||||||
runApp(MyApp());
|
|
||||||
|
runApp(
|
||||||
|
MultiProvider(
|
||||||
|
providers: [
|
||||||
|
ChangeNotifierProvider(create: (context) => BlogProvider()),
|
||||||
|
],
|
||||||
|
child: MyApp(),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
|
|||||||
@@ -107,3 +107,30 @@ class BlogStats {
|
|||||||
|
|
||||||
Map<String, dynamic> toJson() => _$BlogStatsToJson(this);
|
Map<String, dynamic> toJson() => _$BlogStatsToJson(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(genericArgumentFactories: true)
|
||||||
|
class BlogSearch {
|
||||||
|
@JsonKey(name: 'id')
|
||||||
|
final int id;
|
||||||
|
|
||||||
|
@JsonKey(name: 'title')
|
||||||
|
final String title;
|
||||||
|
|
||||||
|
@JsonKey(name: 'content')
|
||||||
|
final String content;
|
||||||
|
|
||||||
|
@JsonKey(name: 'highlight')
|
||||||
|
final String highlight;
|
||||||
|
|
||||||
|
const BlogSearch({
|
||||||
|
required this.id,
|
||||||
|
required this.title,
|
||||||
|
required this.content,
|
||||||
|
required this.highlight,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory BlogSearch.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$BlogSearchFromJson(json);
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => _$BlogSearchToJson(this);
|
||||||
|
}
|
||||||
|
|||||||
@@ -59,3 +59,18 @@ Map<String, dynamic> _$BlogStatsToJson(BlogStats instance) => <String, dynamic>{
|
|||||||
'visitCount': instance.visitCount,
|
'visitCount': instance.visitCount,
|
||||||
'wordCount': instance.wordCount,
|
'wordCount': instance.wordCount,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BlogSearch _$BlogSearchFromJson(Map<String, dynamic> json) => BlogSearch(
|
||||||
|
id: (json['id'] as num).toInt(),
|
||||||
|
title: json['title'] as String,
|
||||||
|
content: json['content'] as String,
|
||||||
|
highlight: json['highlight'] as String,
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, dynamic> _$BlogSearchToJson(BlogSearch instance) =>
|
||||||
|
<String, dynamic>{
|
||||||
|
'id': instance.id,
|
||||||
|
'title': instance.title,
|
||||||
|
'content': instance.content,
|
||||||
|
'highlight': instance.highlight,
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import 'package:blog_app/apis/blog.dart';
|
import 'package:blog_app/widget/common.dart';
|
||||||
import 'package:blog_app/models/blog.dart';
|
import 'package:blog_app/widget/markdown.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:blog_app/provider/blog.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:markdown_widget/config/toc.dart';
|
import 'package:markdown_widget/config/toc.dart';
|
||||||
import 'package:markdown_widget/widget/markdown.dart';
|
|
||||||
|
|
||||||
class BlogDetailPage extends StatefulWidget {
|
class BlogDetailPage extends StatefulWidget {
|
||||||
final int blogId;
|
final int blogId;
|
||||||
@@ -16,20 +17,14 @@ class BlogDetailPage extends StatefulWidget {
|
|||||||
class _BlogDetailPageState extends State<BlogDetailPage> {
|
class _BlogDetailPageState extends State<BlogDetailPage> {
|
||||||
final tocController = TocController();
|
final tocController = TocController();
|
||||||
bool _showToc = false;
|
bool _showToc = false;
|
||||||
late Future<Blog> _blogDetail;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_blogDetail = _loadBlogDetail();
|
// 初始化加载数据
|
||||||
}
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<BlogProvider>().queryBlogById(widget.blogId);
|
||||||
Future<Blog> _loadBlogDetail() async {
|
});
|
||||||
try {
|
|
||||||
return await queryBlogByIdApi(widget.blogId);
|
|
||||||
} catch (e) {
|
|
||||||
throw Exception('获取博客详情失败: $e');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildBlogTitle(String title) {
|
Widget _buildBlogTitle(String title) {
|
||||||
@@ -43,113 +38,64 @@ class _BlogDetailPageState extends State<BlogDetailPage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildTocPanel() => Visibility(
|
Widget? buildFloatingActionButton(BlogProvider provider) {
|
||||||
visible: _showToc,
|
if (!provider.isLoading) {
|
||||||
child: Align(
|
return FloatingActionButton(
|
||||||
alignment: Alignment.bottomRight,
|
|
||||||
child: Container(
|
|
||||||
width: 250,
|
|
||||||
height: 400,
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: Colors.white,
|
|
||||||
borderRadius: BorderRadius.circular(12),
|
|
||||||
boxShadow: [
|
|
||||||
BoxShadow(
|
|
||||||
color: Colors.black.withAlpha(50),
|
|
||||||
blurRadius: 8,
|
|
||||||
offset: const Offset(0, 4),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
border: Border.all(color: Colors.grey[300]!),
|
|
||||||
),
|
|
||||||
child: Column(
|
|
||||||
children: [
|
|
||||||
Container(
|
|
||||||
padding: const EdgeInsets.all(12),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: Colors.grey[100],
|
|
||||||
borderRadius: const BorderRadius.only(
|
|
||||||
topLeft: Radius.circular(12),
|
|
||||||
topRight: Radius.circular(12),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
child: Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
children: [
|
|
||||||
const Text(
|
|
||||||
'目录',
|
|
||||||
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
Expanded(
|
|
||||||
child: TocWidget(
|
|
||||||
controller: tocController,
|
|
||||||
tocTextStyle: TextStyle(fontSize: 14),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
Widget _buildMarkdown(String data) =>
|
|
||||||
MarkdownWidget(data: data, tocController: tocController);
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return Scaffold(
|
|
||||||
appBar: AppBar(title: Text('博客详情')),
|
|
||||||
body: Container(
|
|
||||||
padding: EdgeInsets.all(16),
|
|
||||||
child: FutureBuilder<Blog>(
|
|
||||||
future: _blogDetail,
|
|
||||||
builder: (context, snapshot) {
|
|
||||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
||||||
return Center(child: CircularProgressIndicator());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (snapshot.hasError) {
|
|
||||||
return Center(
|
|
||||||
child: Column(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
children: [Text('加载失败: ${snapshot.error}')],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!snapshot.hasData) {
|
|
||||||
return Center(child: Text('暂无数据'));
|
|
||||||
}
|
|
||||||
|
|
||||||
final blogDetail = snapshot.data!;
|
|
||||||
return _buildBlogDetailContent(context, blogDetail);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
floatingActionButton: FloatingActionButton(
|
|
||||||
onPressed: () => setState(() => _showToc = !_showToc),
|
onPressed: () => setState(() => _showToc = !_showToc),
|
||||||
mini: true,
|
mini: true,
|
||||||
backgroundColor: Theme.of(context).primaryColor,
|
backgroundColor: Theme.of(context).primaryColor,
|
||||||
child: Icon(_showToc ? Icons.close : Icons.list, color: Colors.white),
|
child: Icon(_showToc ? Icons.close : Icons.list, color: Colors.white),
|
||||||
),
|
);
|
||||||
);
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildBlogDetailContent(BuildContext context, Blog blogDetail) {
|
Widget _buildContent(BlogProvider provider) {
|
||||||
|
if (provider.error != null) {
|
||||||
|
return buildErrorInfo(
|
||||||
|
errorInfo: provider.error!,
|
||||||
|
onPressed: () => provider.queryBlogById(widget.blogId),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return Stack(
|
return Stack(
|
||||||
children: [
|
children: [
|
||||||
Column(
|
Column(
|
||||||
children: [
|
children: [
|
||||||
_buildBlogTitle(blogDetail.title),
|
_buildBlogTitle(provider.currentBlog.title),
|
||||||
SizedBox(height: 10),
|
SizedBox(height: 10),
|
||||||
Expanded(child: _buildMarkdown(blogDetail.content!)),
|
Expanded(
|
||||||
|
child: buildMarkdown(
|
||||||
|
tocController: tocController,
|
||||||
|
data: provider.currentBlog.content!,
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
_buildTocPanel(),
|
buildTocPanel(tocController: tocController, showToc: _showToc),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final blogProvider = context.watch<BlogProvider>();
|
||||||
|
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: Text('博客详情')),
|
||||||
|
body: Container(
|
||||||
|
padding: EdgeInsets.all(16),
|
||||||
|
child: Stack(
|
||||||
|
children: [
|
||||||
|
if (blogProvider.isLoading)
|
||||||
|
buildLoadingIndicator()
|
||||||
|
else
|
||||||
|
_buildContent(blogProvider),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
floatingActionButton: buildFloatingActionButton(blogProvider),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import 'package:blog_app/apis/blog.dart';
|
import 'package:blog_app/provider/blog.dart';
|
||||||
import 'package:blog_app/models/blog.dart';
|
import 'package:blog_app/widget/common.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
import 'package:blog_app/widget/easy_refresh.dart';
|
import 'package:blog_app/widget/easy_refresh.dart';
|
||||||
import 'package:blog_app/widget/blog.dart';
|
import 'package:blog_app/widget/blog.dart';
|
||||||
import 'package:easy_refresh/easy_refresh.dart';
|
import 'package:easy_refresh/easy_refresh.dart';
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
|
|
||||||
class BlogPage extends StatefulWidget {
|
class BlogPage extends StatefulWidget {
|
||||||
const BlogPage({super.key});
|
const BlogPage({super.key});
|
||||||
@@ -13,25 +14,21 @@ class BlogPage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _BlogPageState extends State<BlogPage> {
|
class _BlogPageState extends State<BlogPage> {
|
||||||
late List<Blog> blogList = [];
|
|
||||||
|
|
||||||
int _currentPage = 1;
|
|
||||||
final int _pageSize = 5;
|
|
||||||
bool _hasMore = true;
|
|
||||||
bool _showScrollToTop = false;
|
|
||||||
|
|
||||||
final EasyRefreshController _freshController = EasyRefreshController(
|
final EasyRefreshController _freshController = EasyRefreshController(
|
||||||
controlFinishRefresh: true,
|
controlFinishRefresh: true,
|
||||||
controlFinishLoad: true,
|
controlFinishLoad: true,
|
||||||
);
|
);
|
||||||
|
|
||||||
final ScrollController _scrollController = ScrollController();
|
final ScrollController _scrollController = ScrollController();
|
||||||
|
bool _showScrollToTop = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_loadData(isRefresh: true);
|
|
||||||
_scrollController.addListener(_onScroll);
|
_scrollController.addListener(_onScroll);
|
||||||
|
// 初始化加载数据
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<BlogProvider>().refreshBlogList();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -42,49 +39,17 @@ class _BlogPageState extends State<BlogPage> {
|
|||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _loadData({required bool isRefresh}) async {
|
|
||||||
try {
|
|
||||||
// 如果是刷新,重置页码
|
|
||||||
if (isRefresh) {
|
|
||||||
_currentPage = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
final result = await queryBlogByPageApi(_currentPage, _pageSize);
|
|
||||||
|
|
||||||
setState(() {
|
|
||||||
if (isRefresh) {
|
|
||||||
// 刷新时直接替换数据
|
|
||||||
blogList = result.records;
|
|
||||||
} else {
|
|
||||||
// 加载更多时追加数据
|
|
||||||
blogList.addAll(result.records);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 判断是否还有更多数据
|
|
||||||
_hasMore = result.current < result.pages;
|
|
||||||
// 如果有更多数据,准备加载下一页
|
|
||||||
if (_hasMore) {
|
|
||||||
_currentPage++;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
// 处理错误
|
|
||||||
debugPrint('加载数据失败: $e');
|
|
||||||
} finally {
|
|
||||||
_freshController.finishRefresh();
|
|
||||||
_freshController.resetFooter();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 下拉刷新
|
// 下拉刷新
|
||||||
Future<void> _onRefresh() async {
|
Future<void> _onRefresh() async {
|
||||||
await _loadData(isRefresh: true);
|
await context.read<BlogProvider>().refreshBlogList();
|
||||||
|
_freshController.finishRefresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 上拉加载
|
// 上拉加载
|
||||||
Future<void> _onLoad() async {
|
Future<void> _onLoad() async {
|
||||||
if (_hasMore) {
|
final blogProvider = context.read<BlogProvider>();
|
||||||
await _loadData(isRefresh: false);
|
if (blogProvider.hasMore) {
|
||||||
|
await blogProvider.loadMoreBlogList();
|
||||||
_freshController.finishLoad(IndicatorResult.success);
|
_freshController.finishLoad(IndicatorResult.success);
|
||||||
} else {
|
} else {
|
||||||
_freshController.finishLoad(IndicatorResult.noMore);
|
_freshController.finishLoad(IndicatorResult.noMore);
|
||||||
@@ -111,15 +76,22 @@ class _BlogPageState extends State<BlogPage> {
|
|||||||
// 滚动到顶部
|
// 滚动到顶部
|
||||||
void _scrollToTop() => scrollToTopAnimateTo(_scrollController);
|
void _scrollToTop() => scrollToTopAnimateTo(_scrollController);
|
||||||
|
|
||||||
Widget buildBlogList() {
|
Widget _buildBlogList(BlogProvider provider) {
|
||||||
|
if (provider.error != null) {
|
||||||
|
return buildErrorInfo(
|
||||||
|
errorInfo: provider.error!,
|
||||||
|
onPressed: () => provider.refreshBlogList,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
controller: _scrollController,
|
controller: _scrollController,
|
||||||
itemCount: blogList.length,
|
itemCount: provider.blogList.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final blog = blogList[index];
|
final blog = provider.blogList[index];
|
||||||
return InkWell(
|
return InkWell(
|
||||||
onTap: () => navigatorToBlogDetail(context, blog.id),
|
onTap: () => navigatorToBlogDetail(context, blog.id),
|
||||||
child: BlogCard(blog: blogList[index]),
|
child: BlogCard(blog: blog),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -127,13 +99,28 @@ class _BlogPageState extends State<BlogPage> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final blogProvider = context.watch<BlogProvider>();
|
||||||
|
|
||||||
return Stack(
|
return Stack(
|
||||||
children: [
|
children: [
|
||||||
buildEasyRefresh(
|
Column(
|
||||||
freshController: _freshController,
|
children: [
|
||||||
onRefresh: _onRefresh,
|
Expanded(
|
||||||
onLoad: _onLoad,
|
child: buildEasyRefresh(
|
||||||
body: buildBlogList(),
|
freshController: _freshController,
|
||||||
|
onRefresh: _onRefresh,
|
||||||
|
onLoad: _onLoad,
|
||||||
|
body: Stack(
|
||||||
|
children: [
|
||||||
|
if (blogProvider.isLoading)
|
||||||
|
buildLoadingIndicator()
|
||||||
|
else
|
||||||
|
_buildBlogList(blogProvider),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
if (_showScrollToTop)
|
if (_showScrollToTop)
|
||||||
buildScrollToTop(context: context, scrollToTop: _scrollToTop),
|
buildScrollToTop(context: context, scrollToTop: _scrollToTop),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'package:blog_app/apis/blog.dart';
|
import 'package:blog_app/widget/common.dart';
|
||||||
import 'package:blog_app/models/blog.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:blog_app/provider/blog.dart';
|
||||||
import 'package:blog_app/widget/blog.dart';
|
import 'package:blog_app/widget/blog.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
@@ -13,30 +14,20 @@ class CategoryListPage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _CategoryListPageState extends State<CategoryListPage> {
|
class _CategoryListPageState extends State<CategoryListPage> {
|
||||||
late List<Blog> blogs = [];
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_loadBlogList();
|
// 初始化加载数据
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<BlogProvider>().refreshBlogByCategory(widget.category);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _loadBlogList() async {
|
Widget _buildBlogList(BlogProvider provider) {
|
||||||
try {
|
|
||||||
final result = await queryBlogByConditionApi(widget.category, null, null);
|
|
||||||
setState(() {
|
|
||||||
blogs = result;
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
throw Exception('获取博客列表失败: $e');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _buildBlogList() {
|
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
itemCount: blogs.length,
|
itemCount: provider.blogList.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final blog = blogs[index];
|
final blog = provider.blogList[index];
|
||||||
return InkWell(
|
return InkWell(
|
||||||
onTap: () => navigatorToBlogDetail(context, blog.id),
|
onTap: () => navigatorToBlogDetail(context, blog.id),
|
||||||
child: buildBlogListItem(context, blog, index + 1),
|
child: buildBlogListItem(context, blog, index + 1),
|
||||||
@@ -45,27 +36,45 @@ class _CategoryListPageState extends State<CategoryListPage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget _buildContent(BlogProvider provider) {
|
||||||
|
if (provider.error != null) {
|
||||||
|
return buildErrorInfo(
|
||||||
|
errorInfo: provider.error!,
|
||||||
|
onPressed: () => provider.refreshBlogByYear(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Center(child: Text(widget.category, style: TextStyle(fontSize: 20))),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
buildListTitle(provider.blogList.length),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Expanded(
|
||||||
|
child:
|
||||||
|
provider.blogList.isEmpty
|
||||||
|
? buildEmpty()
|
||||||
|
: _buildBlogList(provider),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final colors = Theme.of(context).colorScheme;
|
final blogProvider = context.watch<BlogProvider>();
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: Text('博客详情')),
|
appBar: AppBar(title: Text('博客详情')),
|
||||||
body: Padding(
|
body: Padding(
|
||||||
padding: const EdgeInsets.all(16.0),
|
padding: const EdgeInsets.all(16.0),
|
||||||
child: Column(
|
child: Stack(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
|
||||||
children: [
|
children: [
|
||||||
Center(
|
if (blogProvider.isLoading)
|
||||||
child: Text(
|
buildLoadingIndicator()
|
||||||
widget.category,
|
else
|
||||||
style: TextStyle(fontSize: 20, color: colors.primary),
|
_buildContent(blogProvider),
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 8),
|
|
||||||
buildListTitle(blogs.length),
|
|
||||||
const SizedBox(height: 8),
|
|
||||||
Expanded(child: blogs.isEmpty ? buildEmpty() : _buildBlogList()),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'package:blog_app/apis/blog.dart';
|
import 'package:blog_app/widget/common.dart';
|
||||||
import 'package:blog_app/models/blog.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:blog_app/provider/blog.dart';
|
||||||
import 'package:blog_app/widget/blog.dart';
|
import 'package:blog_app/widget/blog.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
@@ -11,38 +12,34 @@ class CategoryPage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _CategoryPageState extends State<CategoryPage> {
|
class _CategoryPageState extends State<CategoryPage> {
|
||||||
late List<BlogCategory> categories = [];
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_loadBlogCategory();
|
// 初始化加载数据
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<BlogProvider>().refreshBlogCategory();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _loadBlogCategory() async {
|
Widget _buildContent(BlogProvider provider) {
|
||||||
try {
|
if (provider.error != null) {
|
||||||
final result = await queryBlogCategoryApi();
|
return buildErrorInfo(
|
||||||
setState(() {
|
errorInfo: provider.error!,
|
||||||
categories = result;
|
onPressed: () => provider.refreshBlogByYear(),
|
||||||
});
|
);
|
||||||
} catch (e) {
|
|
||||||
throw Exception('获取博客分类失败: $e');
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return Column(
|
return Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
buildCategoryTitle(context, categories.length),
|
buildCategoryTitle(context, provider.categoryList.length),
|
||||||
SizedBox(height: 10),
|
SizedBox(height: 10),
|
||||||
// 分类列表
|
// 分类列表
|
||||||
Expanded(
|
Expanded(
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
itemCount: categories.length,
|
itemCount: provider.categoryList.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final category = categories[index];
|
final category = provider.categoryList[index];
|
||||||
return buildCategoryCard(context, category);
|
return buildCategoryCard(context, category);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -50,4 +47,24 @@ class _CategoryPageState extends State<CategoryPage> {
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final blogProvider = context.watch<BlogProvider>();
|
||||||
|
|
||||||
|
return Column(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: Stack(
|
||||||
|
children: [
|
||||||
|
if (blogProvider.isLoading)
|
||||||
|
buildLoadingIndicator()
|
||||||
|
else
|
||||||
|
_buildContent(blogProvider),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
|
import 'package:blog_app/apis/blog.dart';
|
||||||
import 'package:blog_app/layout/drawer.dart';
|
import 'package:blog_app/layout/drawer.dart';
|
||||||
import 'package:blog_app/layout/menu.dart';
|
import 'package:blog_app/layout/menu.dart';
|
||||||
|
import 'package:blog_app/models/blog.dart';
|
||||||
|
import 'package:blog_app/widget/search.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
class HomePage extends StatefulWidget {
|
class HomePage extends StatefulWidget {
|
||||||
@@ -11,6 +14,12 @@ class _HomePageState extends State<HomePage> {
|
|||||||
int _currentPageIndex = 0;
|
int _currentPageIndex = 0;
|
||||||
late PageController _pageController;
|
late PageController _pageController;
|
||||||
|
|
||||||
|
bool _isSearching = false;
|
||||||
|
final TextEditingController _searchController = TextEditingController();
|
||||||
|
final FocusNode _searchFocusNode = FocusNode();
|
||||||
|
List<BlogSearch> _searchResults = [];
|
||||||
|
String _searchQuery = '';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
@@ -20,53 +29,145 @@ class _HomePageState extends State<HomePage> {
|
|||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_pageController.dispose();
|
_pageController.dispose();
|
||||||
|
_searchController.dispose();
|
||||||
|
_searchFocusNode.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _startSearch() {
|
||||||
|
setState(() {
|
||||||
|
_isSearching = true;
|
||||||
|
});
|
||||||
|
Future.delayed(Duration(milliseconds: 100), () {
|
||||||
|
_searchFocusNode.requestFocus();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _exitSearch() {
|
||||||
|
setState(() {
|
||||||
|
_isSearching = false;
|
||||||
|
_searchController.clear();
|
||||||
|
_searchResults.clear();
|
||||||
|
_searchQuery = '';
|
||||||
|
});
|
||||||
|
FocusScope.of(context).unfocus();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _clearSearch() {
|
||||||
|
_searchController.clear();
|
||||||
|
setState(() {
|
||||||
|
_searchResults.clear();
|
||||||
|
_searchQuery = '';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onSearchTextChanged(String query) async {
|
||||||
|
setState(() {
|
||||||
|
_searchQuery = query;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (query.isEmpty) {
|
||||||
|
setState(() {
|
||||||
|
_searchResults.clear();
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final result = await searchBlog(query);
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_searchResults = result;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildLeading() {
|
||||||
|
if (_isSearching) {
|
||||||
|
return IconButton(
|
||||||
|
icon: Icon(Icons.arrow_back, color: Colors.white),
|
||||||
|
onPressed: _exitSearch,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Builder(
|
||||||
|
builder:
|
||||||
|
(context) => IconButton(
|
||||||
|
icon: Icon(Icons.menu, color: Colors.white),
|
||||||
|
onPressed: () => Scaffold.of(context).openDrawer(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildAppBarTitle() {
|
||||||
|
if (_isSearching) {
|
||||||
|
return buildSearchField(
|
||||||
|
controller: _searchController,
|
||||||
|
focusNode: _searchFocusNode,
|
||||||
|
onChanged: _onSearchTextChanged,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Text(
|
||||||
|
pages[_currentPageIndex].title,
|
||||||
|
style: TextStyle(color: Colors.white),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Widget> _buildAppBarActions() {
|
||||||
|
if (_isSearching) {
|
||||||
|
return [
|
||||||
|
if (_searchController.text.isNotEmpty)
|
||||||
|
IconButton(
|
||||||
|
icon: Icon(Icons.clear, color: Colors.white),
|
||||||
|
onPressed: _clearSearch,
|
||||||
|
),
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
return [
|
||||||
|
IconButton(
|
||||||
|
icon: Icon(Icons.search, color: Colors.white),
|
||||||
|
onPressed: _startSearch,
|
||||||
|
),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Widget _buildPageView() {
|
Widget _buildPageView() {
|
||||||
|
return _isSearching ? _buildSearchResults() : _buildNormalPageView();
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildNormalPageView() {
|
||||||
return PageView(
|
return PageView(
|
||||||
controller: _pageController,
|
controller: _pageController,
|
||||||
onPageChanged: (index) {
|
onPageChanged: (index) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_currentPageIndex = index;
|
_currentPageIndex = index;
|
||||||
|
if (_isSearching) {
|
||||||
|
_exitSearch();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
children: pageWidgets,
|
children: pageWidgets,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
Widget _buildSearchResults() {
|
||||||
Widget build(BuildContext context) {
|
if (_searchQuery.isEmpty) {
|
||||||
return Scaffold(
|
return buildSearchQueryEmpty();
|
||||||
appBar: AppBar(
|
}
|
||||||
title: Text(
|
|
||||||
pages[_currentPageIndex].title,
|
if (_searchResults.isEmpty) {
|
||||||
style: TextStyle(color: Colors.white),
|
return buildSearchResultEmpty(_searchQuery);
|
||||||
),
|
}
|
||||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
|
||||||
elevation: 0,
|
return buildSearchResultList(context, _searchResults);
|
||||||
leading: Builder(
|
|
||||||
builder:
|
|
||||||
(context) => IconButton(
|
|
||||||
icon: Icon(Icons.menu, color: Colors.white),
|
|
||||||
onPressed: () => Scaffold.of(context).openDrawer(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
actions: [
|
|
||||||
IconButton(
|
|
||||||
icon: Icon(Icons.search, color: Colors.white),
|
|
||||||
onPressed: () {},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
drawer: _buildDrawer(),
|
|
||||||
body: Padding(padding: EdgeInsets.all(10), child: _buildPageView()),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onTapDrawerItem(int index) {
|
void onTapDrawerItem(int index) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_currentPageIndex = index;
|
_currentPageIndex = index;
|
||||||
|
if (_isSearching) {
|
||||||
|
_exitSearch();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
_pageController.animateToPage(
|
_pageController.animateToPage(
|
||||||
index,
|
index,
|
||||||
@@ -99,13 +200,26 @@ class _HomePageState extends State<HomePage> {
|
|||||||
color: Colors.white,
|
color: Colors.white,
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
// 抽屉头部
|
|
||||||
buildDrawerHeader(context),
|
buildDrawerHeader(context),
|
||||||
// 菜单项列表
|
|
||||||
Expanded(child: _buildDrawerBody()),
|
Expanded(child: _buildDrawerBody()),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: _buildAppBarTitle(),
|
||||||
|
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||||
|
elevation: 0,
|
||||||
|
leading: _buildLeading(),
|
||||||
|
actions: _buildAppBarActions(),
|
||||||
|
),
|
||||||
|
drawer: _buildDrawer(),
|
||||||
|
body: Padding(padding: EdgeInsets.all(10), child: _buildPageView()),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,122 +0,0 @@
|
|||||||
import 'package:blog_app/utils/http_utils.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:markdown_widget/config/toc.dart';
|
|
||||||
import 'package:markdown_widget/widget/markdown.dart';
|
|
||||||
|
|
||||||
class MarkdownPage extends StatefulWidget {
|
|
||||||
const MarkdownPage({super.key});
|
|
||||||
|
|
||||||
@override
|
|
||||||
State<MarkdownPage> createState() => _MarkdownPageState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _MarkdownPageState extends State<MarkdownPage> {
|
|
||||||
final tocController = TocController();
|
|
||||||
bool _showToc = false;
|
|
||||||
late String markdownData = '';
|
|
||||||
|
|
||||||
Widget _buildTocPanel() => AnimatedOpacity(
|
|
||||||
opacity: _showToc ? 1.0 : 0.0,
|
|
||||||
duration: const Duration(milliseconds: 300),
|
|
||||||
child: Visibility(
|
|
||||||
visible: _showToc,
|
|
||||||
child: Align(
|
|
||||||
alignment: Alignment.bottomRight,
|
|
||||||
child: Container(
|
|
||||||
width: 250,
|
|
||||||
height: 400,
|
|
||||||
margin: const EdgeInsets.only(bottom: 60, right: 60),
|
|
||||||
// 调整位置在按钮左侧
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: Colors.white,
|
|
||||||
borderRadius: BorderRadius.circular(12),
|
|
||||||
boxShadow: [
|
|
||||||
BoxShadow(
|
|
||||||
color: Colors.black.withOpacity(0.2),
|
|
||||||
blurRadius: 8,
|
|
||||||
offset: const Offset(0, 4),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
border: Border.all(color: Colors.grey[300]!),
|
|
||||||
),
|
|
||||||
child: Column(
|
|
||||||
children: [
|
|
||||||
// 标题栏
|
|
||||||
Container(
|
|
||||||
padding: const EdgeInsets.all(12),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: Colors.grey[100],
|
|
||||||
borderRadius: const BorderRadius.only(
|
|
||||||
topLeft: Radius.circular(12),
|
|
||||||
topRight: Radius.circular(12),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
child: Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
||||||
children: [
|
|
||||||
const Text(
|
|
||||||
'目录',
|
|
||||||
style: TextStyle(
|
|
||||||
fontWeight: FontWeight.bold,
|
|
||||||
fontSize: 16,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
IconButton(
|
|
||||||
icon: const Icon(Icons.close, size: 20),
|
|
||||||
onPressed: () => setState(() => _showToc = false),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
Expanded(child: TocWidget(controller: tocController)),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
Widget buildMarkdown() =>
|
|
||||||
MarkdownWidget(data: markdownData, tocController: tocController);
|
|
||||||
|
|
||||||
@override
|
|
||||||
void initState() {
|
|
||||||
super.initState();
|
|
||||||
// 初始加载数据
|
|
||||||
_loadData();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _loadData() async {
|
|
||||||
final result = await HttpUtil().get("/condition");
|
|
||||||
|
|
||||||
setState(() {
|
|
||||||
markdownData = result[0]['content'];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return Scaffold(
|
|
||||||
appBar: AppBar(title: const Text('文档')),
|
|
||||||
body: Padding(
|
|
||||||
padding: EdgeInsets.all(16),
|
|
||||||
child: Stack(
|
|
||||||
children: [
|
|
||||||
// 主内容
|
|
||||||
buildMarkdown(),
|
|
||||||
|
|
||||||
// 悬浮TOC面板
|
|
||||||
_buildTocPanel(),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
floatingActionButton: FloatingActionButton(
|
|
||||||
onPressed: () => setState(() => _showToc = !_showToc),
|
|
||||||
child: Icon(_showToc ? Icons.close : Icons.list),
|
|
||||||
mini: true,
|
|
||||||
backgroundColor:
|
|
||||||
_showToc ? Colors.grey : Theme.of(context).primaryColor,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import 'package:blog_app/apis/blog.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:blog_app/models/blog.dart';
|
import 'package:blog_app/provider/blog.dart';
|
||||||
import 'package:blog_app/models/common.dart';
|
import 'package:blog_app/widget/blog.dart';
|
||||||
import 'package:blog_app/widget/chart.dart';
|
import 'package:blog_app/widget/chart.dart';
|
||||||
import 'package:blog_app/widget/common.dart';
|
import 'package:blog_app/widget/common.dart';
|
||||||
import 'package:blog_app/widget/year_selector.dart';
|
import 'package:blog_app/widget/year_selector.dart';
|
||||||
@@ -16,50 +16,18 @@ class StatsPage extends StatefulWidget {
|
|||||||
class StatsPageState extends State<StatsPage> {
|
class StatsPageState extends State<StatsPage> {
|
||||||
final double chartHeight = 400;
|
final double chartHeight = 400;
|
||||||
final double statsHeight = 120;
|
final double statsHeight = 120;
|
||||||
int currentYear = DateTime.now().year;
|
|
||||||
|
|
||||||
late List<ChartData> approvedStats = [];
|
|
||||||
late List<ChartData> visitStats = [];
|
|
||||||
late List<ChartData> visitRankStats = [];
|
|
||||||
late List<ChartData> categoryStats = [];
|
|
||||||
late List<ChartData> readRankStats = [];
|
|
||||||
late BlogStats overviewStats = BlogStats(
|
|
||||||
blogCount: 0,
|
|
||||||
categoryCount: 0,
|
|
||||||
greatCount: 0,
|
|
||||||
visitCount: 0,
|
|
||||||
wordCount: 0,
|
|
||||||
);
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_loadStatsList();
|
|
||||||
|
// 初始化加载数据
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<BlogProvider>().refreshBlogStats();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _loadStatsList() async {
|
Widget _buildApprovedStats(BlogProvider provider) {
|
||||||
try {
|
|
||||||
final approvedResult = await queryBlogApprovedStatsApi(currentYear);
|
|
||||||
final visitResult = await queryBlogVisitStatsApi(currentYear);
|
|
||||||
final visitRankResult = await queryBlogVisitRankStatsApi();
|
|
||||||
final categoryResult = await queryBlogCategoryStatsApi();
|
|
||||||
final readRankResult = await queryBlogReadRankStatsApi();
|
|
||||||
final overviewResult = await queryBlogOverviewStatsApi();
|
|
||||||
|
|
||||||
setState(() {
|
|
||||||
approvedStats = approvedResult;
|
|
||||||
visitStats = visitResult;
|
|
||||||
visitRankStats = visitRankResult;
|
|
||||||
categoryStats = categoryResult;
|
|
||||||
readRankStats = readRankResult;
|
|
||||||
overviewStats = overviewResult;
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
throw Exception('获取统计数据失败: $e');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _buildApprovedStats(BuildContext context) {
|
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
buildChartTitle(context, '每月博客发布统计', Icons.show_chart),
|
buildChartTitle(context, '每月博客发布统计', Icons.show_chart),
|
||||||
@@ -72,14 +40,14 @@ class StatsPageState extends State<StatsPage> {
|
|||||||
xAxisName: '月份',
|
xAxisName: '月份',
|
||||||
yAxisName: '数量',
|
yAxisName: '数量',
|
||||||
unit: '篇',
|
unit: '篇',
|
||||||
data: approvedStats,
|
data: provider.approvedStats,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildVisitStats(BuildContext context) {
|
Widget _buildVisitStats(BlogProvider provider) {
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
buildChartTitle(context, '每月博客访问统计', Icons.show_chart),
|
buildChartTitle(context, '每月博客访问统计', Icons.show_chart),
|
||||||
@@ -92,14 +60,14 @@ class StatsPageState extends State<StatsPage> {
|
|||||||
xAxisName: '月份',
|
xAxisName: '月份',
|
||||||
yAxisName: '访问量',
|
yAxisName: '访问量',
|
||||||
unit: '人次',
|
unit: '人次',
|
||||||
data: visitStats,
|
data: provider.visitStats,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildVisitRankStats(BuildContext context) {
|
Widget _buildVisitRankStats(BlogProvider provider) {
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
buildChartTitle(context, '博客访问数量排行', Icons.bar_chart),
|
buildChartTitle(context, '博客访问数量排行', Icons.bar_chart),
|
||||||
@@ -112,14 +80,14 @@ class StatsPageState extends State<StatsPage> {
|
|||||||
xAxisName: '名称',
|
xAxisName: '名称',
|
||||||
yAxisName: '访问量',
|
yAxisName: '访问量',
|
||||||
unit: '人次',
|
unit: '人次',
|
||||||
data: visitRankStats,
|
data: provider.visitRankStats,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildCategoryStats(BuildContext context) {
|
Widget _buildCategoryStats(BlogProvider provider) {
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
buildChartTitle(context, '博客类别统计', Icons.pie_chart),
|
buildChartTitle(context, '博客类别统计', Icons.pie_chart),
|
||||||
@@ -127,13 +95,17 @@ class StatsPageState extends State<StatsPage> {
|
|||||||
buildChartDivider(context),
|
buildChartDivider(context),
|
||||||
const SizedBox(height: 3),
|
const SizedBox(height: 3),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: pieChart(context: context, unit: '篇', data: categoryStats),
|
child: pieChart(
|
||||||
|
context: context,
|
||||||
|
unit: '篇',
|
||||||
|
data: provider.categoryStats,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildReadRankStats(BuildContext context) {
|
Widget _buildReadRankStats(BlogProvider provider) {
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
buildChartTitle(context, '博客阅读时长排行', Icons.bar_chart),
|
buildChartTitle(context, '博客阅读时长排行', Icons.bar_chart),
|
||||||
@@ -146,64 +118,14 @@ class StatsPageState extends State<StatsPage> {
|
|||||||
xAxisName: '名称',
|
xAxisName: '名称',
|
||||||
yAxisName: '时长',
|
yAxisName: '时长',
|
||||||
unit: '分钟',
|
unit: '分钟',
|
||||||
data: readRankStats,
|
data: provider.readRankStats,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget buildStatsCard({
|
Widget _buildBlogStatsGrid(BlogProvider provider) {
|
||||||
required BuildContext context,
|
|
||||||
required String title,
|
|
||||||
required int count,
|
|
||||||
required String unit,
|
|
||||||
}) {
|
|
||||||
return buildCard(
|
|
||||||
context: context,
|
|
||||||
child: Column(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
|
||||||
children: [
|
|
||||||
Text(
|
|
||||||
title,
|
|
||||||
style: TextStyle(
|
|
||||||
fontSize: 16,
|
|
||||||
color: Colors.grey.shade600,
|
|
||||||
fontWeight: FontWeight.w500,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 12),
|
|
||||||
Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.end,
|
|
||||||
children: [
|
|
||||||
Text(
|
|
||||||
count.toString(),
|
|
||||||
style: const TextStyle(
|
|
||||||
fontSize: 28,
|
|
||||||
fontWeight: FontWeight.bold,
|
|
||||||
color: Colors.black87,
|
|
||||||
height: 1.0,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(width: 4),
|
|
||||||
Text(
|
|
||||||
unit,
|
|
||||||
style: TextStyle(
|
|
||||||
fontSize: 14,
|
|
||||||
color: Colors.grey.shade600,
|
|
||||||
fontWeight: FontWeight.w500,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget buildBlogStatsGrid(BuildContext context) {
|
|
||||||
return GridView.count(
|
return GridView.count(
|
||||||
crossAxisCount: 2,
|
crossAxisCount: 2,
|
||||||
shrinkWrap: true,
|
shrinkWrap: true,
|
||||||
@@ -213,92 +135,106 @@ class StatsPageState extends State<StatsPage> {
|
|||||||
buildStatsCard(
|
buildStatsCard(
|
||||||
context: context,
|
context: context,
|
||||||
title: '博客数量',
|
title: '博客数量',
|
||||||
count: overviewStats.blogCount,
|
count: provider.overviewStats.blogCount,
|
||||||
unit: '篇',
|
unit: '篇',
|
||||||
),
|
),
|
||||||
buildStatsCard(
|
buildStatsCard(
|
||||||
context: context,
|
context: context,
|
||||||
title: '分类数量',
|
title: '分类数量',
|
||||||
count: overviewStats.categoryCount,
|
count: provider.overviewStats.categoryCount,
|
||||||
unit: '个',
|
unit: '个',
|
||||||
),
|
),
|
||||||
buildStatsCard(
|
buildStatsCard(
|
||||||
context: context,
|
context: context,
|
||||||
title: '访问量',
|
title: '访问量',
|
||||||
count: overviewStats.visitCount,
|
count: provider.overviewStats.visitCount,
|
||||||
unit: '次',
|
unit: '次',
|
||||||
),
|
),
|
||||||
buildStatsCard(
|
buildStatsCard(
|
||||||
context: context,
|
context: context,
|
||||||
title: '总字数',
|
title: '总字数',
|
||||||
count: overviewStats.wordCount,
|
count: provider.overviewStats.wordCount,
|
||||||
unit: '字',
|
unit: '字',
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget _buildContent(BlogProvider provider) {
|
||||||
|
if (provider.error != null) {
|
||||||
|
return buildErrorInfo(
|
||||||
|
errorInfo: provider.error!,
|
||||||
|
onPressed: () => provider.refreshBlogStats(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Column(
|
||||||
|
children: [
|
||||||
|
YearSelector(
|
||||||
|
initialYear: provider.currentYear,
|
||||||
|
minYear: 2000,
|
||||||
|
maxYear: 2100,
|
||||||
|
onYearChanged: (year) {
|
||||||
|
setState(() {
|
||||||
|
provider.currentYear = year;
|
||||||
|
provider.refreshBlogStats();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
const SizedBox(height: 6),
|
||||||
|
_buildBlogStatsGrid(provider),
|
||||||
|
const SizedBox(height: 6),
|
||||||
|
SizedBox(
|
||||||
|
height: chartHeight,
|
||||||
|
child: buildCard(
|
||||||
|
context: context,
|
||||||
|
child: _buildApprovedStats(provider),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 6),
|
||||||
|
SizedBox(
|
||||||
|
height: chartHeight,
|
||||||
|
child: buildCard(context: context, child: _buildVisitStats(provider)),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 6),
|
||||||
|
SizedBox(
|
||||||
|
height: chartHeight,
|
||||||
|
child: buildCard(
|
||||||
|
context: context,
|
||||||
|
child: _buildVisitRankStats(provider),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 6),
|
||||||
|
SizedBox(
|
||||||
|
height: chartHeight,
|
||||||
|
child: buildCard(
|
||||||
|
context: context,
|
||||||
|
child: _buildCategoryStats(provider),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 6),
|
||||||
|
SizedBox(
|
||||||
|
height: chartHeight,
|
||||||
|
child: buildCard(
|
||||||
|
context: context,
|
||||||
|
child: _buildReadRankStats(provider),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return SingleChildScrollView(
|
final blogProvider = context.watch<BlogProvider>();
|
||||||
child: Column(
|
|
||||||
children: [
|
return Stack(
|
||||||
YearSelector(
|
children: [
|
||||||
initialYear: DateTime.now().year,
|
if (blogProvider.isLoading)
|
||||||
minYear: 2000,
|
buildLoadingIndicator()
|
||||||
maxYear: 2100,
|
else
|
||||||
onYearChanged:
|
SingleChildScrollView(child: _buildContent(blogProvider))
|
||||||
(year) => {
|
]
|
||||||
setState(() {
|
|
||||||
currentYear = year;
|
|
||||||
_loadStatsList();
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
SizedBox(height: 6),
|
|
||||||
buildBlogStatsGrid(context),
|
|
||||||
SizedBox(height: 6),
|
|
||||||
SizedBox(
|
|
||||||
height: chartHeight,
|
|
||||||
child: buildCard(
|
|
||||||
context: context,
|
|
||||||
child: _buildApprovedStats(context),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
SizedBox(height: 6),
|
|
||||||
SizedBox(
|
|
||||||
height: chartHeight,
|
|
||||||
child: buildCard(
|
|
||||||
context: context,
|
|
||||||
child: _buildVisitStats(context),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
SizedBox(height: 6),
|
|
||||||
SizedBox(
|
|
||||||
height: chartHeight,
|
|
||||||
child: buildCard(
|
|
||||||
context: context,
|
|
||||||
child: _buildVisitRankStats(context),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
SizedBox(height: 6),
|
|
||||||
SizedBox(
|
|
||||||
height: chartHeight,
|
|
||||||
child: buildCard(
|
|
||||||
context: context,
|
|
||||||
child: _buildCategoryStats(context),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
SizedBox(height: 6),
|
|
||||||
SizedBox(
|
|
||||||
height: chartHeight,
|
|
||||||
child: buildCard(
|
|
||||||
context: context,
|
|
||||||
child: _buildReadRankStats(context),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'package:blog_app/apis/blog.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:blog_app/models/blog.dart';
|
import 'package:blog_app/provider/blog.dart';
|
||||||
import 'package:blog_app/widget/blog.dart';
|
import 'package:blog_app/widget/blog.dart';
|
||||||
|
import 'package:blog_app/widget/common.dart';
|
||||||
import 'package:blog_app/widget/year_selector.dart';
|
import 'package:blog_app/widget/year_selector.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:timelines_plus/timelines_plus.dart';
|
import 'package:timelines_plus/timelines_plus.dart';
|
||||||
@@ -9,48 +10,27 @@ class TimelinePage extends StatefulWidget {
|
|||||||
const TimelinePage({super.key});
|
const TimelinePage({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<TimelinePage> createState() => _TimelinePageState();
|
State<TimelinePage> createState() => TimelinePageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _TimelinePageState extends State<TimelinePage> {
|
class TimelinePageState extends State<TimelinePage> {
|
||||||
late List<Blog> blogs = [];
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_loadBlogList();
|
// 初始化加载数据
|
||||||
}
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<BlogProvider>().refreshBlogByYear();
|
||||||
Future<void> _loadBlogList() async {
|
|
||||||
try {
|
|
||||||
final result = await queryBlogByConditionApi(
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
DateTime.now().year,
|
|
||||||
);
|
|
||||||
setState(() {
|
|
||||||
blogs = result;
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
throw Exception('获取博客列表失败: $e');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> refreshRecord(int year) async {
|
|
||||||
final result = await queryBlogByConditionApi(null, null, year);
|
|
||||||
setState(() {
|
|
||||||
blogs = result;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildBlogList() {
|
Widget _buildBlogList(BlogProvider provider) {
|
||||||
final colors = Theme.of(context).colorScheme;
|
final colors = Theme.of(context).colorScheme;
|
||||||
|
|
||||||
return Timeline.tileBuilder(
|
return Timeline.tileBuilder(
|
||||||
theme: TimelineThemeData(nodePosition: 0, indicatorPosition: 0),
|
theme: TimelineThemeData(nodePosition: 0, indicatorPosition: 0),
|
||||||
padding: EdgeInsets.all(6),
|
padding: EdgeInsets.all(6),
|
||||||
builder: TimelineTileBuilder.connected(
|
builder: TimelineTileBuilder.connected(
|
||||||
itemCount: blogs.length,
|
itemCount: provider.blogList.length,
|
||||||
connectorBuilder:
|
connectorBuilder:
|
||||||
(context, index, type) =>
|
(context, index, type) =>
|
||||||
Connector.solidLine(thickness: 2, color: colors.primary),
|
Connector.solidLine(thickness: 2, color: colors.primary),
|
||||||
@@ -58,14 +38,24 @@ class _TimelinePageState extends State<TimelinePage> {
|
|||||||
return Indicator.dot(size: 12.0, color: colors.primary);
|
return Indicator.dot(size: 12.0, color: colors.primary);
|
||||||
},
|
},
|
||||||
contentsBuilder: (context, index) {
|
contentsBuilder: (context, index) {
|
||||||
return buildBlogListItem(context, blogs[index], index + 1);
|
final blog = provider.blogList[index];
|
||||||
|
return InkWell(
|
||||||
|
onTap: () => navigatorToBlogDetail(context, blog.id),
|
||||||
|
child: buildBlogListItem(context, blog, index + 1),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
Widget _buildContent(BlogProvider provider) {
|
||||||
Widget build(BuildContext context) {
|
if (provider.error != null) {
|
||||||
|
return buildErrorInfo(
|
||||||
|
errorInfo: provider.error!,
|
||||||
|
onPressed: () => provider.refreshBlogByYear(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
@@ -73,12 +63,42 @@ class _TimelinePageState extends State<TimelinePage> {
|
|||||||
initialYear: DateTime.now().year,
|
initialYear: DateTime.now().year,
|
||||||
minYear: 2000,
|
minYear: 2000,
|
||||||
maxYear: 2100,
|
maxYear: 2100,
|
||||||
onYearChanged: (year) => refreshRecord(year),
|
onYearChanged: (year) {
|
||||||
|
setState(() {
|
||||||
|
provider.currentYear = year;
|
||||||
|
provider.refreshBlogByYear();
|
||||||
|
});
|
||||||
|
},
|
||||||
),
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
buildListTitle(blogs.length),
|
buildListTitle(provider.blogList.length),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
Expanded(child: blogs.isEmpty ? buildEmpty() : _buildBlogList()),
|
Expanded(
|
||||||
|
child:
|
||||||
|
provider.blogList.isEmpty
|
||||||
|
? buildEmpty()
|
||||||
|
: _buildBlogList(provider),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final blogProvider = context.watch<BlogProvider>();
|
||||||
|
|
||||||
|
return Column(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: Stack(
|
||||||
|
children: [
|
||||||
|
if (blogProvider.isLoading)
|
||||||
|
buildLoadingIndicator()
|
||||||
|
else
|
||||||
|
_buildContent(blogProvider),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
204
lib/provider/blog.dart
Normal file
204
lib/provider/blog.dart
Normal file
@@ -0,0 +1,204 @@
|
|||||||
|
import 'package:blog_app/models/common.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:blog_app/apis/blog.dart';
|
||||||
|
import 'package:blog_app/models/blog.dart';
|
||||||
|
|
||||||
|
class BlogProvider with ChangeNotifier {
|
||||||
|
List<Blog> blogList = [];
|
||||||
|
List<BlogCategory> categoryList = [];
|
||||||
|
Blog currentBlog = Blog(
|
||||||
|
id: 0,
|
||||||
|
title: '',
|
||||||
|
topValue: 0,
|
||||||
|
isGreat: false,
|
||||||
|
category: '',
|
||||||
|
summary: '',
|
||||||
|
content: '',
|
||||||
|
wordCount: 0,
|
||||||
|
readDuration: 0,
|
||||||
|
visitCount: 0,
|
||||||
|
createTime: '',
|
||||||
|
updateTime: '',
|
||||||
|
);
|
||||||
|
int currentPage = 1;
|
||||||
|
final int pageSize = 5;
|
||||||
|
bool hasMore = true;
|
||||||
|
bool isLoading = false;
|
||||||
|
int currentYear = DateTime.now().year;
|
||||||
|
String? error;
|
||||||
|
|
||||||
|
late List<ChartData> approvedStats = [];
|
||||||
|
late List<ChartData> visitStats = [];
|
||||||
|
late List<ChartData> visitRankStats = [];
|
||||||
|
late List<ChartData> categoryStats = [];
|
||||||
|
late List<ChartData> readRankStats = [];
|
||||||
|
late BlogStats overviewStats = BlogStats(
|
||||||
|
blogCount: 0,
|
||||||
|
categoryCount: 0,
|
||||||
|
greatCount: 0,
|
||||||
|
visitCount: 0,
|
||||||
|
wordCount: 0,
|
||||||
|
);
|
||||||
|
|
||||||
|
Future<void> queryBlogByPage({bool isRefresh = true}) async {
|
||||||
|
if (isLoading) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
isLoading = true;
|
||||||
|
error = null;
|
||||||
|
notifyListeners();
|
||||||
|
|
||||||
|
// 如果是刷新,重置页码
|
||||||
|
if (isRefresh) {
|
||||||
|
currentPage = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
final result = await queryBlogByPageApi(currentPage, pageSize);
|
||||||
|
|
||||||
|
// 更新数据
|
||||||
|
if (isRefresh) {
|
||||||
|
blogList = result.records;
|
||||||
|
} else {
|
||||||
|
blogList.addAll(result.records);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断是否还有更多数据
|
||||||
|
hasMore = result.current < result.pages;
|
||||||
|
if (hasMore) {
|
||||||
|
currentPage++;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = '加载数据失败: $e';
|
||||||
|
debugPrint('加载数据失败: $e');
|
||||||
|
} finally {
|
||||||
|
isLoading = false;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> refreshBlogCategory() async {
|
||||||
|
if (isLoading) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
isLoading = true;
|
||||||
|
error = null;
|
||||||
|
categoryList = [];
|
||||||
|
notifyListeners();
|
||||||
|
|
||||||
|
final result = await queryBlogCategoryApi();
|
||||||
|
categoryList = result;
|
||||||
|
} catch (e) {
|
||||||
|
error = '加载数据失败: $e';
|
||||||
|
debugPrint('加载数据失败: $e');
|
||||||
|
} finally {
|
||||||
|
isLoading = false;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> refreshBlogStats() async {
|
||||||
|
if (isLoading) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
isLoading = true;
|
||||||
|
error = null;
|
||||||
|
notifyListeners();
|
||||||
|
|
||||||
|
final approvedResult = await queryBlogApprovedStatsApi(currentYear);
|
||||||
|
final visitResult = await queryBlogVisitStatsApi(currentYear);
|
||||||
|
final visitRankResult = await queryBlogVisitRankStatsApi();
|
||||||
|
final categoryResult = await queryBlogCategoryStatsApi();
|
||||||
|
final readRankResult = await queryBlogReadRankStatsApi();
|
||||||
|
final overviewResult = await queryBlogOverviewStatsApi();
|
||||||
|
|
||||||
|
approvedStats = approvedResult;
|
||||||
|
visitStats = visitResult;
|
||||||
|
visitRankStats = visitRankResult;
|
||||||
|
categoryStats = categoryResult;
|
||||||
|
readRankStats = readRankResult;
|
||||||
|
overviewStats = overviewResult;
|
||||||
|
} catch (e) {
|
||||||
|
error = '加载数据失败: $e';
|
||||||
|
debugPrint('加载数据失败: $e');
|
||||||
|
} finally {
|
||||||
|
isLoading = false;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> refreshBlogByCategory(String category) async {
|
||||||
|
if (isLoading) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
isLoading = true;
|
||||||
|
error = null;
|
||||||
|
blogList = [];
|
||||||
|
notifyListeners();
|
||||||
|
|
||||||
|
final result = await queryBlogByConditionApi(category, null, null);
|
||||||
|
blogList = result;
|
||||||
|
} catch (e) {
|
||||||
|
error = '加载数据失败: $e';
|
||||||
|
debugPrint('加载数据失败: $e');
|
||||||
|
} finally {
|
||||||
|
isLoading = false;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> refreshBlogByYear() async {
|
||||||
|
if (isLoading) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
isLoading = true;
|
||||||
|
error = null;
|
||||||
|
blogList = [];
|
||||||
|
notifyListeners();
|
||||||
|
|
||||||
|
final result = await queryBlogByConditionApi(null, null, currentYear);
|
||||||
|
blogList = result;
|
||||||
|
} catch (e) {
|
||||||
|
error = '加载数据失败: $e';
|
||||||
|
debugPrint('加载数据失败: $e');
|
||||||
|
} finally {
|
||||||
|
isLoading = false;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> refreshBlogList() async {
|
||||||
|
await queryBlogByPage(isRefresh: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> queryBlogById(int blogId) async {
|
||||||
|
if (isLoading) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
isLoading = true;
|
||||||
|
error = null;
|
||||||
|
notifyListeners();
|
||||||
|
|
||||||
|
final result = await queryBlogByIdApi(blogId);
|
||||||
|
currentBlog = result;
|
||||||
|
} catch (e) {
|
||||||
|
error = '加载数据失败: $e';
|
||||||
|
debugPrint('加载数据失败: $e');
|
||||||
|
} finally {
|
||||||
|
isLoading = false;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载更多
|
||||||
|
Future<void> loadMoreBlogList() async {
|
||||||
|
if (hasMore && !isLoading) {
|
||||||
|
await queryBlogByPage(isRefresh: false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清除错误
|
||||||
|
void clearError() {
|
||||||
|
error = null;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -267,6 +267,56 @@ Widget buildBlogListItem(BuildContext context, Blog blog, int index) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget buildStatsCard({
|
||||||
|
required BuildContext context,
|
||||||
|
required String title,
|
||||||
|
required int count,
|
||||||
|
required String unit,
|
||||||
|
}) {
|
||||||
|
return buildCard(
|
||||||
|
context: context,
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
title,
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 16,
|
||||||
|
color: Colors.grey.shade600,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.end,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
count.toString(),
|
||||||
|
style: const TextStyle(
|
||||||
|
fontSize: 28,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
color: Colors.black87,
|
||||||
|
height: 1.0,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 4),
|
||||||
|
Text(
|
||||||
|
unit,
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 14,
|
||||||
|
color: Colors.grey.shade600,
|
||||||
|
fontWeight: FontWeight.w500,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
void navigatorToBlogDetail(BuildContext context, int blogId) {
|
void navigatorToBlogDetail(BuildContext context, int blogId) {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
|
|||||||
@@ -33,4 +33,23 @@ Widget circleIconButton({
|
|||||||
),
|
),
|
||||||
child: Icon(icon, color: Colors.white),
|
child: Icon(icon, color: Colors.white),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget buildErrorInfo({
|
||||||
|
required String errorInfo,
|
||||||
|
required VoidCallback onPressed,
|
||||||
|
}) {
|
||||||
|
return Center(
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Text('加载失败: $errorInfo'),
|
||||||
|
ElevatedButton(onPressed: onPressed, child: Text('重试')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget buildLoadingIndicator() {
|
||||||
|
return Center(child: CircularProgressIndicator());
|
||||||
|
}
|
||||||
|
|||||||
63
lib/widget/markdown.dart
Normal file
63
lib/widget/markdown.dart
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:markdown_widget/config/toc.dart';
|
||||||
|
import 'package:markdown_widget/widget/markdown.dart';
|
||||||
|
|
||||||
|
Widget buildMarkdown({
|
||||||
|
required TocController tocController,
|
||||||
|
required String data,
|
||||||
|
}) => MarkdownWidget(data: data, tocController: tocController);
|
||||||
|
|
||||||
|
Widget buildTocPanel({
|
||||||
|
required TocController tocController,
|
||||||
|
required bool showToc,
|
||||||
|
}) => Visibility(
|
||||||
|
visible: showToc,
|
||||||
|
child: Align(
|
||||||
|
alignment: Alignment.bottomRight,
|
||||||
|
child: Container(
|
||||||
|
width: 250,
|
||||||
|
height: 400,
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.white,
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
boxShadow: [
|
||||||
|
BoxShadow(
|
||||||
|
color: Colors.black.withAlpha(50),
|
||||||
|
blurRadius: 8,
|
||||||
|
offset: const Offset(0, 4),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
border: Border.all(color: Colors.grey[300]!),
|
||||||
|
),
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
Container(
|
||||||
|
padding: const EdgeInsets.all(12),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.grey[100],
|
||||||
|
borderRadius: const BorderRadius.only(
|
||||||
|
topLeft: Radius.circular(12),
|
||||||
|
topRight: Radius.circular(12),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
const Text(
|
||||||
|
'目录',
|
||||||
|
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Expanded(
|
||||||
|
child: TocWidget(
|
||||||
|
controller: tocController,
|
||||||
|
tocTextStyle: TextStyle(fontSize: 14),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
99
lib/widget/search.dart
Normal file
99
lib/widget/search.dart
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
import 'package:blog_app/models/blog.dart';
|
||||||
|
import 'package:blog_app/pages/blog_detail_page.dart';
|
||||||
|
import 'package:blog_app/widget/blog.dart';
|
||||||
|
import 'package:blog_app/widget/common.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
Widget buildSearchField({
|
||||||
|
required TextEditingController controller,
|
||||||
|
required FocusNode focusNode,
|
||||||
|
required Function(String) onChanged,
|
||||||
|
}) {
|
||||||
|
return TextField(
|
||||||
|
controller: controller,
|
||||||
|
focusNode: focusNode,
|
||||||
|
autofocus: true,
|
||||||
|
style: TextStyle(color: Colors.white, fontSize: 18),
|
||||||
|
cursorColor: Colors.white,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
hintText: '搜索文章内容...',
|
||||||
|
hintStyle: TextStyle(color: Colors.white70),
|
||||||
|
border: InputBorder.none,
|
||||||
|
focusedBorder: InputBorder.none,
|
||||||
|
enabledBorder: InputBorder.none,
|
||||||
|
),
|
||||||
|
onChanged: onChanged,
|
||||||
|
textInputAction: TextInputAction.search,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget buildSearchResultItem(BuildContext context, BlogSearch blog) {
|
||||||
|
return buildCard(
|
||||||
|
context: context,
|
||||||
|
child: ListTile(
|
||||||
|
title: Text(
|
||||||
|
blog.title,
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
color: Theme.of(context).colorScheme.primary,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
subtitle: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
SizedBox(height: 4),
|
||||||
|
Text(
|
||||||
|
blog.content,
|
||||||
|
style: TextStyle(color: Colors.grey[600], fontSize: 14),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
onTap: () => navigatorToBlogDetail(context, blog.id),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget buildSearchResultList(
|
||||||
|
BuildContext context,
|
||||||
|
List<BlogSearch> searchResultList,
|
||||||
|
) {
|
||||||
|
return ListView.builder(
|
||||||
|
itemCount: searchResultList.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
return buildSearchResultItem(context, searchResultList[index]);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget buildSearchQueryEmpty() {
|
||||||
|
return Center(
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Icon(Icons.search, size: 64, color: Colors.grey[400]),
|
||||||
|
SizedBox(height: 16),
|
||||||
|
Text('搜索文章内容', style: TextStyle(color: Colors.grey[600], fontSize: 16)),
|
||||||
|
SizedBox(height: 8),
|
||||||
|
Text('输入标题或内容进行搜索', style: TextStyle(color: Colors.grey[500])),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget buildSearchResultEmpty(String query) {
|
||||||
|
return Center(
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Icon(Icons.search_off, size: 64, color: Colors.grey[400]),
|
||||||
|
SizedBox(height: 16),
|
||||||
|
Text(
|
||||||
|
'没有找到"$query"相关文章',
|
||||||
|
style: TextStyle(color: Colors.grey[600], fontSize: 16),
|
||||||
|
),
|
||||||
|
SizedBox(height: 8),
|
||||||
|
Text('请尝试其他关键词', style: TextStyle(color: Colors.grey[500])),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
16
pubspec.lock
16
pubspec.lock
@@ -456,6 +456,14 @@ packages:
|
|||||||
url: "https://pub.flutter-io.cn"
|
url: "https://pub.flutter-io.cn"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.0"
|
||||||
|
nested:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: nested
|
||||||
|
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.0"
|
||||||
package_config:
|
package_config:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -536,6 +544,14 @@ packages:
|
|||||||
url: "https://pub.flutter-io.cn"
|
url: "https://pub.flutter-io.cn"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.5.2"
|
version: "1.5.2"
|
||||||
|
provider:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: provider
|
||||||
|
sha256: "4e82183fa20e5ca25703ead7e05de9e4cceed1fbd1eadc1ac3cb6f565a09f272"
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "6.1.5+1"
|
||||||
pub_semver:
|
pub_semver:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ dependencies:
|
|||||||
timelines_plus: ^1.0.7
|
timelines_plus: ^1.0.7
|
||||||
syncfusion_localizations: ^30.1.37
|
syncfusion_localizations: ^30.1.37
|
||||||
syncfusion_flutter_charts: ^30.1.41
|
syncfusion_flutter_charts: ^30.1.41
|
||||||
|
provider: ^6.1.1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user