Add drawer

master
落雨楓 2 years ago
parent d9c92541d1
commit 1faaf0e222

@ -1,5 +1,6 @@
import 'dart:ui'; import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter_slider_drawer/flutter_slider_drawer.dart'; import 'package:flutter_slider_drawer/flutter_slider_drawer.dart';
class Utils { class Utils {
@ -8,16 +9,23 @@ class Utils {
/// ///
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) { switch (direction) {
case SlideDirection.LEFT_TO_RIGHT: case SlideDirection.LEFT_TO_RIGHT:
return Offset(value, 0); return Offset(sliderCurrentSize, 0);
case SlideDirection.RIGHT_TO_LEFT: case SlideDirection.RIGHT_TO_LEFT:
return Offset(-value, 0); return Offset(-sliderCurrentSize, 0);
case SlideDirection.TOP_TO_BOTTOM: case SlideDirection.TOP_TO_BOTTOM:
return Offset(0, value); return Offset(0, sliderCurrentSize);
default: default:
return Offset(value, 0); return Offset(sliderCurrentSize, 0);
} }
} }

@ -60,6 +60,12 @@ class SliderDrawer extends StatefulWidget {
/// ///
final bool isDraggable; 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 ///[appBar] if you set [null] then it will not display app bar
/// ///
final Widget? appBar; final Widget? appBar;
@ -104,7 +110,9 @@ class SliderDrawer extends StatefulWidget {
this.slideDirection = SlideDirection.LEFT_TO_RIGHT, this.slideDirection = SlideDirection.LEFT_TO_RIGHT,
this.sliderBoxShadow, this.sliderBoxShadow,
this.appBar = const SliderAppBar(), 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); : super(key: key);
@override @override
@ -119,7 +127,7 @@ class SliderDrawerState extends State<SliderDrawer>
double _percent = 0.0; double _percent = 0.0;
late AnimationController _animationDrawerController; late AnimationController _animationDrawerController;
late Animation _animation; late Animation<double> _animation;
bool _isDragging = false; bool _isDragging = false;
@ -149,12 +157,10 @@ class SliderDrawerState extends State<SliderDrawer>
vsync: this, vsync: this,
duration: Duration(milliseconds: widget.animationDuration)); duration: Duration(milliseconds: widget.animationDuration));
_animation = _animation = Tween<double>(begin: 0, end: 1).animate(CurvedAnimation(
Tween<double>(begin: widget.sliderCloseSize, end: widget.sliderOpenSize) parent: _animationDrawerController,
.animate(CurvedAnimation( curve: Curves.decelerate,
parent: _animationDrawerController, reverseCurve: Curves.decelerate));
curve: Curves.decelerate,
reverseCurve: Curves.decelerate));
if (widget.appBar is SliderAppBar) { if (widget.appBar is SliderAppBar) {
_appBarColor = (widget.appBar as SliderAppBar).appBarColor; _appBarColor = (widget.appBar as SliderAppBar).appBarColor;
} }
@ -163,6 +169,9 @@ class SliderDrawerState extends State<SliderDrawer>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constrain) { return LayoutBuilder(builder: (context, constrain) {
void onHorizontalDragUpdate(DragUpdateDetails detail) =>
_onHorizontalDragUpdate(detail, constrain);
return SizedBox( return SizedBox(
child: Stack(children: [ child: Stack(children: [
///Menu ///Menu
@ -188,16 +197,32 @@ class SliderDrawerState extends State<SliderDrawer>
animation: _animationDrawerController, animation: _animationDrawerController,
builder: (_, child) { builder: (_, child) {
return Transform.translate( return Transform.translate(
offset: Utils.getOffsetValues( offset: Utils.getOffsetValues(
widget.slideDirection, _animation.value), widget.slideDirection,
child: child, _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( child: GestureDetector(
onHorizontalDragStart: _onHorizontalDragStart, onHorizontalDragStart: _onHorizontalDragStart,
onHorizontalDragEnd: _onHorizontalDragEnd, onHorizontalDragEnd: _onHorizontalDragEnd,
onHorizontalDragUpdate: (detail) => onHorizontalDragUpdate: onHorizontalDragUpdate,
_onHorizontalDragUpdate(detail, constrain),
child: Container( child: Container(
width: double.infinity, width: double.infinity,
height: double.infinity, height: double.infinity,
@ -303,6 +328,12 @@ class SliderDrawerState extends State<SliderDrawer>
} }
} }
void _onCoverTap() {
if (_animation.isCompleted) {
closeSlider();
}
}
move(double percent) { move(double percent) {
_percent = percent; _percent = percent;
_animationDrawerController.value = percent; _animationDrawerController.value = percent;

Loading…
Cancel
Save