From 594f57e0d3a01ccae1b01570abf79647c884f63d Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 22 Dec 2024 17:37:37 +0800 Subject: [PATCH] :sparkles: Tappable label tags --- lib/router.dart | 7 +++++-- lib/screens/post/post_search.dart | 15 ++++++++++++++- lib/widgets/post/post_item.dart | 18 ++++++++++++++++-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/router.dart b/lib/router.dart index 8047a41..a614d9b 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -77,8 +77,11 @@ final _appRoutes = [ GoRoute( path: '/search', name: 'postSearch', - builder: (context, state) => const AppBackground( - child: PostSearchScreen(), + builder: (context, state) => AppBackground( + child: PostSearchScreen( + initialTags: state.uri.queryParameters['tags']?.split(','), + initialCategories: state.uri.queryParameters['categories']?.split(','), + ), ), ), GoRoute( diff --git a/lib/screens/post/post_search.dart b/lib/screens/post/post_search.dart index 906c469..583228e 100644 --- a/lib/screens/post/post_search.dart +++ b/lib/screens/post/post_search.dart @@ -13,7 +13,10 @@ import 'package:surface/widgets/post/post_tags_field.dart'; import 'package:very_good_infinite_list/very_good_infinite_list.dart'; class PostSearchScreen extends StatefulWidget { - const PostSearchScreen({super.key}); + final Iterable? initialTags; + final Iterable? initialCategories; + + const PostSearchScreen({super.key, this.initialTags, this.initialCategories}); @override State createState() => _PostSearchScreenState(); @@ -31,6 +34,16 @@ class _PostSearchScreenState extends State { String _searchTerm = ''; Duration? _lastTook; + @override + void initState() { + super.initState(); + _searchTags.addAll(widget.initialTags ?? []); + _searchCategories.addAll(widget.initialCategories ?? []); + if (_searchTags.isNotEmpty || _searchCategories.isNotEmpty) { + _fetchPosts(); + } + } + Future _fetchPosts() async { if (_searchTerm.isEmpty && _searchCategories.isEmpty && _searchTags.isEmpty) return; if (_postCount != null && _posts.length >= _postCount!) return; diff --git a/lib/widgets/post/post_item.dart b/lib/widgets/post/post_item.dart index e5af559..6b3bb76 100644 --- a/lib/widgets/post/post_item.dart +++ b/lib/widgets/post/post_item.dart @@ -989,7 +989,14 @@ class _PostTagsList extends StatelessWidget { ), ], ), - onTap: () {}, + onTap: () { + GoRouter.of(context).pushNamed( + 'postSearch', + queryParameters: { + 'categories': ele.alias, + }, + ); + }, ), ) .toList(), @@ -1008,7 +1015,14 @@ class _PostTagsList extends StatelessWidget { Text(ele.alias, style: GoogleFonts.robotoMono()), ], ), - onTap: () {}, + onTap: () { + GoRouter.of(context).pushNamed( + 'postSearch', + queryParameters: { + 'tags': ele.alias, + }, + ); + }, ), ) .toList(),