feat(custom-header): add custom category header functionality

This commit is contained in:
markkiepe 2024-04-03 17:15:07 +02:00
parent 65b958a3e7
commit 8a6adbbee3
2 changed files with 31 additions and 15 deletions

View file

@ -7,7 +7,12 @@ import "package:flutter/widgets.dart";
class Category { class Category {
const Category({ const Category({
/// The name of the category. /// The name of the category.
required this.name, this.name,
/// Optional custom title widget for the category. This will be displayed
/// at the top of the category. If set, the text title will be ignored.
/// This will be displayed before the content of the category.
this.customTitle,
/// The content of the category. This can be anything, but is usually /// The content of the category. This can be anything, but is usually
/// a list of widgets. /// a list of widgets.
@ -23,9 +28,13 @@ class Category {
/// ///
/// Default is an empty list. /// Default is an empty list.
this.nestedCategories = const <Category>[], this.nestedCategories = const <Category>[],
}); }) : assert(
name != null || customTitle != null,
"A name or a custom title must be set",
);
final String name; final String? name;
final Widget? customTitle;
final List<Category> nestedCategories; final List<Category> nestedCategories;
final List<Widget> content; final List<Widget> content;
} }

View file

@ -212,8 +212,11 @@ class _CategoryColumnState extends State<_CategoryColumn> {
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
), ),
const SizedBox(width: 8), const SizedBox(width: 8),
if (widget.category.customTitle != null)
widget.category.customTitle!
else
Text( Text(
widget.category.name, widget.category.name!,
style: styleOfCategory, style: styleOfCategory,
), ),
], ],
@ -223,13 +226,17 @@ class _CategoryColumnState extends State<_CategoryColumn> {
Widget _buildNonCollapsibleHeader(TextStyle styleOfCategory) => Widget _buildNonCollapsibleHeader(TextStyle styleOfCategory) =>
widget.headerCentered widget.headerCentered
? Center( ? Center(
child: Text( child: widget.category.customTitle != null
widget.category.name, ? widget.category.customTitle!
: Text(
widget.category.name!,
style: styleOfCategory, style: styleOfCategory,
), ),
) )
: widget.category.customTitle != null
? widget.category.customTitle!
: Text( : Text(
widget.category.name, widget.category.name!,
style: styleOfCategory, style: styleOfCategory,
); );