Skip to content

Commit

Permalink
mute button
Browse files Browse the repository at this point in the history
  • Loading branch information
qrhfz committed Feb 7, 2024
1 parent 06a4ac0 commit dbee421
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
39 changes: 29 additions & 10 deletions lib/app/interval/interval_route.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:interval/app/interval/interval_controller.dart';
import 'package:interval/app/interval/timer_auido_controller.dart';
import 'package:interval/di.dart';
import 'package:interval/utils/duration_extension.dart';
import 'package:percent_indicator/percent_indicator.dart';
Expand Down Expand Up @@ -80,16 +81,7 @@ class _RunningPageState extends State<RunningPage> {
// backgroundColor: currentTask.color,
appBar: AppBar(
actions: const [
// IconButton(
// onPressed: () {
// setState(() {
// mute = !mute;
// });

// getIt.get<AudioService>().setVolume(mute ? 0 : 100);
// },
// icon: Icon(mute ? Icons.volume_off : Icons.volume_up),
// ),
MuteButton(),
],
),

Expand All @@ -109,6 +101,33 @@ class _RunningPageState extends State<RunningPage> {
}
}

class MuteButton extends StatefulWidget {
const MuteButton({
super.key,
});

@override
State<MuteButton> createState() => _MuteButtonState();
}

class _MuteButtonState extends State<MuteButton> {
final TimerAudioController audio = getIt();
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: audio.mute,
builder: (context, mute, _) {
return IconButton(
onPressed: () {
audio.muteVolume();
},
icon: Icon(mute ? Icons.volume_off : Icons.volume_up),
);
},
);
}
}

class FinishPage extends StatefulWidget {
const FinishPage({super.key});

Expand Down
12 changes: 12 additions & 0 deletions lib/app/interval/timer_auido_controller.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'package:flutter/foundation.dart';
import 'package:just_audio/just_audio.dart';

class TimerAudioController {
final bell = AudioPlayer();
final bell2 = AudioPlayer();

final _mute = ValueNotifier(false);
ValueListenable<bool> get mute => _mute;

bool _isReady = false;

TimerAudioController() {
Expand All @@ -13,6 +17,10 @@ class TimerAudioController {
]).then((value) {
_isReady = true;
});

mute.addListener(() {
setVolume(mute.value ? 0 : 100);
});
}

void setVolume(double n) {
Expand All @@ -35,4 +43,8 @@ class TimerAudioController {
bell2.seek(Duration.zero);
bell2.play();
}

void muteVolume() {
_mute.value = !_mute.value;
}
}

0 comments on commit dbee421

Please sign in to comment.