feat(capitalization): add header capitalization

This commit is contained in:
markkiepe 2024-04-04 10:13:30 +02:00
parent 6ddabf3ed7
commit 80a6f97dca
5 changed files with 45 additions and 4 deletions

View file

@ -29,10 +29,11 @@ class MyApp extends StatelessWidget {
}, },
defaultStyle: defaultStyle:
theme.textTheme.titleLarge!.copyWith(color: Colors.green), theme.textTheme.titleLarge!.copyWith(color: Colors.green),
capitalization: CategoryHeaderCapitalization.uppercase,
), ),
content: [ content: [
Category( Category(
name: "Category 1", name: "category 1",
content: [ content: [
const Text("Content 1"), const Text("Content 1"),
Image.network( Image.network(

View file

@ -5,5 +5,6 @@
library flutter_nested_categories; library flutter_nested_categories;
export "src/category.dart"; export "src/category.dart";
export "src/category_header_capitalization.dart";
export "src/category_header_styling.dart"; export "src/category_header_styling.dart";
export "src/category_list.dart"; export "src/category_list.dart";

View file

@ -0,0 +1,16 @@
/// An enum that represents the capitalization of the category header.
/// This is used to determine how the header should be displayed.
/// The header is the name of the category.
enum CategoryHeaderCapitalization {
/// The first letter of the header will be capitalized.
capitalizeFirstLetter,
/// The header will be displayed in all lowercase.
lowercase,
/// The header will be displayed in all uppercase.
uppercase,
/// The header will be displayed as is.
none,
}

View file

@ -1,4 +1,5 @@
import "package:flutter/widgets.dart"; import "package:flutter/widgets.dart";
import "package:flutter_nested_categories/src/category_header_capitalization.dart";
/// This class is used to style the headers of the categories in the list. /// This class is used to style the headers of the categories in the list.
/// The headers are the names of the categories. /// The headers are the names of the categories.
@ -8,11 +9,16 @@ class CategoryHeaderStyling {
/// is not found in the [headerStyles]. /// is not found in the [headerStyles].
required this.defaultStyle, required this.defaultStyle,
/// The capitalization of the headers. This is used to determine how the
/// headers should be displayed.
this.capitalization = CategoryHeaderCapitalization.none,
/// The styles for the headers. The key is the depth of the category. /// The styles for the headers. The key is the depth of the category.
/// The value is the text style for the header. /// The value is the text style for the header.
this.headerStyles = const {}, this.headerStyles = const {},
}); });
final Map<int, TextStyle?> headerStyles; final Map<int, TextStyle?> headerStyles;
final CategoryHeaderCapitalization capitalization;
final TextStyle defaultStyle; final TextStyle defaultStyle;
} }

View file

@ -193,6 +193,23 @@ class _CategoryColumnState extends State<_CategoryColumn> {
); );
} }
String? formatCategoryName() {
var name = widget.category.name;
if (name == null) return null;
switch (widget.headerStyling?.capitalization) {
case CategoryHeaderCapitalization.capitalizeFirstLetter:
return name[0].toUpperCase() + name.substring(1);
case CategoryHeaderCapitalization.lowercase:
return name.toLowerCase();
case CategoryHeaderCapitalization.uppercase:
return name.toUpperCase();
case CategoryHeaderCapitalization.none:
default:
return name;
}
}
Widget _buildCollapsibleHeader(TextStyle styleOfCategory) => InkWell( Widget _buildCollapsibleHeader(TextStyle styleOfCategory) => InkWell(
onTap: () { onTap: () {
setState(() { setState(() {
@ -216,7 +233,7 @@ class _CategoryColumnState extends State<_CategoryColumn> {
widget.category.customTitle! widget.category.customTitle!
else else
Text( Text(
widget.category.name!, formatCategoryName()!,
style: styleOfCategory, style: styleOfCategory,
), ),
], ],
@ -229,14 +246,14 @@ class _CategoryColumnState extends State<_CategoryColumn> {
child: widget.category.customTitle != null child: widget.category.customTitle != null
? widget.category.customTitle! ? widget.category.customTitle!
: Text( : Text(
widget.category.name!, formatCategoryName()!,
style: styleOfCategory, style: styleOfCategory,
), ),
) )
: widget.category.customTitle != null : widget.category.customTitle != null
? widget.category.customTitle! ? widget.category.customTitle!
: Text( : Text(
widget.category.name!, formatCategoryName()!,
style: styleOfCategory, style: styleOfCategory,
); );