Skip to content

Commit

Permalink
feat: volume slider in player page
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Jun 15, 2023
1 parent 2b35c04 commit 7abe2c1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 51 deletions.
66 changes: 66 additions & 0 deletions lib/components/player/volume_slider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotube/collections/spotube_icons.dart';
import 'package:spotube/provider/volume_provider.dart';

class VolumeSlider extends HookConsumerWidget {
final bool fullWidth;
const VolumeSlider({
Key? key,
this.fullWidth = false,
}) : super(key: key);

@override
Widget build(BuildContext context, ref) {
final volume = ref.watch(volumeProvider);
final volumeNotifier = ref.watch(volumeProvider.notifier);

var slider = Listener(
onPointerSignal: (event) async {
if (event is PointerScrollEvent) {
if (event.scrollDelta.dy > 0) {
final value = volume - .2;
volumeNotifier.setVolume(value < 0 ? 0 : value);
} else {
final value = volume + .2;
volumeNotifier.setVolume(value > 1 ? 1 : value);
}
}
},
child: Slider.adaptive(
min: 0,
max: 1,
value: volume,
onChanged: volumeNotifier.setVolume,
),
);
return Row(
mainAxisAlignment:
!fullWidth ? MainAxisAlignment.center : MainAxisAlignment.start,
children: [
IconButton(
icon: Icon(
volume == 0
? SpotubeIcons.volumeMute
: volume <= 0.2
? SpotubeIcons.volumeLow
: volume <= 0.6
? SpotubeIcons.volumeMedium
: SpotubeIcons.volumeHigh,
size: 16,
),
onPressed: () {
if (volume == 0) {
volumeNotifier.setVolume(1);
} else {
volumeNotifier.setVolume(0);
}
},
),
if (fullWidth) Expanded(child: slider) else slider,
],
);
}
}
53 changes: 2 additions & 51 deletions lib/components/root/bottom_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:spotube/components/player/player_actions.dart';
import 'package:spotube/components/player/player_overlay.dart';
import 'package:spotube/components/player/player_track_details.dart';
import 'package:spotube/components/player/player_controls.dart';
import 'package:spotube/components/player/volume_slider.dart';
import 'package:spotube/extensions/constrains.dart';
import 'package:spotube/extensions/context.dart';
import 'package:spotube/hooks/use_brightness_value.dart';
Expand Down Expand Up @@ -115,57 +116,7 @@ class BottomPlayer extends HookConsumerWidget {
Container(
height: 40,
constraints: const BoxConstraints(maxWidth: 250),
child: HookBuilder(builder: (context) {
final volume = ref.watch(volumeProvider);
final volumeNotifier =
ref.watch(volumeProvider.notifier);

return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(
volume == 0
? SpotubeIcons.volumeMute
: volume <= 0.2
? SpotubeIcons.volumeLow
: volume <= 0.6
? SpotubeIcons.volumeMedium
: SpotubeIcons.volumeHigh,
size: 16,
),
onPressed: () {
if (volume == 0) {
volumeNotifier.setVolume(1);
} else {
volumeNotifier.setVolume(0);
}
},
),
Listener(
onPointerSignal: (event) async {
if (event is PointerScrollEvent) {
if (event.scrollDelta.dy > 0) {
final value = volume - .2;
volumeNotifier
.setVolume(value < 0 ? 0 : value);
} else {
final value = volume + .2;
volumeNotifier
.setVolume(value > 1 ? 1 : value);
}
}
},
child: Slider.adaptive(
min: 0,
max: 1,
value: volume,
onChanged: volumeNotifier.setVolume,
),
),
],
);
}),
child: const VolumeSlider(),
)
],
)
Expand Down
20 changes: 20 additions & 0 deletions lib/pages/player/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:spotube/collections/assets.gen.dart';
import 'package:spotube/collections/spotube_icons.dart';
import 'package:spotube/components/player/player_actions.dart';
import 'package:spotube/components/player/player_controls.dart';
import 'package:spotube/components/player/volume_slider.dart';
import 'package:spotube/components/shared/animated_gradient.dart';
import 'package:spotube/components/shared/dialogs/track_details_dialog.dart';
import 'package:spotube/components/shared/page_window_title_bar.dart';
Expand Down Expand Up @@ -247,6 +248,25 @@ class PlayerView extends HookConsumerWidget {
const SizedBox(width: 10),
],
),
const SizedBox(height: 25),
SliderTheme(
data: theme.sliderTheme.copyWith(
activeTrackColor: titleTextColor,
inactiveTrackColor: bodyTextColor,
thumbColor: titleTextColor,
overlayColor: titleTextColor?.withOpacity(0.2),
trackHeight: 2,
thumbShape: const RoundSliderThumbShape(
enabledThumbRadius: 6,
),
),
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: VolumeSlider(
fullWidth: true,
),
),
),
],
),
),
Expand Down

0 comments on commit 7abe2c1

Please sign in to comment.