implement swipe gesture in left and right drawer

master
Nikhil Vadoliya 5 years ago
parent bc279be86f
commit d44608ffa1

@ -30,13 +30,12 @@ class _MyAppState extends State<MyApp> {
return MaterialApp( return MaterialApp(
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
home: Scaffold( home: Scaffold(
body: SliderMenuContainer( body: Padding(
padding: const EdgeInsets.only(top: 0),
child: SliderMenuContainer(
appBarColor: Colors.white, appBarColor: Colors.white,
key: _key, key: _key,
appBarPadding: const EdgeInsets.only(top: 20),
sliderMenuOpenSize: 200, sliderMenuOpenSize: 200,
sliderMenuCloseSize: 60,
appBarHeight: 60,
title: Text( title: Text(
title, title,
style: TextStyle(fontSize: 22, fontWeight: FontWeight.w700), style: TextStyle(fontSize: 22, fontWeight: FontWeight.w700),
@ -51,6 +50,7 @@ class _MyAppState extends State<MyApp> {
), ),
sliderMain: MainWidget()), sliderMain: MainWidget()),
), ),
),
); );
} }
} }

@ -27,8 +27,11 @@ class _MainWidgetState extends State<MainWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return Container(
margin: const EdgeInsets.all(20),
child: ListView.separated( child: ListView.separated(
padding: const EdgeInsets.symmetric(horizontal: 10), scrollDirection: Axis.vertical,
// physics: BouncingScrollPhysics(),
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
itemBuilder: (builder, index) { itemBuilder: (builder, index) {
return LimitedBox( return LimitedBox(
maxHeight: 150, maxHeight: 150,

@ -11,6 +11,7 @@ class SliderMenuContainer extends StatefulWidget {
final int animationDuration; final int animationDuration;
final double sliderMenuOpenSize; final double sliderMenuOpenSize;
final double sliderMenuCloseSize; final double sliderMenuCloseSize;
final bool isDraggable;
final bool hasAppBar; final bool hasAppBar;
final Color drawerIconColor; final Color drawerIconColor;
@ -34,6 +35,7 @@ class SliderMenuContainer extends StatefulWidget {
Key key, Key key,
this.sliderMenu, this.sliderMenu,
this.sliderMain, this.sliderMain,
this.isDraggable = true,
this.animationDuration = 200, this.animationDuration = 200,
this.sliderMenuOpenSize = 265, this.sliderMenuOpenSize = 265,
this.drawerIconColor = Colors.black, this.drawerIconColor = Colors.black,
@ -63,9 +65,17 @@ class SliderMenuContainer extends StatefulWidget {
class SliderMenuContainerState extends State<SliderMenuContainer> class SliderMenuContainerState extends State<SliderMenuContainer>
with TickerProviderStateMixin { with TickerProviderStateMixin {
static const double WIDTH_GESTURE = 50.0;
static const double HEIGHT_GESTURE = 30.0;
static const double BLUR_SHADOW = 20.0;
double slideAmount = 0.0;
double _percent = 0.0;
AnimationController _animationDrawerController; AnimationController _animationDrawerController;
Animation animation; Animation animation;
bool dragging = false;
Widget drawerIcon; Widget drawerIcon;
/// check whether drawer is open /// check whether drawer is open
@ -103,6 +113,7 @@ class SliderMenuContainerState extends State<SliderMenuContainer>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constrain) {
return Container( return Container(
child: Stack(children: <Widget>[ child: Stack(children: <Widget>[
/// Display Menu /// Display Menu
@ -116,7 +127,7 @@ class SliderMenuContainerState extends State<SliderMenuContainer>
if (widget.isShadow) ...[ if (widget.isShadow) ...[
AnimatedBuilder( AnimatedBuilder(
animation: _animationDrawerController, animation: _animationDrawerController,
builder: (anim, child) { builder: (_, child) {
return Transform.translate( return Transform.translate(
offset: Utils.getOffsetValueForShadow(widget.slideDirection, offset: Utils.getOffsetValueForShadow(widget.slideDirection,
animation.value, widget.sliderMenuOpenSize), animation.value, widget.sliderMenuOpenSize),
@ -130,8 +141,10 @@ class SliderMenuContainerState extends State<SliderMenuContainer>
decoration: BoxDecoration(shape: BoxShape.rectangle, boxShadow: [ decoration: BoxDecoration(shape: BoxShape.rectangle, boxShadow: [
BoxShadow( BoxShadow(
color: widget.shadowColor, color: widget.shadowColor,
blurRadius: widget.shadowBlurRadius, // soften the shadow blurRadius: widget.shadowBlurRadius,
spreadRadius: widget.shadowSpreadRadius, //extend the shadow // soften the shadow
spreadRadius: widget.shadowSpreadRadius,
//extend the shadow
offset: Offset( offset: Offset(
15.0, // Move to right 15 horizontally 15.0, // Move to right 15 horizontally
15.0, // Move to bottom 15 Vertically 15.0, // Move to bottom 15 Vertically
@ -145,13 +158,19 @@ class SliderMenuContainerState extends State<SliderMenuContainer>
//Display Main Screen //Display Main Screen
AnimatedBuilder( AnimatedBuilder(
animation: _animationDrawerController, animation: _animationDrawerController,
builder: (anim, child) { builder: (_, child) {
return Transform.translate( return Transform.translate(
offset: offset:
Utils.getOffsetValues(widget.slideDirection, animation.value), Utils.getOffsetValues(widget.slideDirection, animation.value),
child: child, child: child,
); );
}, },
child: GestureDetector(
behavior: HitTestBehavior.deferToChild,
onHorizontalDragStart: _onHorizontalDragStart,
onHorizontalDragEnd: _onHorizontalDragEnd,
onHorizontalDragUpdate: (detail) =>
_onHorizontalDragUpdate(detail, constrain),
child: Container( child: Container(
width: double.infinity, width: double.infinity,
height: double.infinity, height: double.infinity,
@ -179,7 +198,9 @@ class SliderMenuContainerState extends State<SliderMenuContainer>
), ),
), ),
), ),
),
])); ]));
});
} }
@override @override
@ -187,4 +208,88 @@ class SliderMenuContainerState extends State<SliderMenuContainer>
super.dispose(); super.dispose();
_animationDrawerController.dispose(); _animationDrawerController.dispose();
} }
void _onHorizontalDragStart(DragStartDetails detail) {
if (!widget.isDraggable) return;
//Check use start dragging from left edge / right edge then enable dragging
if ((widget.slideDirection == SlideDirection.LEFT_TO_RIGHT &&
detail.localPosition.dx <= WIDTH_GESTURE) ||
(widget.slideDirection == SlideDirection.RIGHT_TO_LEFT &&
detail.localPosition.dx >=
WIDTH_GESTURE) /*&&
detail.localPosition.dy <= widget.appBarHeight*/
) {
this.setState(() {
dragging = true;
});
}
//Check use start dragging from top edge / bottom edge then enable dragging
if (widget.slideDirection == SlideDirection.TOP_TO_BOTTOM &&
detail.localPosition.dy >= HEIGHT_GESTURE) {
this.setState(() {
dragging = true;
});
}
}
void _onHorizontalDragEnd(DragEndDetails detail) {
if (!widget.isDraggable) return;
if (dragging) {
openOrClose();
setState(() {
dragging = false;
});
}
}
void _onHorizontalDragUpdate(
DragUpdateDetails detail,
BoxConstraints constraints,
) {
if (!widget.isDraggable) return;
// open drawer for left/right type drawer
if (dragging && widget.slideDirection == SlideDirection.LEFT_TO_RIGHT ||
widget.slideDirection == SlideDirection.RIGHT_TO_LEFT) {
var globalPosition = detail.globalPosition.dx;
globalPosition = globalPosition < 0 ? 0 : globalPosition;
double position = globalPosition / constraints.maxWidth;
var realPosition = widget.slideDirection == SlideDirection.LEFT_TO_RIGHT
? position
: (1 - position);
move(realPosition);
}
// open drawer for top/bottom type drawer
/*if (dragging && widget.slideDirection == SlideDirection.TOP_TO_BOTTOM) {
var globalPosition = detail.globalPosition.dx;
globalPosition = globalPosition < 0 ? 0 : globalPosition;
double position = globalPosition / constraints.maxHeight;
var realPosition = widget.slideDirection == SlideDirection.TOP_TO_BOTTOM
? position
: (1 - position);
move(realPosition);
}*/
// close drawer for left/right type drawer
if (isDrawerOpen &&
(widget.slideDirection == SlideDirection.LEFT_TO_RIGHT ||
widget.slideDirection == SlideDirection.RIGHT_TO_LEFT) &&
detail.delta.dx < 15) {
closeDrawer();
}
}
move(double percent) {
_percent = percent;
_animationDrawerController.value = percent;
_animationDrawerController.notifyListeners();
}
openOrClose() {
if (_percent > 0.3) {
openDrawer();
} else {
closeDrawer();
}
}
} }

Loading…
Cancel
Save