This is my class it contains slider with volume + and – buttons
Once the slider is tapped using buttons that need to save in pref and when i come back to this screen it need to show the pref saved value.
class AppSettings extends StatefulWidget {
const AppSettings({super.key});
@override
State<AppSettings> createState() => _AppSettingsState();
}
class _AppSettingsState extends State<AppSettings> {
double _volume = 0;
double sliderValue = 0;
double? sliderSavedValue;
final double _minValue = 0.0;
final double _maxValue = 100.0;
bool isSelected = false;
final GlobalKey _sliderKey = GlobalKey();
void toggleSwitch(bool value){
setState(() {
isSelected=!isSelected;
});
}
@override
void initState() {
super.initState();
SharedPreferencesService().getDouble("slider_value").then((value) {
setState(() {
sliderValue = value;
debugPrint("Print slider value : $sliderValue");
SharedPreferencesService().setDouble("slider_value", sliderValue);
});
});
FlutterVolumeController.updateShowSystemUI(false);
FlutterVolumeController.addListener((volume) {
setState(() {
sliderValue = volume;
SharedPreferencesService().setDouble("slider_value", sliderValue);
});
});
}
void _incrementSlider() {
setState(() {
if (sliderValue < _maxValue) {
sliderValue = (sliderValue + 10).clamp(_minValue, _maxValue);
FlutterVolumeController.setVolume(sliderValue);
SharedPreferencesService().setDouble("slider_value", sliderValue);
}
});
}
void _decrementSlider() {
setState(() {
if (sliderValue > _minValue) {
sliderValue = (sliderValue - 10).clamp(_minValue, _maxValue);
FlutterVolumeController.setVolume(sliderValue);
SharedPreferencesService().setDouble("slider_value", sliderValue);
}
});
}
@override
Widget build(BuildContext context) {
debugPrint("Print slider value inside: $sliderValue");
debugPrint("Print slider value inside sliderSavedValue: $sliderSavedValue");
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
elevation: 0.5,
shadowColor: Colors.white,
title: const Row(
children: [
SizedBox(width: 10.0),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"App Settings",
style:
TextStyle(fontSize: 22.0, fontWeight: FontWeight.bold),
)
],
)
],
),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(left: 20.0, right: 12.0, top: 12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.only(bottom: 8.0),
child: Text("Volume", style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),),
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
InkWell(
onTap: () {
_decrementSlider();
},
child: Container(
width: 36,
height: 36,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
color: Colors.white,
border: Border.all(color: Colors.black, width: 2)
),
child: const Icon(Icons.remove),
),
),
Expanded(
child: Slider(
key: _sliderKey,
value: sliderValue,
min: _minValue,
max: _maxValue,
thumbColor: Colors.black,
activeColor: Colors.black,
inactiveColor: AppColors.colorDarkGrey,
onChanged: (newValue) {
setState(() {
sliderValue = newValue;
FlutterVolumeController.setVolume(newValue);
SharedPreferencesService().setDouble("slider_value", sliderValue);
});
},
),
),
InkWell(
onTap: _incrementSlider,
child: Container(
width: 36,
height: 36,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
color: Colors.black,
border: Border.all(color: Colors.black, width: 2)
),
child: const Icon(Icons.add, color: Colors.white,),
),
),
],
),
const Padding(
padding: EdgeInsets.only(top: 14.0, bottom: 18),
child: Text("", style: TextStyle(fontSize: 18),),
),
Row(
children: [
const Text("", style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),),
const Spacer(),
Switch(
value: isSelected ,
onChanged: toggleSwitch,
activeColor: Colors.white,
inactiveThumbColor: Colors.black,
activeTrackColor: Colors.black,
)
],
)
],
),
),
),
);
}
}
I am trying to save the value in preferences and get that value
BUt in slider ui always value shows as 1.0
in init state it prints correctly
How to fix this issue?