【问题标题】:Flutter Slider – Keep it on/off even when user close app & re-visit with GetxFlutter Slider – 即使用户关闭应用程序并使用 Getx 重新访问,也可以保持打开/关闭状态
【发布时间】:2021-12-28 05:02:08
【问题描述】:

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


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;
       });
     },
   );
 }
}

【问题讨论】:

  • 你需要使用像get_storage这样的持久化存储来保存位置值。
  • 你能给我展示一些如何使用持久存储的例子吗?

标签: flutter dart flutter-getx


【解决方案1】:

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

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

您可以使用 shared_prefences,如 official flutter pages 中所述。但这仅适用于简单数据(get_storage 也是)。如果您的应用程序会更复杂并且您必须存储更多数据,您应该考虑使用其他存储方式,例如 sqlite

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

1.添加包到pubspec.yaml:

dependencies:
  shared_preferences: "<newest version>"

2.您的带有 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();
      },
    );
  }
}

【讨论】:

  • 您好 Andreas,非常感谢您的帮助和时间。你的例子正是我想要完成的。不幸的是,我是编码的完整初学者。 Dart 和 Flutter Synthax 对我来说也是未知的。大多数时候,我试图通过以下示例来学习。我不想听起来很贪婪,但是你能用上面的例子告诉我它是如何使用 sqlite 的吗?再次......非常感谢
  • 您好 Carlos,我很高兴在这里为您提供帮助。要使用 sqlite,您应该尝试关注我在答案中链接的officical flutter tutorial。然后你可以尝试编辑它。
  • 好的。我会试试的。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多