Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YaruPageItem: add more flexibility #242

Merged
merged 6 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions example/lib/example_page_items.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,65 @@ final examplePageItems = <YaruPageItem>[
YaruPageItem(
titleBuilder: (context) => YaruPageItemTitle.text('YaruBanner'),
builder: (context) => const BannerPage(),
iconData: YaruIcons.image,
iconBuilder: (context, selected) => selected
? const Icon(YaruIcons.image_filled)
: const Icon(YaruIcons.image),
),
YaruPageItem(
titleBuilder: (context) => YaruPageItemTitle.text('YaruCarousel'),
builder: (_) => const CarouselPage(),
iconData: YaruIcons.refresh,
iconBuilder: (context, selected) => const Icon(YaruIcons.refresh),
),
YaruPageItem(
titleBuilder: (context) => YaruPageItemTitle.text('YaruColorDisk'),
builder: (context) => const ColorDiskPage(),
iconData: YaruIcons.color_select,
iconBuilder: (context, selected) => const Icon(YaruIcons.color_select),
),
YaruPageItem(
titleBuilder: (context) => YaruPageItemTitle.text('YaruDraggable'),
builder: (context) => const DraggablePage(),
iconData: YaruIcons.drag_handle,
iconBuilder: (context, selected) => const Icon(YaruIcons.drag_handle),
),
YaruPageItem(
titleBuilder: (context) => YaruPageItemTitle.text('YaruExpandable'),
iconData: YaruIcons.pan_down,
iconBuilder: (context, selected) => const Icon(YaruIcons.pan_down),
builder: (_) => const ExpandablePage(),
),
YaruPageItem(
titleBuilder: (context) => YaruPageItemTitle.text('YaruProgressIndicator'),
iconData: YaruIcons.download,
iconBuilder: (context, selected) => const Icon(YaruIcons.download),
builder: (_) => const ProgressIndicatorPage(),
),
YaruPageItem(
titleBuilder: (context) => YaruPageItemTitle.text('YaruOptionButton'),
iconData: YaruIcons.settings,
iconBuilder: (context, selected) => const Icon(YaruIcons.settings),
builder: (_) => const YaruPage(children: [OptionButtonPage()]),
),
YaruPageItem(
titleBuilder: (context) => YaruPageItemTitle.text('YaruRoundToggleButton'),
builder: (context) => const RoundToggleButtonPage(),
iconData: YaruIcons.app_grid,
iconBuilder: (context, selected) => const Icon(YaruIcons.app_grid),
),
YaruPageItem(
titleBuilder: (context) => YaruPageItemTitle.text('YaruSection'),
iconData: YaruIcons.window,
iconBuilder: (context, selected) => const Icon(YaruIcons.window),
builder: (_) => const SectionPage(),
),
YaruPageItem(
titleBuilder: (context) =>
YaruPageItemTitle.text('YaruSelectableContainer'),
iconData: YaruIcons.selection,
iconBuilder: (context, selected) => const Icon(YaruIcons.selection),
builder: (_) => const SelectableContainerPage(),
),
YaruPageItem(
titleBuilder: (context) => YaruPageItemTitle.text('YaruTabbedPage'),
builder: (_) => const TabbedPagePage(),
iconData: YaruIcons.tab_new,
iconBuilder: (context, selected) => const Icon(YaruIcons.tab_new),
),
YaruPageItem(
titleBuilder: (context) => YaruPageItemTitle.text('YaruTile'),
iconData: YaruIcons.format_unordered_list,
iconBuilder: (context, selected) =>
const Icon(YaruIcons.format_unordered_list),
builder: (_) => const TilePage(),
itemWidget: const SizedBox(
height: 20,
child: YaruCircularProgressIndicator(strokeWidth: 2),
),
),
];
33 changes: 4 additions & 29 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class Home extends StatefulWidget {
class _HomeState extends State<Home> {
final _filteredItems = <YaruPageItem>[];
bool _compactMode = false;
int _amount = 3;

@override
Widget build(BuildContext context) {
Expand All @@ -44,31 +43,9 @@ class _HomeState extends State<Home> {
onChanged: (v) => setState(() => _compactMode = v),
),
),
if (_compactMode)
YaruTile(
title: const Text('YaruPageItem amount'),
trailing: Row(
children: [
TextButton(
onPressed: () {
if (_amount >= examplePageItems.length) return;
setState(() => _amount++);
},
child: const Icon(YaruIcons.plus),
),
TextButton(
onPressed: () {
if (_amount <= 2) return;
setState(() => _amount--);
},
child: const Icon(YaruIcons.minus),
),
],
),
)
],
),
iconData: YaruIcons.settings,
iconBuilder: (context, selected) => const Icon(YaruIcons.settings),
);

