Skip to content

Commit

Permalink
Merge pull request #860 from EnsembleUI/navigate-view-group
Browse files Browse the repository at this point in the history
Add navigate to view group action
  • Loading branch information
vinothvino42 authored Nov 6, 2023
2 parents cfab9d4 + 2dcbeea commit 628e381
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 49 deletions.
22 changes: 22 additions & 0 deletions assets/schema/ensemble_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3148,6 +3148,28 @@
}
}
},
{
"title": "Navigate View Group",
"type": "object",
"required": [
"navigateViewGroup"
],
"properties": {
"navigateViewGroup": {
"type": "object",
"description": "Navigate between the screens in the view group. It is applicable only when inside a ViewGroup",
"required": [
"viewIndex"
],
"properties": {
"viewIndex": {
"type": "integer",
"description": "Enter the index of the screen to navigate"
}
}
}
}
},
{
"title": "Navigate Modal Screen",
"type": "object",
Expand Down
23 changes: 23 additions & 0 deletions lib/framework/action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import 'package:ensemble/framework/extensions.dart';
import 'package:ensemble/framework/keychain_manager.dart';
import 'package:ensemble/framework/permissions_manager.dart';
import 'package:ensemble/framework/scope.dart';
import 'package:ensemble/framework/view/bottom_nav_page_group.dart';
import 'package:ensemble/framework/view/page_group.dart';
import 'package:ensemble/framework/widget/view_util.dart';
import 'package:ensemble/screen_controller.dart';
import 'package:ensemble/util/utils.dart';
Expand Down Expand Up @@ -114,6 +116,24 @@ class NavigateScreenAction extends BaseNavigateScreenAction {
}
}

class NavigateViewGroupAction extends EnsembleAction {
NavigateViewGroupAction({dynamic viewIndex}) : _viewIndex = viewIndex;

final dynamic _viewIndex;

factory NavigateViewGroupAction.from({Map? payload}) {
return NavigateViewGroupAction(viewIndex: payload?['viewIndex']);
}

@override
Future execute(BuildContext context, ScopeManager scopeManager,
{DataContext? dataContext}) {
PageGroupWidget.getPageController(context)!.jumpToPage(_viewIndex);
bottomNavBarNotifier.updatePage(_viewIndex);
return Future.value(null);
}
}

