-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add categories for recent items, make recent category titles smaller and italic to visually separate them from original categories. Recent items are collapsed by default to reduce the clutter for long lists.
- Loading branch information
Showing
3 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
kitchenowl/lib/widgets/home_page/sliver_category_recent_item_grid_list.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:kitchenowl/kitchenowl.dart'; | ||
import 'package:kitchenowl/models/category.dart'; | ||
import 'package:kitchenowl/models/household.dart'; | ||
import 'package:kitchenowl/models/item.dart'; | ||
import 'package:kitchenowl/models/shoppinglist.dart'; | ||
import 'package:sliver_tools/sliver_tools.dart'; | ||
import 'package:kitchenowl/widgets/home_page/sliver_category_item_grid_list.dart'; | ||
|
||
class SliverCategoryRecentItemGridList<T extends Item> extends StatefulWidget { | ||
final String name; | ||
final Duration animationDuration; | ||
|
||
// SliverItemGridList | ||
final void Function()? onRefresh; | ||
final Nullable<void Function(T)>? onPressed; | ||
final Nullable<void Function(T)>? onLongPressed; | ||
final List<T> items; | ||
final List<Category> categories; // forwarded to item page on long press | ||
final ShoppingList? shoppingList; // forwarded to item page on long press | ||
final Household? | ||
household; // forwarded to item page on long press for offline functionality | ||
final bool Function(T)? selected; | ||
final bool isLoading; | ||
final bool? allRaised; | ||
final bool splitByCategories; | ||
|
||
const SliverCategoryRecentItemGridList({ | ||
super.key, | ||
required this.name, | ||
this.animationDuration = const Duration(milliseconds: 150), | ||
this.onRefresh, | ||
this.onPressed, | ||
this.onLongPressed, | ||
this.items = const [], | ||
this.household, | ||
this.categories = const [], | ||
this.shoppingList, | ||
this.selected, | ||
this.isLoading = false, | ||
this.allRaised, | ||
this.splitByCategories = false, | ||
}); | ||
|
||
@override | ||
State<SliverCategoryRecentItemGridList<T>> createState() => | ||
_SliverCategoryRecentItemGridListState<T>(); | ||
} | ||
|
||
class _SliverCategoryRecentItemGridListState<T extends Item> | ||
extends State<SliverCategoryRecentItemGridList<T>> { | ||
bool isExpanded = false; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
List<Widget> list = []; | ||
|
||
if (widget.splitByCategories) { | ||
for (int i = 0; i < widget.categories.length + 1; i++) { | ||
Category? category = i < widget.categories.length | ||
? widget.categories[i] | ||
: null; | ||
final List<T> items = widget.items | ||
.where((e) => e.category == category) | ||
.toList(); | ||
if (items.isEmpty) continue; | ||
|
||
list.add(SliverCategoryItemGridList( | ||
name: category?.name ?? | ||
AppLocalizations.of(context)!.uncategorized, | ||
items: items, | ||
categories: widget.categories, | ||
shoppingList: widget.shoppingList, | ||
selected: (item) => false, | ||
isLoading: widget.isLoading, | ||
onRefresh: widget.onRefresh, | ||
onPressed: widget.onPressed, | ||
isSubTitle: true | ||
)); | ||
} | ||
} | ||
else | ||
list.add(SliverItemGridList<T>( | ||
onRefresh: widget.onRefresh, | ||
onPressed: widget.onPressed, | ||
onLongPressed: widget.onLongPressed, | ||
items: widget.items, | ||
categories: widget.categories, | ||
shoppingList: widget.shoppingList, | ||
selected: widget.selected, | ||
isLoading: widget.isLoading, | ||
allRaised: widget.allRaised, | ||
)); | ||
|
||
return SliverMainAxisGroup( | ||
slivers: [ | ||
SliverToBoxAdapter( | ||
child: AnimatedPadding( | ||
padding: EdgeInsets.fromLTRB(16, 0, 16, isExpanded ? 8 : 4), | ||
duration: widget.animationDuration, | ||
child: InkWell( | ||
onTap: () => setState(() { | ||
isExpanded = !isExpanded; | ||
}), | ||
child: Row( | ||
crossAxisAlignment: CrossAxisAlignment.end, | ||
children: [ | ||
Expanded( | ||
child: Text( | ||
widget.name, | ||
style: Theme.of(context).textTheme.titleLarge, | ||
), | ||
), | ||
IconButton( | ||
onPressed: () => setState(() { | ||
isExpanded = !isExpanded; | ||
}), | ||
icon: AnimatedRotation( | ||
duration: widget.animationDuration, | ||
turns: isExpanded ? 0 : .25, | ||
child: const Icon(Icons.expand_more_rounded), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
), | ||
SliverAnimatedSwitcher( | ||
duration: widget.animationDuration, | ||
child: !isExpanded | ||
? const SliverToBoxAdapter(child: SizedBox()) | ||
: MultiSliver(children: list), | ||
), | ||
], | ||
); | ||
} | ||
} |