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

Provide access to GlobalKey field of tab bar buttons #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions lib/MotionTabBar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MotionTabBar extends StatefulWidget {

final List<String?> labels;
final List<IconData>? icons;
final List<GlobalKey>? buttonKeys;
final bool useSafeArea;
final MotionTabBarController? controller;

Expand All @@ -40,9 +41,11 @@ class MotionTabBar extends StatefulWidget {
this.useSafeArea = true,
this.badges,
this.controller,
this.buttonKeys,
}) : assert(labels.contains(initialSelectedTab)),
assert(icons != null && icons.length == labels.length),
assert((badges != null && badges.length > 0) ? badges.length == labels.length : true);
assert((badges != null && badges.length > 0) ? badges.length == labels.length : true),
assert((buttonKeys != null && buttonKeys.length > 0) ? buttonKeys.length == labels.length : true);

@override
_MotionTabBarState createState() => _MotionTabBarState();
Expand All @@ -59,6 +62,7 @@ class _MotionTabBarState extends State<MotionTabBar> with TickerProviderStateMix

late List<String?> labels;
late Map<String?, IconData> icons;
late Map<String?, GlobalKey> buttonGlobalKeys;

get tabAmount => icons.keys.length;
get index => labels.indexOf(selectedTab);
Expand Down Expand Up @@ -106,7 +110,15 @@ class _MotionTabBarState extends State<MotionTabBar> with TickerProviderStateMix
key: (label) => label,
value: (label) => widget.icons![labels.indexOf(label)],
);

if(widget.buttonKeys!=null && widget.buttonKeys!.isNotEmpty) {
buttonGlobalKeys = Map.fromIterable(
labels,
key: (label) => label,
value: (label) => widget.buttonKeys![labels.indexOf(label)],
);
}else{
buttonGlobalKeys = {};
}
selectedTab = widget.initialSelectedTab;
activeIcon = icons[selectedTab];

Expand Down Expand Up @@ -276,11 +288,13 @@ class _MotionTabBarState extends State<MotionTabBar> with TickerProviderStateMix
bool isRtl = Directionality.of(context).index == 0;
return labels.map((tabLabel) {
IconData? icon = icons[tabLabel];
GlobalKey? buttonKey = buttonGlobalKeys.isEmpty ? null : buttonGlobalKeys[tabLabel];

int selectedIndex = labels.indexWhere((element) => element == tabLabel);
Widget? badge = (widget.badges != null && widget.badges!.length > 0) ? widget.badges![selectedIndex] : null;

return MotionTabItem(
key: buttonKey,
selected: selectedTab == tabLabel,
iconData: icon,
title: tabLabel,
Expand Down
4 changes: 2 additions & 2 deletions lib/MotionTabItem.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class MotionTabItem extends StatefulWidget {
final Color tabIconColor;
final double? tabIconSize;
final Widget? badge;

MotionTabItem({
GlobalKey? key,
required this.title,
required this.selected,
required this.iconData,
Expand All @@ -27,7 +27,7 @@ class MotionTabItem extends StatefulWidget {
required this.callbackFunction,
this.tabIconSize = 24,
this.badge,
});
}) : super(key:key);

@override
_MotionTabItemState createState() => _MotionTabItemState();
Expand Down