flutter_miniplayer/lib/miniplayer.dart

387 lines
12 KiB
Dart
Raw Normal View History

2020-07-29 22:13:39 +02:00
library miniplayer;
2020-07-29 22:32:28 +02:00
import 'dart:async';
import 'package:flutter/material.dart';
2020-09-26 15:12:50 +02:00
import 'package:miniplayer/src/utils.dart';
2020-07-29 22:32:28 +02:00
2020-09-26 15:13:06 +02:00
///Type definition for the builder function
2020-07-29 22:32:28 +02:00
typedef Widget MiniplayerBuilder(double height, double percentage);
2020-09-26 15:13:06 +02:00
///Miniplayers are commonly used in media applications like Spotify and Youtube.
///A miniplayer can be expanded and minified and
///remains on the screen when minified until dismissed by the user.
2020-07-29 22:32:28 +02:00
class Miniplayer extends StatefulWidget {
2020-09-26 15:13:06 +02:00
///Required option to set the minimum and maximum height
final double minHeight, maxHeight;
///Option to enable and set elevation for the miniplayer
final double elevation;
///Central API-Element
///Provides a builder with useful information
2020-07-29 22:32:28 +02:00
final MiniplayerBuilder builder;
2020-09-26 15:13:06 +02:00
///Option to set the animation curve
2020-07-29 22:32:28 +02:00
final Curve curve;
2020-09-26 15:13:06 +02:00
///Sets the background-color of the miniplayer
final Color backgroundColor;
2020-09-26 15:13:06 +02:00
///Option to set the animation duration
2020-08-26 16:40:39 +02:00
final Duration duration;
2020-09-26 15:13:06 +02:00
///Allows you to use a global ValueNotifier with the current progress.
///This can be used to hide the BottomNavigationBar.
final ValueNotifier<double> valueNotifier;
2020-09-26 15:13:06 +02:00
///If onDismiss is set, the miniplayer can be dismissed
2020-09-20 18:42:43 +02:00
final Function onDismiss;
2020-07-29 22:32:28 +02:00
2020-10-06 20:57:24 +02:00
final MiniplayerController controller;
2020-07-30 17:01:37 +02:00
const Miniplayer({
Key key,
@required this.minHeight,
@required this.maxHeight,
@required this.builder,
2020-09-21 13:30:22 +02:00
this.curve = Curves.easeOut,
2020-07-30 17:01:37 +02:00
this.elevation = 0,
this.backgroundColor = const Color(0x70000000),
this.valueNotifier,
2020-08-26 16:40:39 +02:00
this.duration = const Duration(milliseconds: 300),
2020-09-20 18:42:43 +02:00
this.onDismiss,
2020-10-06 20:57:24 +02:00
this.controller,
2020-07-30 17:01:37 +02:00
}) : super(key: key);
2020-07-29 22:32:28 +02:00
@override
_MiniplayerState createState() => _MiniplayerState();
}
class _MiniplayerState extends State<Miniplayer> with TickerProviderStateMixin {
ValueNotifier<double> heightNotifier;
2020-09-20 18:42:43 +02:00
ValueNotifier<double> dragDownPercentage = ValueNotifier(0);
2020-10-06 20:57:24 +02:00
PanelState snap;
2020-09-20 18:42:43 +02:00
///Current y position of drag gesture
double _dragHeight;
2020-09-20 18:42:43 +02:00
///Used to determine SnapPosition
double _startHeight;
2020-07-29 22:32:28 +02:00
2020-09-20 18:42:43 +02:00
bool dismissed = false;
bool animating = false;
2020-07-29 22:32:28 +02:00
2020-09-21 00:12:58 +02:00
///Counts how many updates were required for a distance (onPanUpdate) -> necessary to calculate the drag speed
int updateCount = 0;
2020-07-29 22:32:28 +02:00
StreamController<double> _heightController =
StreamController<double>.broadcast();
AnimationController _animationController;
2020-09-20 18:42:43 +02:00
2020-09-23 08:06:05 +02:00
void _statusListener(AnimationStatus status) {
if (status == AnimationStatus.completed) _resetAnimationController();
}
2020-10-06 20:57:24 +02:00
void _resetAnimationController({Duration duration}) {
if (_animationController != null) _animationController.dispose();
_animationController = AnimationController(
vsync: this,
2020-10-06 20:57:24 +02:00
duration: duration == null ? widget.duration : duration,
);
_animationController.addStatusListener(_statusListener);
animating = false;
2020-09-20 18:42:43 +02:00
}
2020-07-29 22:32:28 +02:00
@override
void initState() {
if (widget.valueNotifier == null)
heightNotifier = ValueNotifier(widget.minHeight);
else
heightNotifier = widget.valueNotifier;
_resetAnimationController();
2020-09-20 18:42:43 +02:00
_dragHeight = widget.minHeight;
2020-07-29 22:32:28 +02:00
2020-10-06 20:57:24 +02:00
if (widget.controller != null) {
widget.controller.addListener(() {
switch (widget.controller.value.height) {
case -1:
_animateToHeight(
widget.minHeight,
duration: widget.controller.value.duration,
);
break;
case -2:
_animateToHeight(
widget.maxHeight,
duration: widget.controller.value.duration,
);
break;
case -3:
_animateToHeight(
-1,
duration: widget.controller.value.duration,
);
break;
default:
_animateToHeight(
widget.controller.value.height.toDouble(),
duration: widget.controller.value.duration,
);
break;
}
});
}
2020-07-29 22:32:28 +02:00
super.initState();
}
@override
void dispose() {
_heightController.close();
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
2020-09-20 18:42:43 +02:00
if (dismissed) return Container();
return ValueListenableBuilder(
builder: (BuildContext context, double value, Widget child) {
var _percentage = ((value - widget.minHeight)) /
(widget.maxHeight - widget.minHeight);
return Stack(
alignment: Alignment.bottomCenter,
children: [
if (_percentage > 0)
GestureDetector(
2020-08-03 15:09:25 +02:00
onTap: () => _animateToHeight(widget.minHeight),
child: Opacity(
2020-09-26 15:12:50 +02:00
opacity: borderDouble(
2020-09-20 18:42:43 +02:00
minRange: 0, maxRange: 1, value: _percentage),
child: Container(color: widget.backgroundColor),
2020-07-29 22:32:28 +02:00
),
),
Align(
alignment: Alignment.bottomCenter,
child: SizedBox(
height: value,
child: GestureDetector(
2020-09-20 18:42:43 +02:00
child: ValueListenableBuilder(
valueListenable: dragDownPercentage,
builder: (context, value, child) {
if (value == 0) return child;
return Opacity(
2020-09-26 15:12:50 +02:00
opacity: borderDouble(
2020-09-21 14:09:43 +02:00
minRange: 0, maxRange: 1, value: 1 - value * 0.8),
2020-09-20 18:42:43 +02:00
child: Transform.translate(
offset: Offset(0.0, widget.minHeight * value * 0.5),
child: child,
),
);
},
child: Material(
color: Theme.of(context).canvasColor,
child: Container(
constraints: BoxConstraints.expand(),
child: widget.builder(value, _percentage),
decoration: BoxDecoration(
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.black45,
blurRadius: widget.elevation,
offset: Offset(0.0, 4))
],
color: Colors.white,
),
2020-08-27 11:41:09 +02:00
),
2020-07-30 17:01:37 +02:00
),
),
2020-09-23 08:06:05 +02:00
onTap: () => _snapToPosition(_dragHeight != widget.maxHeight
2020-10-06 20:57:24 +02:00
? PanelState.MAX
: PanelState.MIN),
2020-09-21 00:12:58 +02:00
onPanStart: (details) {
_startHeight = _dragHeight;
updateCount = 0;
if (animating) _resetAnimationController();
2020-09-21 00:12:58 +02:00
},
onPanEnd: (details) async {
2020-09-21 00:12:58 +02:00
///Calculates drag speed
double speed =
(_dragHeight - _startHeight * _dragHeight < _startHeight
? 1
: -1) /
updateCount *
100;
///Define the percentage distance depending on the speed with which the widget should snap
double snapPercentage = 0.005;
if (speed <= 4)
snapPercentage = 0.2;
else if (speed <= 9)
snapPercentage = 0.08;
else if (speed <= 50) snapPercentage = 0.01;
///Determine to which SnapPosition the widget should snap
2020-10-06 20:57:24 +02:00
PanelState snap = PanelState.MIN;
2020-09-26 15:12:50 +02:00
final _percentageMax = percentageFromValueInRange(
2020-09-20 18:42:43 +02:00
min: widget.minHeight,
max: widget.maxHeight,
value: _dragHeight);
2020-09-20 18:42:43 +02:00
///Started from expanded state
if (_startHeight > widget.minHeight) {
2020-09-21 00:12:58 +02:00
if (_percentageMax > 1 - snapPercentage)
2020-10-06 20:57:24 +02:00
snap = PanelState.MAX;
2020-09-20 18:42:43 +02:00
}
2020-09-20 18:42:43 +02:00
///Started from minified state
else {
2020-09-21 00:12:58 +02:00
if (_percentageMax > snapPercentage)
2020-10-06 20:57:24 +02:00
snap = PanelState.MAX;
2020-09-20 18:42:43 +02:00
else
2020-09-20 18:42:43 +02:00
///DismissedPercentage > 0.2 -> dismiss
if (widget.onDismiss != null &&
2020-09-26 15:12:50 +02:00
percentageFromValueInRange(
min: widget.minHeight,
max: 0,
value: _dragHeight) >
2020-10-06 20:57:24 +02:00
snapPercentage) snap = PanelState.DISMISS;
2020-09-20 18:42:43 +02:00
}
2020-09-21 00:12:58 +02:00
///Snap to position
2020-09-23 08:06:05 +02:00
_snapToPosition(snap);
2020-09-20 18:42:43 +02:00
},
onPanUpdate: (details) {
if (dismissed) return;
_dragHeight -= details.delta.dy;
2020-09-21 00:12:58 +02:00
updateCount++;
2020-09-20 18:42:43 +02:00
2020-09-23 08:06:05 +02:00
_handleHeightChange();
},
2020-07-29 22:32:28 +02:00
),
),
),
],
);
2020-07-29 22:32:28 +02:00
},
valueListenable: heightNotifier,
2020-07-29 22:32:28 +02:00
);
}
2020-09-20 18:42:43 +02:00
///Determines whether the panel should be updated in height or discarded
2020-09-23 08:06:05 +02:00
void _handleHeightChange({bool animation = false}) {
///Drag above minHeight
2020-09-20 18:42:43 +02:00
if (_dragHeight >= widget.minHeight) {
if (dragDownPercentage.value != 0) dragDownPercentage.value = 0;
2020-09-21 14:10:15 +02:00
if (_dragHeight > widget.maxHeight) return;
2020-09-21 14:10:15 +02:00
heightNotifier.value = _dragHeight;
}
///Drag below minHeight
else if (widget.onDismiss != null) {
2020-09-26 15:12:50 +02:00
var percentageDown = borderDouble(
2020-09-20 18:42:43 +02:00
minRange: 0,
maxRange: 1,
2020-09-26 15:12:50 +02:00
value: percentageFromValueInRange(
2020-09-20 18:42:43 +02:00
min: widget.minHeight, max: 0, value: _dragHeight));
if (dragDownPercentage.value != percentageDown)
dragDownPercentage.value = percentageDown;
2020-09-21 13:58:35 +02:00
if (percentageDown >= 1 && animation && !dismissed) {
2020-09-20 18:42:43 +02:00
if (widget.onDismiss != null) widget.onDismiss();
setState(() {
dismissed = true;
});
}
}
}
///Animates the panel height according to a SnapPoint
2020-10-06 20:57:24 +02:00
void _snapToPosition(PanelState snapPosition) {
2020-09-20 18:42:43 +02:00
switch (snapPosition) {
2020-10-06 20:57:24 +02:00
case PanelState.MAX:
2020-09-20 18:42:43 +02:00
_animateToHeight(widget.maxHeight);
return;
2020-10-06 20:57:24 +02:00
case PanelState.MIN:
2020-09-20 18:42:43 +02:00
_animateToHeight(widget.minHeight);
return;
2020-10-06 20:57:24 +02:00
case PanelState.DISMISS:
2020-09-20 18:42:43 +02:00
_animateToHeight(0);
return;
}
}
///Animates the panel height to a specific value
2020-10-06 20:57:24 +02:00
void _animateToHeight(final double h, {Duration duration}) {
2020-09-20 18:42:43 +02:00
final startHeight = _dragHeight;
2020-10-06 20:57:24 +02:00
if (duration != null) _resetAnimationController(duration: duration);
2020-09-20 18:42:43 +02:00
Animation<double> _sizeAnimation = Tween(
begin: startHeight,
2020-07-29 22:32:28 +02:00
end: h,
).animate(
CurvedAnimation(parent: _animationController, curve: widget.curve));
_sizeAnimation.addListener(() {
2020-09-20 18:42:43 +02:00
if (_sizeAnimation.value == startHeight) return;
_dragHeight = _sizeAnimation.value;
2020-09-23 08:06:05 +02:00
_handleHeightChange(animation: true);
2020-07-29 22:32:28 +02:00
});
2020-09-20 18:42:43 +02:00
animating = true;
_animationController.forward(from: 0);
2020-07-29 22:32:28 +02:00
}
2020-07-29 22:13:39 +02:00
}
2020-10-06 20:57:24 +02:00
///-1 Min, -2 Max, -3 Dismiss
enum PanelState { MAX, MIN, DISMISS }
class ControllerData {
final int height;
final Duration duration;
const ControllerData(this.height, this.duration);
}
class MiniplayerController extends ValueNotifier<ControllerData> {
MiniplayerController() : super(null);
void animateToHeight(
{double height, PanelState state, Duration duration}) {
if (height == null && state == null) return;
if (height != null && state != null) return;
ControllerData valBefore = value;
if (state != null)
value = ControllerData(state.heightCode, duration);
else {
if (height < 0) return;
value = ControllerData(height.round(), duration);
}
if (valBefore == value) notifyListeners();
}
void dismiss() {}
}