From 25dd895e0d30732f99f96faace1f0c7bdfc6020c Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 22 Feb 2025 14:58:20 +0800 Subject: [PATCH] :lipstick: Optimize category selector --- lib/screens/explore.dart | 47 ++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/screens/explore.dart b/lib/screens/explore.dart index b16e7a2..9bfe077 100644 --- a/lib/screens/explore.dart +++ b/lib/screens/explore.dart @@ -39,6 +39,8 @@ class ExploreScreen extends StatefulWidget { State createState() => _ExploreScreenState(); } +// You know what? I'm not going to make this a global variable. +// Cuz the global key make the selected category not update to child widget when the category is changed. SnPostCategory? _selectedCategory; class _ExploreScreenState extends State with SingleTickerProviderStateMixin { @@ -516,28 +518,41 @@ class _PostCategoryPickerPopup extends StatelessWidget { }, ), const Divider(height: 1), - Wrap( - spacing: 4, - runSpacing: 4, + GridView.count( + crossAxisCount: 4, + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), + childAspectRatio: 1, children: categories .map( - (ele) => ChoiceChip( - avatar: Icon(kCategoryIcons[ele.alias] ?? Symbols.question_mark), - label: Text( - 'postCategory${ele.alias.capitalize()}'.trExists() - ? 'postCategory${ele.alias.capitalize()}'.tr() - : ele.name, - ), - selected: ele == selected, - onSelected: (value) { - if (value) { - Navigator.pop(context, ele); - } + (ele) => InkWell( + onTap: () { + _selectedCategory = ele; + Navigator.pop(context, ele); }, + child: Column( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + mainAxisSize: MainAxisSize.min, + children: [ + Icon( + kCategoryIcons[ele.alias] ?? Symbols.question_mark, + color: selected == ele ? Theme.of(context).colorScheme.primary : null, + ), + const Gap(4), + Text( + 'postCategory${ele.alias.capitalize()}'.trExists() + ? 'postCategory${ele.alias.capitalize()}'.tr() + : ele.name, + ) + .textStyle(Theme.of(context).textTheme.titleMedium!) + .textColor(selected == ele ? Theme.of(context).colorScheme.primary : null), + ], + ), ), ) .toList(), - ).padding(horizontal: 12), + ), ], ); }