diff --git a/lib/src/helper/utils.dart b/lib/src/helper/utils.dart index f8f870d..52acd6c 100644 --- a/lib/src/helper/utils.dart +++ b/lib/src/helper/utils.dart @@ -1,5 +1,6 @@ import 'dart:ui'; +import 'package:flutter/foundation.dart'; import 'package:flutter_slider_drawer/flutter_slider_drawer.dart'; class Utils { @@ -8,16 +9,23 @@ class Utils { /// Utils._(); - static Offset getOffsetValues(SlideDirection direction, double value) { + static Offset getOffsetValues(SlideDirection direction, double value, + double sliderCloseSize, double sliderOpenSize) { + final sliderAnimateSize = sliderOpenSize - sliderCloseSize; + final sliderCurrentSize = clampDouble( + sliderAnimateSize * value + sliderCloseSize, + sliderCloseSize, + sliderOpenSize); + switch (direction) { case SlideDirection.LEFT_TO_RIGHT: - return Offset(value, 0); + return Offset(sliderCurrentSize, 0); case SlideDirection.RIGHT_TO_LEFT: - return Offset(-value, 0); + return Offset(-sliderCurrentSize, 0); case SlideDirection.TOP_TO_BOTTOM: - return Offset(0, value); + return Offset(0, sliderCurrentSize); default: - return Offset(value, 0); + return Offset(sliderCurrentSize, 0); } } diff --git a/lib/src/slider.dart b/lib/src/slider.dart index 1ac125c..6809ea5 100644 --- a/lib/src/slider.dart +++ b/lib/src/slider.dart @@ -60,6 +60,12 @@ class SliderDrawer extends StatefulWidget { /// final bool isDraggable; + ///[bool] whether show cover on child when slider is opened + final bool showCover; + + ///[Color] whether show cover on child when slider is opened + final Color coverColor; + ///[appBar] if you set [null] then it will not display app bar /// final Widget? appBar; @@ -104,7 +110,9 @@ class SliderDrawer extends StatefulWidget { this.slideDirection = SlideDirection.LEFT_TO_RIGHT, this.sliderBoxShadow, this.appBar = const SliderAppBar(), - this.isCupertino = false}) + this.isCupertino = false, + this.showCover = false, + this.coverColor = const Color.fromRGBO(0, 0, 0, 0.3)}) : super(key: key); @override @@ -119,7 +127,7 @@ class SliderDrawerState extends State double _percent = 0.0; late AnimationController _animationDrawerController; - late Animation _animation; + late Animation _animation; bool _isDragging = false; @@ -149,12 +157,10 @@ class SliderDrawerState extends State vsync: this, duration: Duration(milliseconds: widget.animationDuration)); - _animation = - Tween(begin: widget.sliderCloseSize, end: widget.sliderOpenSize) - .animate(CurvedAnimation( - parent: _animationDrawerController, - curve: Curves.decelerate, - reverseCurve: Curves.decelerate)); + _animation = Tween(begin: 0, end: 1).animate(CurvedAnimation( + parent: _animationDrawerController, + curve: Curves.decelerate, + reverseCurve: Curves.decelerate)); if (widget.appBar is SliderAppBar) { _appBarColor = (widget.appBar as SliderAppBar).appBarColor; } @@ -163,6 +169,9 @@ class SliderDrawerState extends State @override Widget build(BuildContext context) { return LayoutBuilder(builder: (context, constrain) { + void onHorizontalDragUpdate(DragUpdateDetails detail) => + _onHorizontalDragUpdate(detail, constrain); + return SizedBox( child: Stack(children: [ ///Menu @@ -188,16 +197,32 @@ class SliderDrawerState extends State animation: _animationDrawerController, builder: (_, child) { return Transform.translate( - offset: Utils.getOffsetValues( - widget.slideDirection, _animation.value), - child: child, - ); + offset: Utils.getOffsetValues( + widget.slideDirection, + _animation.value, + widget.sliderCloseSize, + widget.sliderOpenSize), + child: Stack(children: [ + child ?? const SizedBox(), + !widget.showCover || _animation.isDismissed + ? const SizedBox() + : GestureDetector( + behavior: HitTestBehavior.opaque, + onTap: _onCoverTap, + onHorizontalDragStart: _onHorizontalDragStart, + onHorizontalDragEnd: _onHorizontalDragEnd, + onHorizontalDragUpdate: onHorizontalDragUpdate, + child: Container( + color: widget.coverColor.withAlpha( + (_animation.value * widget.coverColor.alpha) + .round())), + ), + ])); }, child: GestureDetector( onHorizontalDragStart: _onHorizontalDragStart, onHorizontalDragEnd: _onHorizontalDragEnd, - onHorizontalDragUpdate: (detail) => - _onHorizontalDragUpdate(detail, constrain), + onHorizontalDragUpdate: onHorizontalDragUpdate, child: Container( width: double.infinity, height: double.infinity, @@ -303,6 +328,12 @@ class SliderDrawerState extends State } } + void _onCoverTap() { + if (_animation.isCompleted) { + closeSlider(); + } + } + move(double percent) { _percent = percent; _animationDrawerController.value = percent;