class NavigateModalScreenAction extends BaseNavigateScreenAction {
NavigateModalScreenAction({
super.initiator,
Expand Down Expand Up @@ -816,6 +836,7 @@ class ClearKeychain extends EnsembleAction {
enum ActionType {
invokeAPI,
navigateScreen,
navigateViewGroup,
navigateExternalScreen,
navigateModalScreen,
showBottomModal,
Expand Down Expand Up @@ -907,6 +928,8 @@ abstract class EnsembleAction {
} else if (actionType == ActionType.navigateExternalScreen) {
return NavigateExternalScreen.from(
initiator: initiator, payload: payload);
} else if (actionType == ActionType.navigateViewGroup) {
return NavigateViewGroupAction.from(payload: payload);
} else if (actionType == ActionType.navigateModalScreen) {
return NavigateModalScreenAction.fromYaml(
initiator: initiator, payload: payload);
Expand Down
115 changes: 74 additions & 41 deletions lib/framework/view/bottom_nav_page_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ import 'package:ensemble/util/utils.dart';
import 'package:ensemble/framework/widget/icon.dart' as ensemble;
import 'package:flutter/material.dart';

class BottomNavBarNotifier extends ChangeNotifier {
int _viewIndex = 0;

int get viewIndex => _viewIndex;

void updatePage(int index, {bool isReload = true}) {
_viewIndex = index;
if (isReload) {
notifyListeners();
}
}
}

final bottomNavBarNotifier = BottomNavBarNotifier();

class FABBottomAppBarItem {
FABBottomAppBarItem({
required this.icon,
Expand Down Expand Up @@ -104,6 +119,8 @@ class _BottomNavPageGroupState extends State<BottomNavPageGroup>
floatingAlignment =
FloatingAlignment.values.byName(fabMenuItem!.floatingAlignment);
}

bottomNavBarNotifier.updatePage(widget.selectedPage, isReload: false);
}

@override
Expand Down Expand Up @@ -179,27 +196,33 @@ class _BottomNavPageGroupState extends State<BottomNavPageGroup>
final notchColor = Utils.getColor(widget.menu.styles?['notchColor']) ??
Theme.of(context).scaffoldBackgroundColor;

return Scaffold(
resizeToAvoidBottomInset: true,
backgroundColor: notchColor,
bottomNavigationBar: _buildBottomNavBar(),
floatingActionButtonLocation: floatingAlignment == FloatingAlignment.none
? null
: floatingAlignment.location,
floatingActionButton: _buildFloatingButton(),
body: PageGroupWidget(
scopeManager: widget.scopeManager,
child: widget.menu.reloadView == true
? widget.children[selectedPage]
: BottomNavPageView(
controller: controller,
children: widget.children,
),
return PageGroupWidget(
scopeManager: widget.scopeManager,
pageController: PageController(initialPage: widget.selectedPage),
child: Scaffold(
resizeToAvoidBottomInset: true,
backgroundColor: notchColor,
bottomNavigationBar: _buildBottomNavBar(),
floatingActionButtonLocation:
floatingAlignment == FloatingAlignment.none
? null
: floatingAlignment.location,
floatingActionButton: _buildFloatingButton(),
body: Builder(
builder: (context) {
final controller = PageGroupWidget.getPageController(context);

return BottomNavPageView(
controller: controller ?? PageController(),
children: widget.children,
);
},
),
),
);
}

EnsembleBottomAppBar? _buildBottomNavBar() {
Widget? _buildBottomNavBar() {
List<FABBottomAppBarItem> navItems = [];

final unselectedColor = Utils.getColor(widget.menu.styles?['color']) ??
Expand Down Expand Up @@ -241,31 +264,41 @@ class _BottomNavPageGroupState extends State<BottomNavPageGroup>
);
}

return EnsembleBottomAppBar(
selectedIndex: widget.selectedPage,
backgroundColor: Utils.getColor(widget.menu.styles?['backgroundColor']) ??
Colors.white,
height: Utils.optionalDouble(widget.menu.styles?['height'] ?? 60),
margin: widget.menu.styles?['margin'],
padding: widget.menu.styles?['padding'],
borderRadius: Utils.getBorderRadius(widget.menu.styles?['borderRadius'])
?.getValue(),
color: unselectedColor,
selectedColor: selectedColor,
notchedShape: const CircularNotchedRectangle(),
onTabSelected: (index) {
if (widget.menu.reloadView == true) {
setState(() {
selectedPage = index;
});
} else {
controller.jumpToPage(index);
}
return ListenableBuilder(
listenable: bottomNavBarNotifier,
builder: (context, _) {
final viewIndex = bottomNavBarNotifier.viewIndex;

return EnsembleBottomAppBar(
key: UniqueKey(),
selectedIndex: viewIndex,
backgroundColor:
Utils.getColor(widget.menu.styles?['backgroundColor']) ??
Colors.white,
height: Utils.optionalDouble(widget.menu.styles?['height'] ?? 60),
margin: widget.menu.styles?['margin'],
padding: widget.menu.styles?['padding'],
borderRadius:
Utils.getBorderRadius(widget.menu.styles?['borderRadius'])
?.getValue(),
color: unselectedColor,
selectedColor: selectedColor,
notchedShape: const CircularNotchedRectangle(),
onTabSelected: (index) {
if (widget.menu.reloadView == true) {
setState(() {
selectedPage = index;
});
} else {
PageGroupWidget.getPageController(context)?.jumpToPage(index);
}
},
items: navItems,
isFloating: fabMenuItem != null,
floatingAlignment: floatingAlignment,
floatingMargin: floatingMargin,
);
},
items: navItems,
isFloating: fabMenuItem != null,
floatingAlignment: floatingAlignment,
floatingMargin: floatingMargin,
);
}

Expand Down
19 changes: 13 additions & 6 deletions lib/framework/view/page_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ class PageGroup extends StatefulWidget {
/// We need this because the menu (i.e. drawer) is determined at the PageGroup
/// level, but need to be injected under each child Page to render.
class PageGroupWidget extends DataScopeWidget {
const PageGroupWidget(
{super.key,
required super.scopeManager,
required super.child,
this.navigationDrawer,
this.navigationEndDrawer});
const PageGroupWidget({
super.key,
required super.scopeManager,
required super.child,
this.navigationDrawer,
this.navigationEndDrawer,
this.pageController,
});

final Drawer? navigationDrawer;
final Drawer? navigationEndDrawer;
final PageController? pageController;

static Drawer? getNavigationDrawer(BuildContext context) => context
.dependOnInheritedWidgetOfExactType<PageGroupWidget>()
Expand All @@ -61,6 +64,10 @@ class PageGroupWidget extends DataScopeWidget {
.dependOnInheritedWidgetOfExactType<PageGroupWidget>()
?.navigationEndDrawer;

static PageController? getPageController(BuildContext context) => context
.dependOnInheritedWidgetOfExactType<PageGroupWidget>()
?.pageController;

/// return the ScopeManager which includes the dataContext
/// TODO: have to repeat this function in DataScopeWidget?
static ScopeManager? getScope(BuildContext context) {
Expand Down
2 changes: 0 additions & 2 deletions lib/widget/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ class ImageController extends BoxController {
}

class ImageState extends WidgetState<EnsembleImage> {

@override
Widget buildWidget(BuildContext context) {
String source = widget._controller.source.trim();
Expand Down Expand Up @@ -211,7 +210,6 @@ class ImageState extends WidgetState<EnsembleImage> {
}
return fallbackWidget;
}

}

class EnsembleImageCacheManager {
Expand Down

0 comments on commit 628e381

Please sign in to comment.