From e02e1339e723a8b3b3ac01966145bc2fd2c19c22 Mon Sep 17 00:00:00 2001 From: xayr0n Date: Thu, 18 Aug 2022 16:23:43 +0530 Subject: [PATCH 1/3] Bring back dismissed player & add check for onTap --- .dart_tool/package_config.json | 56 ++++++++++++++ .dart_tool/package_config_subset | 33 +++++++++ .dart_tool/version | 1 + .packages | 14 ++++ example/lib/main.dart | 18 +++-- example/lib/widgets/player.dart | 8 +- example/pubspec.lock | 36 ++++----- lib/miniplayer.dart | 123 +++++++++++++++---------------- pubspec.lock | 50 +++++++++++++ pubspec.yaml | 4 +- 10 files changed, 252 insertions(+), 91 deletions(-) create mode 100644 .dart_tool/package_config.json create mode 100644 .dart_tool/package_config_subset create mode 100644 .dart_tool/version create mode 100644 .packages create mode 100644 pubspec.lock diff --git a/.dart_tool/package_config.json b/.dart_tool/package_config.json new file mode 100644 index 0000000..8fc046c --- /dev/null +++ b/.dart_tool/package_config.json @@ -0,0 +1,56 @@ +{ + "configVersion": 2, + "packages": [ + { + "name": "characters", + "rootUri": "file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/characters-1.2.0", + "packageUri": "lib/", + "languageVersion": "2.12" + }, + { + "name": "collection", + "rootUri": "file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/collection-1.16.0", + "packageUri": "lib/", + "languageVersion": "2.12" + }, + { + "name": "flutter", + "rootUri": "file:///C:/flutter/packages/flutter", + "packageUri": "lib/", + "languageVersion": "2.12" + }, + { + "name": "material_color_utilities", + "rootUri": "file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/material_color_utilities-0.1.4", + "packageUri": "lib/", + "languageVersion": "2.13" + }, + { + "name": "meta", + "rootUri": "file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/meta-1.7.0", + "packageUri": "lib/", + "languageVersion": "2.12" + }, + { + "name": "sky_engine", + "rootUri": "file:///C:/flutter/bin/cache/pkg/sky_engine", + "packageUri": "lib/", + "languageVersion": "2.12" + }, + { + "name": "vector_math", + "rootUri": "file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/vector_math-2.1.2", + "packageUri": "lib/", + "languageVersion": "2.14" + }, + { + "name": "miniplayer", + "rootUri": "../", + "packageUri": "lib/", + "languageVersion": "2.17" + } + ], + "generated": "2022-08-18T09:15:09.050201Z", + "generator": "pub", + "generatorVersion": "2.17.6" +} diff --git a/.dart_tool/package_config_subset b/.dart_tool/package_config_subset new file mode 100644 index 0000000..fa9cb8b --- /dev/null +++ b/.dart_tool/package_config_subset @@ -0,0 +1,33 @@ +characters +2.12 +file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/characters-1.2.0/ +file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/characters-1.2.0/lib/ +collection +2.12 +file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/collection-1.16.0/ +file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/collection-1.16.0/lib/ +material_color_utilities +2.13 +file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/material_color_utilities-0.1.4/ +file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/material_color_utilities-0.1.4/lib/ +meta +2.12 +file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/meta-1.7.0/ +file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/meta-1.7.0/lib/ +vector_math +2.14 +file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/vector_math-2.1.2/ +file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/vector_math-2.1.2/lib/ +sky_engine +2.12 +file:///C:/flutter/bin/cache/pkg/sky_engine/ +file:///C:/flutter/bin/cache/pkg/sky_engine/lib/ +flutter +2.12 +file:///C:/flutter/packages/flutter/ +file:///C:/flutter/packages/flutter/lib/ +miniplayer +2.17 +file:///E:/Projects/miniplayer/ +file:///E:/Projects/miniplayer/lib/ +2 diff --git a/.dart_tool/version b/.dart_tool/version new file mode 100644 index 0000000..7da3c16 --- /dev/null +++ b/.dart_tool/version @@ -0,0 +1 @@ +3.0.5 \ No newline at end of file diff --git a/.packages b/.packages new file mode 100644 index 0000000..2fc212d --- /dev/null +++ b/.packages @@ -0,0 +1,14 @@ +# This file is deprecated. Tools should instead consume +# `.dart_tool/package_config.json`. +# +# For more info see: https://dart.dev/go/dot-packages-deprecation +# +# Generated by pub on 2022-08-18 14:45:09.031156. +characters:file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/characters-1.2.0/lib/ +collection:file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/collection-1.16.0/lib/ +flutter:file:///C:/flutter/packages/flutter/lib/ +material_color_utilities:file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/material_color_utilities-0.1.4/lib/ +meta:file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/meta-1.7.0/lib/ +sky_engine:file:///C:/flutter/bin/cache/pkg/sky_engine/lib/ +vector_math:file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/vector_math-2.1.2/lib/ +miniplayer:lib/ diff --git a/example/lib/main.dart b/example/lib/main.dart index 617c378..371ae69 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -45,13 +45,17 @@ class _MyHomePageState extends State { ], ), ValueListenableBuilder( - valueListenable: currentlyPlaying, - builder: (BuildContext context, AudioObject? audioObject, - Widget? child) => - audioObject != null - ? DetailedPlayer(audioObject: audioObject) - : Container(), - ), + valueListenable: currentlyPlaying, + builder: (BuildContext context, AudioObject? audioObject, + Widget? child) { + bool isActive = audioObject != null; + return isActive + ? DetailedPlayer( + audioObject: audioObject, + isActive: isActive, + ) + : Container(); + }), ], ), bottomNavigationBar: ValueListenableBuilder( diff --git a/example/lib/widgets/player.dart b/example/lib/widgets/player.dart index c5f4760..322a2c7 100644 --- a/example/lib/widgets/player.dart +++ b/example/lib/widgets/player.dart @@ -11,8 +11,13 @@ final MiniplayerController controller = MiniplayerController(); class DetailedPlayer extends StatelessWidget { final AudioObject audioObject; + final bool isActive; - const DetailedPlayer({Key? key, required this.audioObject}) : super(key: key); + const DetailedPlayer({ + Key? key, + required this.audioObject, + required this.isActive, + }) : super(key: key); @override Widget build(BuildContext context) { @@ -22,6 +27,7 @@ class DetailedPlayer extends StatelessWidget { maxHeight: playerMaxHeight, controller: controller, elevation: 4, + isActive: isActive, onDismissed: () => currentlyPlaying.value = null, curve: Curves.easeOut, builder: (height, percentage) { diff --git a/example/pubspec.lock b/example/pubspec.lock index cddd362..28a8014 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.8.1" + version: "2.8.2" boolean_selector: dependency: transitive description: @@ -21,7 +21,7 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" charcode: dependency: transitive description: @@ -42,14 +42,14 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "1.16.0" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" flutter: dependency: "direct main" description: flutter @@ -66,7 +66,14 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10" + version: "0.12.11" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.4" meta: dependency: transitive description: @@ -80,14 +87,14 @@ packages: path: ".." relative: true source: path - version: "1.0.1" + version: "1.1.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" sky_engine: dependency: transitive description: flutter @@ -99,7 +106,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -134,20 +141,13 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.2" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" + version: "0.4.9" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.2" sdks: - dart: ">=2.12.0 <3.0.0" + dart: ">=2.17.1 <3.0.0" diff --git a/lib/miniplayer.dart b/lib/miniplayer.dart index 18635ee..a561361 100644 --- a/lib/miniplayer.dart +++ b/lib/miniplayer.dart @@ -9,10 +9,10 @@ import 'package:miniplayer/src/utils.dart'; export 'package:miniplayer/src/miniplayer_will_pop_scope.dart'; ///Type definition for the builder function -typedef Widget MiniplayerBuilder(double height, double percentage); +typedef MiniplayerBuilder = Widget Function(double height, double percentage); ///Type definition for onDismiss. Will be used in a future version. -typedef void DismissCallback(double percentage); +typedef DismissCallback = void Function(double percentage); ///Miniplayer class class Miniplayer extends StatefulWidget { @@ -50,6 +50,12 @@ class Miniplayer extends StatefulWidget { //Allows you to manually control the miniplayer in code final MiniplayerController? controller; + ///Checks if the miniplayer is currently in use + final bool isActive; + + ///check to disable onTap when necessary + final bool useOnTap; + const Miniplayer({ Key? key, required this.minHeight, @@ -63,13 +69,15 @@ class Miniplayer extends StatefulWidget { this.onDismiss, this.onDismissed, this.controller, + required this.isActive, + this.useOnTap = false, }) : super(key: key); @override - _MiniplayerState createState() => _MiniplayerState(); + MiniplayerState createState() => MiniplayerState(); } -class _MiniplayerState extends State with TickerProviderStateMixin { +class MiniplayerState extends State with TickerProviderStateMixin { late ValueNotifier heightNotifier; ValueNotifier dragDownPercentage = ValueNotifier(0); @@ -89,7 +97,7 @@ class _MiniplayerState extends State with TickerProviderStateMixin { ///Counts how many updates were required for a distance (onPanUpdate) -> necessary to calculate the drag speed int updateCount = 0; - StreamController _heightController = + final StreamController _heightController = StreamController.broadcast(); AnimationController? _animationController; @@ -98,9 +106,7 @@ class _MiniplayerState extends State with TickerProviderStateMixin { } void _resetAnimationController({Duration? duration}) { - if (_animationController != null) { - _animationController!.dispose(); - } + if (_animationController != null) _animationController!.dispose(); _animationController = AnimationController( vsync: this, duration: duration ?? widget.duration, @@ -128,7 +134,6 @@ class _MiniplayerState extends State with TickerProviderStateMixin { if (widget.onDismissed != null) { onDismissed = widget.onDismissed; } else { - // ignore: deprecated_member_use_from_same_package onDismissed = widget.onDismiss; } @@ -138,10 +143,7 @@ class _MiniplayerState extends State with TickerProviderStateMixin { @override void dispose() { _heightController.close(); - - if (_animationController != null) { - _animationController!.dispose(); - } + if (_animationController != null) _animationController!.dispose(); if (widget.controller != null) { widget.controller!.removeListener(controllerListener); @@ -153,9 +155,12 @@ class _MiniplayerState extends State with TickerProviderStateMixin { @override Widget build(BuildContext context) { if (dismissed) { + if (widget.isActive) { + dismissed = false; + _snapToPosition(PanelState.MIN); + } return Container(); } - return MiniplayerWillPopScope( onWillPop: () async { if (heightNotifier.value > widget.minHeight) { @@ -167,20 +172,17 @@ class _MiniplayerState extends State with TickerProviderStateMixin { child: ValueListenableBuilder( valueListenable: heightNotifier, builder: (BuildContext context, double height, Widget? _) { - var _percentage = ((height - widget.minHeight)) / + var percentage = ((height - widget.minHeight)) / (widget.maxHeight - widget.minHeight); return Stack( alignment: Alignment.bottomCenter, children: [ - if (_percentage > 0) - GestureDetector( - onTap: () => _animateToHeight(widget.minHeight), - child: Opacity( - opacity: borderDouble( - minRange: 0.0, maxRange: 1.0, value: _percentage), - child: Container(color: widget.backgroundColor), - ), + if (percentage > 0) + Opacity( + opacity: borderDouble( + minRange: 0.0, maxRange: 1.0, value: percentage), + child: Container(color: widget.backgroundColor), ), Align( alignment: Alignment.bottomCenter, @@ -191,6 +193,8 @@ class _MiniplayerState extends State with TickerProviderStateMixin { valueListenable: dragDownPercentage, builder: (BuildContext context, double value, Widget? child) { + if (value == 0.0) return child!; + return Opacity( opacity: borderDouble( minRange: 0.0, @@ -204,30 +208,30 @@ class _MiniplayerState extends State with TickerProviderStateMixin { }, child: Material( child: Container( - constraints: BoxConstraints.expand(), - child: widget.builder(height, _percentage), + constraints: const BoxConstraints.expand(), decoration: BoxDecoration( boxShadow: [ BoxShadow( - color: Colors.black45, - blurRadius: widget.elevation, - offset: Offset(0.0, 4)) + color: Colors.black45, + blurRadius: widget.elevation, + offset: const Offset(0.0, 4), + ) ], color: Theme.of(context).canvasColor, ), + child: widget.builder(height, percentage), ), ), ), - onTap: () => _snapToPosition(_dragHeight != widget.maxHeight - ? PanelState.MAX - : PanelState.MIN), + onTap: () => _snapToPosition( + _dragHeight != widget.maxHeight && widget.useOnTap + ? PanelState.MAX + : PanelState.MIN), onPanStart: (details) { _startHeight = _dragHeight; updateCount = 0; - if (animating) { - _resetAnimationController(); - } + if (animating) _resetAnimationController(); }, onPanEnd: (details) async { ///Calculates drag speed @@ -251,31 +255,30 @@ class _MiniplayerState extends State with TickerProviderStateMixin { ///Determine to which SnapPosition the widget should snap PanelState snap = PanelState.MIN; - final _percentageMax = percentageFromValueInRange( + final percentageMax = percentageFromValueInRange( min: widget.minHeight, max: widget.maxHeight, value: _dragHeight); ///Started from expanded state if (_startHeight > widget.minHeight) { - if (_percentageMax > 1 - snapPercentage) { + if (percentageMax > 1 - snapPercentage) { snap = PanelState.MAX; } } ///Started from minified state else { - if (_percentageMax > snapPercentage) { + if (percentageMax > snapPercentage) { snap = PanelState.MAX; - } + } else ///DismissedPercentage > 0.2 -> dismiss - else if (onDismissed != null && + if (onDismissed != null && percentageFromValueInRange( - min: widget.minHeight, - max: 0, - value: _dragHeight, - ) > + min: widget.minHeight, + max: 0, + value: _dragHeight) > snapPercentage) { snap = PanelState.DISMISS; } @@ -306,9 +309,7 @@ class _MiniplayerState extends State with TickerProviderStateMixin { void _handleHeightChange({bool animation = false}) { ///Drag above minHeight if (_dragHeight >= widget.minHeight) { - if (dragDownPercentage.value != 0) { - dragDownPercentage.value = 0; - } + if (dragDownPercentage.value != 0) dragDownPercentage.value = 0; if (_dragHeight > widget.maxHeight) return; @@ -317,7 +318,7 @@ class _MiniplayerState extends State with TickerProviderStateMixin { ///Drag below minHeight else if (onDismissed != null) { - final percentageDown = borderDouble( + var percentageDown = borderDouble( minRange: 0.0, maxRange: 1.0, value: percentageFromValueInRange( @@ -328,10 +329,10 @@ class _MiniplayerState extends State with TickerProviderStateMixin { } if (percentageDown >= 1 && animation && !dismissed) { - if (onDismissed != null) { - onDismissed!(); - } - setState(() => dismissed = true); + if (onDismissed != null) onDismissed!(); + setState(() { + dismissed = true; + }); } } } @@ -356,20 +357,18 @@ class _MiniplayerState extends State with TickerProviderStateMixin { if (_animationController == null) return; final startHeight = _dragHeight; - if (duration != null) { - _resetAnimationController(duration: duration); - } + if (duration != null) _resetAnimationController(duration: duration); - Animation _sizeAnimation = Tween( + Animation sizeAnimation = Tween( begin: startHeight, end: h, ).animate( CurvedAnimation(parent: _animationController!, curve: widget.curve)); - _sizeAnimation.addListener(() { - if (_sizeAnimation.value == startHeight) return; + sizeAnimation.addListener(() { + if (sizeAnimation.value == startHeight) return; - _dragHeight = _sizeAnimation.value; + _dragHeight = sizeAnimation.value; _handleHeightChange(animation: true); }); @@ -412,7 +411,7 @@ class _MiniplayerState extends State with TickerProviderStateMixin { } } -///-1 Min, -2 Max, -3 Dismiss +///-1 min, -2 max, -3 dismiss enum PanelState { MAX, MIN, DISMISS } //ControllerData class. Used for the controller @@ -441,15 +440,13 @@ class MiniplayerController extends ValueNotifier { ControllerData? valBefore = value; if (state != null) { - value = ControllerData(state.heightCode, duration); + value = ControllerData(state.hashCode, duration); } else { if (height! < 0) return; value = ControllerData(height.round(), duration); } - if (valBefore == value) { - notifyListeners(); - } + if (valBefore == value) notifyListeners(); } } diff --git a/pubspec.lock b/pubspec.lock new file mode 100644 index 0000000..1c22f13 --- /dev/null +++ b/pubspec.lock @@ -0,0 +1,50 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + collection: + dependency: transitive + description: + name: collection + url: "https://pub.dartlang.org" + source: hosted + version: "1.16.0" + flutter: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.4" + meta: + dependency: transitive + description: + name: meta + url: "https://pub.dartlang.org" + source: hosted + version: "1.7.0" + sky_engine: + dependency: transitive + description: flutter + source: sdk + version: "0.0.99" + vector_math: + dependency: transitive + description: + name: vector_math + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.2" +sdks: + dart: ">=2.17.1 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 42a8166..a288130 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,11 +1,11 @@ name: miniplayer description: A lightweight flutter package to simplify the creation of a miniplayer. -version: 1.0.1 +version: 1.1.0 homepage: https://www.peterscode.dev repository: https://github.com/peterscodee/miniplayer environment: - sdk: ">=2.12.0 <3.0.0" + sdk: ">=2.17.1 <3.0.0" dependencies: flutter: From 45b1c2213d65290f93357f8be28795863707308d Mon Sep 17 00:00:00 2001 From: xayr0n Date: Thu, 18 Aug 2022 22:40:09 +0530 Subject: [PATCH 2/3] Fix tap to close miniplayer --- .dart_tool/package_config.json | 2 +- .packages | 2 +- lib/miniplayer.dart | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.dart_tool/package_config.json b/.dart_tool/package_config.json index 8fc046c..a355b9a 100644 --- a/.dart_tool/package_config.json +++ b/.dart_tool/package_config.json @@ -50,7 +50,7 @@ "languageVersion": "2.17" } ], - "generated": "2022-08-18T09:15:09.050201Z", + "generated": "2022-08-18T09:15:24.925783Z", "generator": "pub", "generatorVersion": "2.17.6" } diff --git a/.packages b/.packages index 2fc212d..ebf89cf 100644 --- a/.packages +++ b/.packages @@ -3,7 +3,7 @@ # # For more info see: https://dart.dev/go/dot-packages-deprecation # -# Generated by pub on 2022-08-18 14:45:09.031156. +# Generated by pub on 2022-08-18 14:45:24.913818. characters:file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/characters-1.2.0/lib/ collection:file:///C:/Users/debji/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/collection-1.16.0/lib/ flutter:file:///C:/flutter/packages/flutter/lib/ diff --git a/lib/miniplayer.dart b/lib/miniplayer.dart index a561361..fbbc817 100644 --- a/lib/miniplayer.dart +++ b/lib/miniplayer.dart @@ -223,10 +223,11 @@ class MiniplayerState extends State with TickerProviderStateMixin { ), ), ), - onTap: () => _snapToPosition( - _dragHeight != widget.maxHeight && widget.useOnTap + onTap: () => widget.useOnTap + ? _snapToPosition(_dragHeight != widget.maxHeight ? PanelState.MAX - : PanelState.MIN), + : PanelState.MIN) + : null, onPanStart: (details) { _startHeight = _dragHeight; updateCount = 0; From 0d46601401799d91fb37916298352bb8243421de Mon Sep 17 00:00:00 2001 From: xayr0n Date: Sun, 21 Aug 2022 10:52:49 +0530 Subject: [PATCH 3/3] Fix derp on heightCode --- lib/miniplayer.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/miniplayer.dart b/lib/miniplayer.dart index fbbc817..255de75 100644 --- a/lib/miniplayer.dart +++ b/lib/miniplayer.dart @@ -441,7 +441,7 @@ class MiniplayerController extends ValueNotifier { ControllerData? valBefore = value; if (state != null) { - value = ControllerData(state.hashCode, duration); + value = ControllerData(state.heightCode, duration); } else { if (height! < 0) return;