这是我的课程,它包含带有音量 + 和 - 按钮的滑块 使用需要保存在首选项中的按钮点击滑块后,当我返回到此屏幕时,它需要显示首选项保存的值。
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,
)
],
)
],
),
),
),
);
}
}
我正在尝试保存首选项中的值并获取该值 但滑块 ui 中的值始终显示为 1.0 在初始化状态下它打印正确 如何解决这个问题?
你必须创建一个 SharedPreferencesService() 实例,因为每次它生成一个新实例时,你都会面临这个问题