A design solution to deal with decoupling features from entry-points component like app-bar, sidebar and bottom-navigation-bar.
- bottom navigation bar
- profile - team
- home - team
- The base component that encapsulate the Contribution information.
abstract class IContribution {
String get contributionId;
bool get isEnabled;
}
- A component that extends IContribution and encapsulate specific contribution
abstract class IBottomNavigationBarContribution implements IContribution {
BottomNavigationBarContributionData get state;
Widget view(BuildContext context);
}
- The Component that encapsulate the state of the contribution.
class BottomNavigationBarContributionData {
final String label;
final IconData icon;
const BottomNavigationBarContributionData({
required this.label,
required this.icon,
});
}
- The component that encapsulate the responsibility of registering contributions.
class ContributionRegistry {
List<IContribution> get contributions => [
//add contributions [we can use DI to avoid cycle-dependency reference]
ProfileBottomNavigationBarContribution(),
HomeBottomNavigationBarContribution()
];
}
- The component that encapsulate the responsibility of providing the right contributions to the consumers.
class ContributionManager<T extends IContribution> {
final ContributionRegistry _registry = ContributionRegistry();
List<T> get contributions {
final list = _registry.contributions.whereType<T>();
return list.where((element) => element.isEnabled).toList();
}
}
class ProfileBottomNavigationBarContribution
implements IBottomNavigationBarContribution {
@override
String get contributionId => 'profile-bottom-navigationBar-contribution';
@override
bool get isEnabled => true;
@override
Widget view(BuildContext context) {
return const Text("Profile-Screen");
}
@override
BottomNavigationBarContributionData get state =>
const BottomNavigationBarContributionData(
label: "Profile",
icon: Icons.person,
);
}
class HomeBottomNavigationBarContribution
implements IBottomNavigationBarContribution {
@override
String get contributionId => 'Home-BottomNavigationBar-Contribution';
@override
bool get isEnabled => true;
@override
Widget view(BuildContext context) {
return const Text("Home-Screen");
}
@override
BottomNavigationBarContributionData get state =>
const BottomNavigationBarContributionData(
icon: Icons.home,
label: "Home",
);
}