Skip to content

Commit

Permalink
Add selectable field into SidebarXItem
Browse files Browse the repository at this point in the history
  • Loading branch information
Frezyx committed Apr 7, 2024
1 parent 5c29c30 commit 809b456
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 15 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,11 @@ class ExampleSidebarX extends StatelessWidget {
icon: Icons.people,
label: 'People',
),
const SidebarXItem(
SidebarXItem(
icon: Icons.favorite,
label: 'Favorites',
selectable: false,
onTap: () => _showDisabledAlert(context),
),
const SidebarXItem(
iconWidget: FlutterLogo(size: 20),
Expand All @@ -167,6 +169,18 @@ class ExampleSidebarX extends StatelessWidget {
],
);
}

void _showDisabledAlert(BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'Item disabled for selecting',
style: TextStyle(color: Colors.black),
),
backgroundColor: Colors.white,
),
);
}
}

class _ScreensExample extends StatelessWidget {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/models/sidebarx_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class SidebarXItem {
this.onTap,
this.onLongPress,
this.onSecondaryTap,
this.selectable = true,
}) : assert(
(icon != null || iconWidget != null) &&
(icon == null || iconWidget == null),
Expand All @@ -17,6 +18,7 @@ class SidebarXItem {
final String? label;
final IconData? icon;
final Widget? iconWidget;
final bool selectable;
final Function()? onTap;
final Function()? onLongPress;
final Function()? onSecondaryTap;
Expand Down
10 changes: 7 additions & 3 deletions lib/src/sidebarx_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ class _SidebarXState extends State<SidebarX>

void _onFooterItemSelected(SidebarXItem item, int index) {
item.onTap?.call();
widget.controller.selectIndex(
widget.items.length + widget.footerItems.length - index - 1);
if (item.selectable) {
widget.controller.selectIndex(
widget.items.length + widget.footerItems.length - index - 1);
}
}

void _onFooterItemLongPressSelected(SidebarXItem item, int index) {
Expand All @@ -199,7 +201,9 @@ class _SidebarXState extends State<SidebarX>

void _onItemSelected(SidebarXItem item, int index) {
item.onTap?.call();
widget.controller.selectIndex(index);
if (item.selectable) {
widget.controller.selectIndex(index);
}
}

void _onItemLongPressSelected(SidebarXItem item, int index) {
Expand Down

0 comments on commit 809b456

Please sign in to comment.