Skip to content

Commit

Permalink
Add support for a drawer slide animation
Browse files Browse the repository at this point in the history
  • Loading branch information
zbarbuto committed Jan 17, 2025
1 parent 8aa024f commit e22f5e1
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions lib/src/widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AdvancedDrawer extends StatefulWidget {
this.backdrop,
this.openRatio = 0.75,
this.openScale = 0.85,
this.drawerSlideRatio = 0,
this.animationDuration = const Duration(milliseconds: 250),
this.animationCurve,
this.childDecoration,
Expand Down Expand Up @@ -41,6 +42,11 @@ class AdvancedDrawer extends StatefulWidget {
/// Opening ratio.
final double openScale;

/// How far the drawer segment should slide with the content
/// 0 = no slide distance
/// 1 = slide from completely off screen
final double drawerSlideRatio;

/// Animation duration.
final Duration animationDuration;

Expand Down Expand Up @@ -77,6 +83,7 @@ class _AdvancedDrawerState extends State<AdvancedDrawer>
late Animation<double> _drawerScaleAnimation;
late Animation<Offset> _childSlideAnimation;
late Animation<double> _childScaleAnimation;
late Animation<Offset> _drawerSlideAnimation;
late Animation<Decoration> _childDecorationAnimation;

late double _offsetValue;
Expand Down Expand Up @@ -120,15 +127,18 @@ class _AdvancedDrawerState extends State<AdvancedDrawer>
alignment: widget.rtlOpening
? Alignment.centerRight
: Alignment.centerLeft,
child: FractionallySizedBox(
widthFactor: widget.openRatio,
child: ScaleTransition(
scale: _drawerScaleAnimation,
alignment: widget.rtlOpening
? Alignment.centerLeft
: Alignment.centerRight,
child: RepaintBoundary(
child: widget.drawer,
child: SlideTransition(
position: _drawerSlideAnimation,
child: FractionallySizedBox(
widthFactor: widget.openRatio,
child: ScaleTransition(
scale: _drawerScaleAnimation,
alignment: widget.rtlOpening
? Alignment.centerLeft
: Alignment.centerRight,
child: RepaintBoundary(
child: widget.drawer,
),
),
),
),
Expand Down Expand Up @@ -232,6 +242,11 @@ class _AdvancedDrawerState extends State<AdvancedDrawer>
end: 1.0,
).animate(parentAnimation);

_drawerSlideAnimation = Tween<Offset>(
begin: Offset(-widget.drawerSlideRatio, 0),
end: Offset.zero,
).animate(parentAnimation);

_childSlideAnimation = Tween<Offset>(
begin: Offset.zero,
end: Offset(widget.openRatio, 0),
Expand All @@ -252,7 +267,9 @@ class _AdvancedDrawerState extends State<AdvancedDrawer>
// If widget is not mounted do nothing
if (!mounted) return;
// If the value of _controller is visible, forward the animation; otherwise, reverse it
_controller.value.visible ? _animationController.forward() : _animationController.reverse();
_controller.value.visible
? _animationController.forward()
: _animationController.reverse();
}

void _handleDragStart(DragStartDetails details) {
Expand Down

0 comments on commit e22f5e1

Please sign in to comment.