diff --git a/lib/model/xdu_planet/xdu_planet.dart b/lib/model/xdu_planet/xdu_planet.dart index 394a8884..c72ecb42 100644 --- a/lib/model/xdu_planet/xdu_planet.dart +++ b/lib/model/xdu_planet/xdu_planet.dart @@ -96,12 +96,14 @@ class Article { final DateTime time; final String content; final String url; + String? author; Article({ required this.title, required this.time, required this.content, required this.url, + this.author, }); factory Article.fromJson(Map json) => diff --git a/lib/page/xdu_planet/xdu_planet_page.dart b/lib/page/xdu_planet/xdu_planet_page.dart index adf0c888..13c582d7 100644 --- a/lib/page/xdu_planet/xdu_planet_page.dart +++ b/lib/page/xdu_planet/xdu_planet_page.dart @@ -7,11 +7,12 @@ import 'dart:math'; // import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; +import 'package:jiffy/jiffy.dart'; import 'package:url_launcher/url_launcher_string.dart'; import 'package:watermeter/model/xdu_planet/xdu_planet.dart'; import 'package:watermeter/page/public_widget/context_extension.dart'; import 'package:watermeter/page/public_widget/public_widget.dart'; -import 'package:watermeter/page/xdu_planet/person_page.dart'; +import 'package:watermeter/page/xdu_planet/content_page.dart'; import 'package:watermeter/repository/xdu_planet_session.dart'; class XDUPlanetPage extends StatefulWidget { @@ -24,6 +25,7 @@ class XDUPlanetPage extends StatefulWidget { class _XDUPlanetPageState extends State with AutomaticKeepAliveClientMixin { late Future repoList; + String selected = "全部"; @override bool get wantKeepAlive => true; @@ -47,9 +49,18 @@ class _XDUPlanetPageState extends State builder: (context) => AlertDialog( title: const Text("XDU Planet 介绍"), content: const Text( - "本服务提供商是西电开源社区的 Planet 服务,查看我们学校同学们的博客。", + "服务提供者是西电开源社区,用于查看我们学校同学们的博客。\n" + "觉得有趣/有用的话,欢迎点点star哦\n\n\n" + "<(=ω=)>", ), actions: [ + TextButton( + child: const Text("项目首页"), + onPressed: () => launchUrlString( + "https://github.com/xdlinux/planet", + mode: LaunchMode.externalApplication, + ), + ), TextButton( child: const Text("网页版"), onPressed: () => launchUrlString( @@ -69,17 +80,6 @@ class _XDUPlanetPageState extends State builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { try { - // Map data = snapshot.data!.repos; - // List keys = data.keys.toList(); - /* - Widget icon(int index) => CachedNetworkImage( - imageUrl: data[keys[index]]!.favicon, - errorWidget: (context, url, error) => - const Icon(Icons.rss_feed), - width: 32, - height: 32, - ); - */ return Center( child: ConstrainedBox( constraints: BoxConstraints( @@ -89,19 +89,112 @@ class _XDUPlanetPageState extends State sheetMaxWidth - 16, ), ), - child: ListView.builder( - itemCount: snapshot.data?.author.length ?? 0, - itemBuilder: (context, index) => ListTile( - title: Text(snapshot.data!.author[index].name), - onTap: () { - context.pushReplacement( - PersonalPage( - key: ValueKey(snapshot.data!.author[index].name), - person: snapshot.data!.author[index], - ), - ); - }), - ), + child: () { + var articles = snapshot.data!.author + .where((e) => selected == "全部" || e.name == selected) + .map((e) => e.article + .map((f) => Article( + title: f.title, + time: f.time, + content: f.content, + url: f.url, + author: e.name)) + .toList()) + .reduce((a, b) => a + b); + articles.sort((a, b) => b.time.compareTo(a.time)); + return Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + SizedBox( + height: 48, + child: ListView( + scrollDirection: Axis.horizontal, + children: () { + var res = snapshot.data!.author + .map((e) => Padding( + padding: const EdgeInsets.all(8.0), + child: TextButton( + style: TextButton.styleFrom( + backgroundColor: selected == e.name + ? Theme.of(context) + .colorScheme + .primaryContainer + : Theme.of(context) + .colorScheme + .secondaryContainer, + ), + onPressed: () { + setState(() { + selected = e.name; + }); + }, + child: Text( + e.name, + style: TextStyle( + color: selected == e.name + ? Theme.of(context) + .colorScheme + .onPrimaryContainer + : Theme.of(context) + .colorScheme + .onSecondaryContainer), + ), + ))) + .toList(); + res.insert( + 0, + Padding( + padding: const EdgeInsets.all(8.0), + child: TextButton( + style: TextButton.styleFrom( + backgroundColor: selected == "全部" + ? Theme.of(context) + .colorScheme + .primary + : Theme.of(context) + .colorScheme + .secondaryContainer, + ), + onPressed: () { + setState(() { + selected = "全部"; + }); + }, + child: Text( + "全部", + style: TextStyle( + color: selected == "全部" + ? Theme.of(context) + .colorScheme + .onPrimaryContainer + : Theme.of(context) + .colorScheme + .onSecondaryContainer), + ), + ))); + return res; + }(), + ), + ), + Expanded( + child: ListView.builder( + itemCount: articles.length ?? 0, + itemBuilder: (context, index) { + return ListTile( + title: Text(articles[index].title), + subtitle: Text( + "${articles[index].author} ${Jiffy.parseFromDateTime( + articles[index].time, + ).format(pattern: "yyyy年MM月dd日")}"), + onTap: () { + context.pushReplacement(ContentPage( + article: articles[index], + author: articles[index].author!)); + }); + })) + ], + ); + }(), ), ); } catch (e) { @@ -114,7 +207,15 @@ class _XDUPlanetPageState extends State ); } } else { - return const Center(child: CircularProgressIndicator()); + return const Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + CircularProgressIndicator(), + SizedBox(height: 16), + Text('加载中,请稍等 <(=ω=)>'), + ], + )); } }, ),