return MaterialApp(
Expand All @@ -78,7 +55,7 @@ class _HomeState extends State<Home> {
theme: context.watch<LightTheme>().value,
darkTheme: context.watch<DarkTheme>().value,
home: _compactMode
? _CompactPage(configItem: configItem, amount: _amount)
? _CompactPage(configItem: configItem)
: YaruMasterDetailPage(
leftPaneWidth: 280,
previousIconData: YaruIcons.go_previous,
Expand All @@ -96,19 +73,17 @@ class _HomeState extends State<Home> {
class _CompactPage extends StatelessWidget {
const _CompactPage({
required this.configItem,
required int amount,
}) : _amount = amount;
});

final YaruPageItem configItem;
final int _amount;

@override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;

return YaruCompactLayout(
extendNavigationRail: width > 1000,
pageItems: [configItem] + examplePageItems.take(_amount).toList(),
pageItems: [configItem] + examplePageItems,
backgroundColor: Theme.of(context).brightness == Brightness.light
? Colors.white
: Theme.of(context).colorScheme.onSurface.withOpacity(0.03),
Expand Down
25 changes: 13 additions & 12 deletions lib/src/pages/layouts/yaru_compact_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,21 @@ class _YaruCompactLayoutState extends State<YaruCompactLayout> {
});
},
labelType: widget.labelType,
destinations: widget.pageItems
.map(
(pageItem) => NavigationRailDestination(
icon: pageItem.itemWidget ??
Icon(pageItem.iconData),
selectedIcon: pageItem.selectedItemWidget ??
pageItem.itemWidget ??
(pageItem.selectedIconData != null
? Icon(pageItem.selectedIconData)
: Icon(pageItem.iconData)),
label: pageItem.titleBuilder(context),
destinations: [
for (int i = 0; i < widget.pageItems.length; i++)
NavigationRailDestination(
icon: widget.pageItems[i].iconBuilder(
context,
i == _index,
),
selectedIcon: widget.pageItems[i].iconBuilder(
context,
i == _index,
),
label:
widget.pageItems[i].titleBuilder(context),
)
.toList(),
],
),
),
),
Expand Down
12 changes: 2 additions & 10 deletions lib/src/pages/layouts/yaru_page_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@ class YaruPageItem {
const YaruPageItem({
required this.titleBuilder,
required this.builder,
required this.iconData,
this.selectedIconData,
this.searchMatches,
this.itemWidget,
this.selectedItemWidget,
required this.iconBuilder,
this.onTap,
});

/// We recommend to use [YaruPageItemTitle] here to avoid line wrap
final WidgetBuilder titleBuilder;

final WidgetBuilder builder;
final IconData iconData;
final Widget? itemWidget;
final Widget? selectedItemWidget;
final IconData? selectedIconData;
final bool Function(String value, BuildContext context)? searchMatches;
final Widget Function(BuildContext context, bool selected) iconBuilder;
final void Function(BuildContext context)? onTap;
}
17 changes: 7 additions & 10 deletions lib/src/pages/layouts/yaru_page_item_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ class YaruPageItemListView extends StatelessWidget {
visualDensity: const VisualDensity(horizontal: -4, vertical: -4),
selected: index == selectedIndex,
title: pages[index].titleBuilder(context),
leading: Icon(pages[index].iconData),
leading:
pages[index].iconBuilder(context, index == selectedIndex),
onTap: () => onTap(index),
)
: _YaruListTile(
selected: index == selectedIndex,
title: pages[index].titleBuilder(context),
iconData: pages[index].iconData,
icon: pages[index].iconBuilder(context, index == selectedIndex),
onTap: () => onTap(index),
),
);
Expand All @@ -69,13 +70,13 @@ class YaruPageItemListView extends StatelessWidget {
class _YaruListTile extends StatelessWidget {
const _YaruListTile({
required this.selected,
this.iconData,
this.icon,
required this.onTap,
required this.title,
});

final bool selected;
final IconData? iconData;
final Widget? icon;
final Function() onTap;
final Widget? title;

Expand All @@ -92,16 +93,12 @@ class _YaruListTile extends StatelessWidget {
child: ListTile(
textColor: Theme.of(context).colorScheme.onSurface,
selectedColor: Theme.of(context).colorScheme.onSurface,
iconColor: Theme.of(context).colorScheme.onSurface.withOpacity(0.8),
visualDensity: const VisualDensity(horizontal: -4, vertical: -4),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(kDefaultButtonRadius)),
),
leading: iconData != null
? Icon(
iconData,
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.8),
)
: null,
leading: icon,
title: title,
selected: selected,
onTap: onTap,
Expand Down