Skip to content

Commit

Permalink
added singleton model
Browse files Browse the repository at this point in the history
  • Loading branch information
raj457036 committed Aug 10, 2020
1 parent 3c12e90 commit 50e9ceb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
13 changes: 13 additions & 0 deletions liquid_ui/lib/src/base/src/liquid_state_manager.dart
Expand Up @@ -18,6 +18,7 @@ abstract class LOverlayManager<T extends State<StatefulWidget>> {
}

class LiquidStates {
LOverlayManager _singleton;
final List<LOverlayManager> _modelManagers = [];

void pushModel(LOverlayManager manager) {
Expand All @@ -38,6 +39,18 @@ class LiquidStates {
await _?.close();
return result;
}

Future<void> setSingletonModel(LOverlayManager manager) async {
await _singleton?.close();
_singleton?.entry?.remove();
_singleton = manager;
}

Future<T> removeSingletonModel<T>([T result]) async {
await _singleton?.close();
_singleton?.entry?.remove();
return result;
}
}

class LiquidStateManager extends InheritedWidget {
Expand Down
36 changes: 35 additions & 1 deletion liquid_ui/lib/src/components/src/models.dart
Expand Up @@ -305,6 +305,8 @@ class LModel extends StatelessWidget {

/// A wrapper around [LModel] for showing `entry` and `exit` animation
class LAnimatedModel extends StatefulWidget {
final Duration duration;

final LModel model;

/// Tween Position for showing and hiding [LModel]
Expand All @@ -324,6 +326,7 @@ class LAnimatedModel extends StatefulWidget {
this.positionTween,
this.barrierDismissable,
this.backdropColor,
this.duration = const Duration(milliseconds: 250),
}) : assert(model != null),
super(key: key);

Expand All @@ -347,7 +350,7 @@ class _LAnimatedModelState extends State<LAnimatedModel>
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(milliseconds: 250),
duration: widget.duration ?? Duration(milliseconds: 250),
);
}

Expand Down Expand Up @@ -507,6 +510,7 @@ void showLModel(
Tween<Offset> positionTween,
Color backdropColor,
bool barrierDismissable = true,
Duration animationDuration,
}) {
assert(barrierDismissable != null);
final overlay = Overlay.of(context);
Expand All @@ -518,6 +522,7 @@ void showLModel(
positionTween: positionTween,
barrierDismissable: barrierDismissable,
backdropColor: backdropColor,
duration: animationDuration,
),
);
overlay.insert(model);
Expand All @@ -527,3 +532,32 @@ void showLModel(
);
LiquidStateManager.of(context).pushModel(_manager);
}

void showSingletonLModel(
BuildContext context, {
@required LModel Function(BuildContext context) builder,
Tween<Offset> positionTween,
Color backdropColor,
bool barrierDismissable = true,
Duration animationDuration,
}) {
assert(barrierDismissable != null);
final overlay = Overlay.of(context);
final GlobalKey<_LAnimatedModelState> key = GlobalKey<_LAnimatedModelState>();
final model = OverlayEntry(
builder: (context) => LAnimatedModel(
key: key,
model: builder(context),
positionTween: positionTween,
barrierDismissable: barrierDismissable,
backdropColor: backdropColor,
duration: animationDuration,
),
);
overlay.insert(model);
final _manager = ModelHandler(
model,
key,
);
LiquidStateManager.of(context).setSingletonModel(_manager);
}

0 comments on commit 50e9ceb

Please sign in to comment.