From f2a1ec07d0153027b0a5e58288a63f0b21be6607 Mon Sep 17 00:00:00 2001 From: Harsh Bhikadia Date: Wed, 6 Sep 2023 16:20:37 +0530 Subject: [PATCH] feat: added `concealBacklayerOnBackButton` option (#133) * feat: added `concealBacklayerOnBackButton` option * not wrapping WillPopScope if opt disabled --- lib/src/scaffold.dart | 108 +++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 49 deletions(-) diff --git a/lib/src/scaffold.dart b/lib/src/scaffold.dart index 7dbe63c..61a86c6 100644 --- a/lib/src/scaffold.dart +++ b/lib/src/scaffold.dart @@ -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]. @@ -331,6 +336,7 @@ class BackdropScaffold extends StatefulWidget { this.onBackLayerConcealed, this.onBackLayerRevealed, this.maintainBackLayerState = true, + this.concealBacklayerOnBackButton = true, this.scaffoldKey, this.appBar, this.floatingActionButton, @@ -600,64 +606,68 @@ class BackdropScaffoldState extends State ); } - Future _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: [ - _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: [ + _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);