Skip to content

Commit

Permalink
enhance: dispatch changes when tab changes
Browse files Browse the repository at this point in the history
  • Loading branch information
snehmehta committed Dec 15, 2023
1 parent dacc84c commit 4b32e1b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/ensemble_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,10 @@ class EnsembleAppState extends State<EnsembleApp> with WidgetsBindingObserver {
Widget renderApp(EnsembleConfig config) {
// notify external app once of EnsembleApp loading status
if (widget.onAppLoad != null && !notifiedAppLoad) {
widget.onAppLoad!.call();
notifiedAppLoad = true;
widget.onAppLoad!.call();
notifiedAppLoad = true;
}


StorageManager().setIsPreview(widget.isPreview);

return MaterialApp(
Expand Down
18 changes: 18 additions & 0 deletions lib/layout/tab_bar.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:ensemble/action/haptic_action.dart';
import 'package:ensemble/framework/action.dart';
import 'package:ensemble/framework/bindings.dart';
import 'package:ensemble/framework/error_handling.dart';
import 'package:ensemble/framework/extensions.dart';
import 'package:ensemble/framework/model.dart';
Expand Down Expand Up @@ -55,6 +56,7 @@ abstract class BaseTabBar extends StatefulWidget
@override
Map<String, Function> setters() {
return {
'id': (value) => _controller.id = Utils.optionalString(value),
'tabPosition': (position) =>
_controller.tabPosition = Utils.optionalString(position),
'indicatorSize': (type) =>
Expand Down Expand Up @@ -146,11 +148,27 @@ class TabBarState extends WidgetState<BaseTabBar>
initialIndex: widget._controller.selectedIndex,
length: widget._controller._items.length,
vsync: this);
_tabController.addListener(notifyListener);
super.initState();
}

void notifyListener() {
ScopeManager? scopeManager = ScreenController().getScopeManager(context);
if (widget._controller.selectedIndex == _tabController.index ||
scopeManager == null ||
widget._controller.id == null) return;
scopeManager.dispatch(
ModelChangeEvent(
WidgetBindingSource(widget._controller.id!, property: 'selectedIndex'),
_tabController.index,
bindingScope: scopeManager,
),
);
}

@override
void dispose() {
_tabController.removeListener(notifyListener);
_tabController.dispose();
super.dispose();
}
Expand Down

0 comments on commit 4b32e1b

Please sign in to comment.