tsacdop-podcast-app-android/lib/util/audiopanel.dart

208 lines
6.4 KiB
Dart
Raw Normal View History

2020-02-14 07:32:21 +01:00
import 'package:flutter/material.dart';
2020-07-08 21:48:41 +02:00
enum SlideDirection { up, down }
2020-02-14 07:32:21 +01:00
class AudioPanel extends StatefulWidget {
final Widget miniPanel;
final Widget expandedPanel;
2020-07-08 21:48:41 +02:00
final double minHeight;
final double maxHeight;
AudioPanel(
{@required this.miniPanel,
@required this.expandedPanel,
this.minHeight = 60,
this.maxHeight = 300,
Key key})
: super(key: key);
2020-02-14 07:32:21 +01:00
@override
_AudioPanelState createState() => _AudioPanelState();
}
2020-07-08 21:48:41 +02:00
class _AudioPanelState extends State<AudioPanel> with TickerProviderStateMixin {
2020-02-14 07:32:21 +01:00
double initSize;
double _startdy;
double _move = 0;
AnimationController _controller;
2020-07-08 21:48:41 +02:00
AnimationController _slowController;
Animation _animation;
2020-07-08 21:48:41 +02:00
SlideDirection _slideDirection;
2020-02-14 07:32:21 +01:00
@override
void initState() {
2020-07-08 21:48:41 +02:00
initSize = widget.minHeight;
_slideDirection = SlideDirection.up;
2020-02-14 07:32:21 +01:00
_controller =
AnimationController(vsync: this, duration: Duration(milliseconds: 50))
2020-02-14 07:32:21 +01:00
..addListener(() {
if (mounted) setState(() {});
2020-02-14 07:32:21 +01:00
});
2020-07-08 21:48:41 +02:00
_slowController =
AnimationController(vsync: this, duration: Duration(milliseconds: 200))
..addListener(() {
if (mounted) setState(() {});
2020-07-08 21:48:41 +02:00
});
2020-02-14 07:32:21 +01:00
_animation =
Tween<double>(begin: initSize, end: initSize).animate(_controller);
super.initState();
2020-02-14 07:32:21 +01:00
}
@override
void dispose() {
_controller.dispose();
2020-07-08 21:48:41 +02:00
_slowController.dispose();
2020-02-14 07:32:21 +01:00
super.dispose();
}
@override
Widget build(BuildContext context) {
return Stack(children: <Widget>[
Container(
2020-07-08 21:48:41 +02:00
child: (_animation.value > widget.minHeight + 30)
? Positioned.fill(
child: GestureDetector(
onTap: () => _backToMini(),
child: Container(
color: Theme.of(context)
.scaffoldBackgroundColor
.withOpacity(0.8),
),
2020-02-14 07:32:21 +01:00
),
)
: Center(),
),
Container(
alignment: Alignment.bottomCenter,
child: GestureDetector(
onVerticalDragStart: (event) => _start(event),
onVerticalDragUpdate: (event) => _update(event),
onVerticalDragEnd: (event) => _end(),
child: Container(
2020-07-08 21:48:41 +02:00
height: (_animation.value >= widget.maxHeight)
? widget.maxHeight
: (_animation.value <= widget.minHeight)
? widget.minHeight
: _animation.value,
child: _animation.value < widget.minHeight + 30
? Container(
color: Theme.of(context).primaryColor,
child: Opacity(
2020-07-08 21:48:41 +02:00
opacity: _animation.value > widget.minHeight
? (widget.minHeight + 30 - _animation.value) / 40
: 1,
child: Container(
child: widget.miniPanel,
),
),
)
: Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
boxShadow: [
BoxShadow(
offset: Offset(0, -0.5),
blurRadius: 1,
color:
Theme.of(context).brightness == Brightness.light
? Colors.grey[400].withOpacity(0.5)
: Colors.grey[800],
),
],
),
child: SingleChildScrollView(
2020-07-08 21:48:41 +02:00
physics: const NeverScrollableScrollPhysics(),
child: Opacity(
2020-07-08 21:48:41 +02:00
opacity: _animation.value < (widget.maxHeight - 50)
? (_animation.value - widget.minHeight) /
(widget.maxHeight - widget.minHeight - 50)
: 1,
child: Container(
2020-07-08 21:48:41 +02:00
height: widget.maxHeight,
child: widget.expandedPanel,
),
),
2020-02-14 07:32:21 +01:00
),
),
),
),
2020-02-14 07:32:21 +01:00
),
]);
}
_backToMini() {
setState(() {
2020-07-08 21:48:41 +02:00
_animation = Tween<double>(begin: initSize, end: widget.minHeight)
.animate(_slowController);
initSize = widget.minHeight;
});
2020-07-08 21:48:41 +02:00
_slowController.forward();
2020-02-14 07:32:21 +01:00
}
_start(DragStartDetails event) {
setState(() {
_startdy = event.localPosition.dy;
_animation =
Tween<double>(begin: initSize, end: initSize).animate(_controller);
2020-02-14 07:32:21 +01:00
});
_controller.forward();
}
_update(DragUpdateDetails event) {
setState(() {
_move = _startdy - event.localPosition.dy;
_animation = Tween<double>(begin: initSize, end: initSize + _move)
.animate(_controller);
2020-07-08 21:48:41 +02:00
_slideDirection = _move > 0 ? SlideDirection.up : SlideDirection.down;
2020-02-14 07:32:21 +01:00
});
_controller.forward();
}
_end() {
2020-07-08 21:48:41 +02:00
if (_slideDirection == SlideDirection.up) {
if (_move > 20) {
setState(() {
_animation =
Tween<double>(begin: _animation.value, end: widget.maxHeight)
.animate(_slowController);
initSize = widget.maxHeight;
});
_slowController.forward();
} else {
setState(() {
_animation =
Tween<double>(begin: _animation.value, end: widget.minHeight)
.animate(_controller);
initSize = widget.minHeight;
});
_controller.forward();
}
} else if (_slideDirection == SlideDirection.down) {
if (_move > -50) {
setState(() {
_animation =
Tween<double>(begin: _animation.value, end: widget.maxHeight)
.animate(_slowController);
initSize = widget.maxHeight;
});
_slowController.forward();
} else {
setState(() {
_animation =
Tween<double>(begin: _animation.value, end: widget.minHeight)
.animate(_controller);
initSize = widget.minHeight;
});
_controller.forward();
}
}
if (_animation.value >= widget.maxHeight) {
2020-02-14 07:32:21 +01:00
setState(() {
2020-07-08 21:48:41 +02:00
initSize = widget.maxHeight;
2020-02-14 07:32:21 +01:00
});
2020-07-08 21:48:41 +02:00
} else if (_animation.value < widget.minHeight) {
2020-02-14 07:32:21 +01:00
setState(() {
2020-07-08 21:48:41 +02:00
initSize = widget.minHeight;
2020-02-14 07:32:21 +01:00
});
}
}
}