added settings to toggle the audio

This commit is contained in:
Rene Kievits
2025-10-28 18:55:38 +01:00
parent a572a6e6fc
commit 4d6ec05162
3 changed files with 144 additions and 31 deletions

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../services/deck_repository.dart';
import 'home_screen.dart';
@@ -12,6 +13,8 @@ class SettingsScreen extends StatefulWidget {
class _SettingsScreenState extends State<SettingsScreen> {
final TextEditingController _apiKeyController = TextEditingController();
bool _playAudio = true;
bool _playCorrectSound = true;
@override
void dispose() {
@@ -19,6 +22,26 @@ class _SettingsScreenState extends State<SettingsScreen> {
super.dispose();
}
@override
void initState() {
super.initState();
_loadSettings();
final repo = Provider.of<DeckRepository>(context, listen: false);
repo.loadApiKey().then((key) {
if (key != null) {
_apiKeyController.text = key;
}
});
}
Future<void> _loadSettings() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_playAudio = prefs.getBool('playAudio') ?? true;
_playCorrectSound = prefs.getBool('playCorrectSound') ?? true;
});
}
Future<void> _saveApiKey() async {
final apiKey = _apiKeyController.text.trim();
if (apiKey.isEmpty) return;
@@ -32,22 +55,11 @@ class _SettingsScreenState extends State<SettingsScreen> {
);
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => HomeScreen()),
MaterialPageRoute(builder: (_) => const HomeScreen()),
);
}
}
@override
void initState() {
super.initState();
final repo = Provider.of<DeckRepository>(context, listen: false);
repo.loadApiKey().then((key) {
if (key != null) {
_apiKeyController.text = key;
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
@@ -85,6 +97,48 @@ class _SettingsScreenState extends State<SettingsScreen> {
),
child: const Text('Save & Start Quiz'),
),
const SizedBox(height: 24),
SwitchListTile(
title: const Text(
'Play audio for vocabulary',
style: TextStyle(color: Colors.white),
),
value: _playAudio,
onChanged: (value) async {
final prefs = await SharedPreferences.getInstance();
prefs.setBool('playAudio', value);
setState(() {
_playAudio = value;
});
},
activeThumbColor: Colors.blueAccent,
inactiveThumbColor: Colors.grey,
tileColor: const Color(0xFF1E1E1E),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
),
const SizedBox(height: 12),
SwitchListTile(
title: const Text(
'Play sound on correct answer',
style: TextStyle(color: Colors.white),
),
value: _playCorrectSound,
onChanged: (value) async {
final prefs = await SharedPreferences.getInstance();
prefs.setBool('playCorrectSound', value);
setState(() {
_playCorrectSound = value;
});
},
activeThumbColor: Colors.blueAccent,
inactiveThumbColor: Colors.grey,
tileColor: const Color(0xFF1E1E1E),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
),
],
),
),