Skip to content

Commit

Permalink
feat: added concealBacklayerOnBackButton option (#133)
Browse files Browse the repository at this point in the history
* feat: added `concealBacklayerOnBackButton` option

* not wrapping WillPopScope if opt disabled
  • Loading branch information
daadu authored Sep 6, 2023
1 parent d21cfaa commit f2a1ec0
Showing 1 changed file with 59 additions and 49 deletions.
108 changes: 59 additions & 49 deletions lib/src/scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ class BackdropScaffold extends StatefulWidget {
/// Defaults to `true`.
final bool maintainBackLayerState;

/// Specifiy whether to conceal [backLayer] when back button pressed.
///
/// Default to `true`.
final bool concealBacklayerOnBackButton;

// ------------- PROPERTIES TAKEN OVER FROM SCAFFOLD ------------- //

/// A key to use when building the [Scaffold].
Expand Down Expand Up @@ -331,6 +336,7 @@ class BackdropScaffold extends StatefulWidget {
this.onBackLayerConcealed,
this.onBackLayerRevealed,
this.maintainBackLayerState = true,
this.concealBacklayerOnBackButton = true,
this.scaffoldKey,
this.appBar,
this.floatingActionButton,
Expand Down Expand Up @@ -600,64 +606,68 @@ class BackdropScaffoldState extends State<BackdropScaffold>
);
}

Future<bool> _willPopCallback(BuildContext context) async {
if (isBackLayerRevealed) {
concealBackLayer();
return false;
}
return true;
}

ColorTween _buildBackLayerScrimColorTween() => ColorTween(
begin: Colors.transparent,
end: widget.backLayerScrim,
);

Widget _buildBody(BuildContext context) {
Widget _wrapWillPopScope(BuildContext context, {required Widget child}) {
if (!widget.concealBacklayerOnBackButton) return child;
return WillPopScope(
onWillPop: () => _willPopCallback(context),
child: Scaffold(
key: scaffoldKey,
appBar: widget.appBar,
body: LayoutBuilder(
builder: (context, constraints) {
return Stack(
fit: StackFit.expand,
children: <Widget>[
_buildBackPanel(),
PositionedTransition(
rect: _getPanelAnimation(context, constraints),
child: _buildFrontPanel(context),
),
],
);
},
),
floatingActionButton: widget.floatingActionButton,
floatingActionButtonLocation: widget.floatingActionButtonLocation,
floatingActionButtonAnimator: widget.floatingActionButtonAnimator,
persistentFooterButtons: widget.persistentFooterButtons,
drawer: widget.drawer,
onDrawerChanged: widget.onDrawerChanged,
endDrawer: widget.endDrawer,
onEndDrawerChanged: widget.onEndDrawerChanged,
bottomNavigationBar: widget.bottomNavigationBar,
bottomSheet: widget.bottomSheet,
backgroundColor: widget.backgroundColor,
resizeToAvoidBottomInset: widget.resizeToAvoidBottomInset,
primary: widget.primary,
drawerDragStartBehavior: widget.drawerDragStartBehavior,
extendBody: widget.extendBody,
extendBodyBehindAppBar: widget.extendBodyBehindAppBar,
drawerScrimColor: widget.drawerScrimColor,
drawerEdgeDragWidth: widget.drawerEdgeDragWidth,
drawerEnableOpenDragGesture: widget.drawerEnableOpenDragGesture,
endDrawerEnableOpenDragGesture: widget.endDrawerEnableOpenDragGesture,
restorationId: widget.restorationId,
),
onWillPop: () async {
if (isBackLayerRevealed) {
concealBackLayer();
return false;
}
return true;
},
child: child,
);
}

Widget _buildBody(BuildContext context) => _wrapWillPopScope(
context,
child: Scaffold(
key: scaffoldKey,
appBar: widget.appBar,
body: LayoutBuilder(
builder: (context, constraints) {
return Stack(
fit: StackFit.expand,
children: <Widget>[
_buildBackPanel(),
PositionedTransition(
rect: _getPanelAnimation(context, constraints),
child: _buildFrontPanel(context),
),
],
);
},
),
floatingActionButton: widget.floatingActionButton,
floatingActionButtonLocation: widget.floatingActionButtonLocation,
floatingActionButtonAnimator: widget.floatingActionButtonAnimator,
persistentFooterButtons: widget.persistentFooterButtons,
drawer: widget.drawer,
onDrawerChanged: widget.onDrawerChanged,
endDrawer: widget.endDrawer,
onEndDrawerChanged: widget.onEndDrawerChanged,
bottomNavigationBar: widget.bottomNavigationBar,
bottomSheet: widget.bottomSheet,
backgroundColor: widget.backgroundColor,
resizeToAvoidBottomInset: widget.resizeToAvoidBottomInset,
primary: widget.primary,
drawerDragStartBehavior: widget.drawerDragStartBehavior,
extendBody: widget.extendBody,
extendBodyBehindAppBar: widget.extendBodyBehindAppBar,
drawerScrimColor: widget.drawerScrimColor,
drawerEdgeDragWidth: widget.drawerEdgeDragWidth,
drawerEnableOpenDragGesture: widget.drawerEnableOpenDragGesture,
endDrawerEnableOpenDragGesture: widget.endDrawerEnableOpenDragGesture,
restorationId: widget.restorationId,
),
);

Container _buildBackLayerScrim() => Container(
color: _backLayerScrimColorTween.evaluate(animationController),
height: _backPanelHeight);
Expand Down

0 comments on commit f2a1ec0

Please sign in to comment.