Flutter Slider – 保持 on/off 即使用户关闭应用程序并使用 Getx 重新访问

Flutter Slider – Keep it on/off even when user close app & re-visit with Getx

Flutter 初学者需要帮助。 即使我们关闭应用程序并使用 Getx 或其他一些 Statemanegment 重新访问应用程序,如何保持滑块在原位并保持值。 按照本教程 Flutter Switch,我尝试使用 Slider 制作相同的东西,但我的编码知识不够,我找不到任何教程来使用 Slider 制作相同的东西。


import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
 const MyApp({Key? key}) : super(key: key);

 static const String _title = 'Flutter Code Sample';

 @override
 Widget build(BuildContext context) {
   return MaterialApp(
     title: _title,
     home: Scaffold(
       appBar: AppBar(title: const Text(_title)),
       body: const MyStatefulWidget(),
     ),
   );
 }
}

class MyStatefulWidget extends StatefulWidget {
 const MyStatefulWidget({Key? key}) : super(key: key);

 @override
 State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
 double _currentSliderValue = 20;

 @override
 Widget build(BuildContext context) {
   return Slider(
     value: _currentSliderValue,
     min: 0,
     max: 100,
     divisions: 5,
     label: _currentSliderValue.round().toString(),
     onChanged: (double value) {
       setState(() {
         _currentSliderValue = value;
       });
     },
   );
 }
}

您链接的教程对于初学者来说似乎过于复杂,因为它同时解释了存储和状态管理。

对我来说似乎更容易,只专注于存储部分。

您可以使用 shared_prefences,如 official flutter pages. But this is only for simple data (get_storage too). If your app will be more complicated and you have to store more data, you should think about some other storage like sqlite 中所述。

因为我不知道 getX,这里有一个 sharedPreferences 的例子:

1.Add 打包到 pubspec.yaml:

dependencies:
  shared_preferences: "<newest version>"

2.Your 代码 shared_preferences:

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatefulWidget(),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  double _currentSliderValue = 20;

  @override
  void initState() {
    super.initState();
    _loadSlider();
  }

  //Loading slider value on start
  void _loadSlider() async {
    final prefs = await SharedPreferences.getInstance();
    setState(() {
      _currentSliderValue = (prefs.getDouble('slider') ?? 20);
    });
  }

  // change slider value to value
  void _changeSlider(double value) {
    setState(() {
      _currentSliderValue = value;
    });
  }

  // store slider value
  void _storeSlider() async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setDouble('slider', _currentSliderValue);
  }

  @override
  Widget build(BuildContext context) {
    return Slider(
      value: _currentSliderValue,
      min: 0,
      max: 100,
      divisions: 5,
      label: _currentSliderValue.round().toString(),
      onChanged: (double value) {
        _changeSlider(value);
        _storeSlider();
      },
    );
  }
}