【问题标题】:How to save selected color via shared preferences in flutter?如何通过颤动中的共享首选项保存选定的颜色?
【发布时间】:2020-11-13 18:33:55
【问题描述】:

我在保存选定的颜色方面已经卡住了很长一段时间。我有一列按钮,允许用户选择颜色来更改框。我认为我可能必须将十六进制颜色值转换为 int 上的字符串,然后通过共享首选项保存。问题是,我不明白如何正确地为颜色执行此操作。我意识到它必须类似于任何其他共享首选项输入(关于字符串或 int)。

任何帮助将不胜感激。

代码如下:

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

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
  const HomePage({Key key, this.title}) : super(key: key);
  final String title;
}

//Green Theme
const lightGreen = const Color(0xff55efc4);
const darkGreen = const Color(0xff00b894);

//Blue Theme
const lightBlue = const Color(0xff74b9ff);
const darkBlue = const Color(0xff0984e3);

//Red Theme
const lightRed = const Color(0xffff7675);
const darkRed = const Color(0xffd63031);

class _HomePageState extends State<HomePage> {
  Color lightColor = lightGreen;
  String _lightColor = "lightColor";
  Color darkColor = darkGreen;
  bool isDark = false;

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

  Future<void> saveColor() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString('switchColor', _lightColor);
  }

  Future<String> getColor() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return _lightColor = prefs.getString("switchColor");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: Text('theme switcher'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          SizedBox(
            height: 25.0,
          ),
          Container(
            height: 100,
            width: 300,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  lightColor,
                  darkColor,
                ],
              ),
            ),
          ),
          SizedBox(
            height: 10.0,
          ),
          Column(
            children: <Widget>[
              //Change Green
              RaisedButton(
                onPressed: () {
                  setState(
                    () {
                      lightColor = lightGreen;
                      darkColor = darkGreen;
                      saveColor();
                    },
                  );
                },
                textColor: Colors.white,
                padding: const EdgeInsets.all(0.0),
                child: Container(
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      colors: [
                        lightGreen,
                        darkGreen,
                      ],
                    ),
                  ),
                  padding: const EdgeInsets.all(10.0),
                  alignment: Alignment.center,
                  child: Text(
                    "Green",
                    textAlign: TextAlign.center,
                    style: TextStyle(fontSize: 10.0),
                  ),
                ),
              ),
              //Change Blue
              RaisedButton(
                onPressed: () {
                  setState(
                    () {
                      lightColor = lightBlue;
                      darkColor = darkBlue;
                      saveColor();
                    },
                  );
                },
                textColor: Colors.white,
                padding: const EdgeInsets.all(0.0),
                child: Container(
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      colors: [
                        lightBlue,
                        darkBlue,
                      ],
                    ),
                  ),
                  padding: const EdgeInsets.all(10.0),
                  alignment: Alignment.center,
                  child: Text(
                    "Blue",
                    textAlign: TextAlign.center,
                    style: TextStyle(fontSize: 10.0),
                  ),
                ),
              ),
              //Change Red
              RaisedButton(
                onPressed: () {
                  setState(
                    () {
                      lightColor = lightRed;
                      darkColor = darkRed;
                      saveColor();
                    },
                  );
                },
                textColor: Colors.white,
                padding: const EdgeInsets.all(0.0),
                child: Container(
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      colors: [
                        lightRed,
                        darkRed,
                      ],
                    ),
                  ),
                  padding: const EdgeInsets.all(10.0),
                  alignment: Alignment.center,
                  child: Text(
                    "Red",
                    textAlign: TextAlign.center,
                    style: TextStyle(fontSize: 10.0),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: flutter button colors save sharedpreferences


    【解决方案1】:

    将颜色值初始化为整数,

    //Green Theme
    const int lightGreenVal = 0xff55efc4;
    const int darkGreenVal = 0xff00b894;
    int colorVal = 0;
    

    在共享首选项中保存颜色的 int 值

    Future<void> saveColor() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        prefs.setInt("switchColorValue", lightGreenVal);
    }
    

    使用键值获取颜色的int值,

    Future<int> getColor() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        return colorVal = prefs.getInt("switchColorValue");
    }
    

    终于可以用colorVal点赞了,

    Container(
        height: 100,
        width: 300,
        color: Color(colorVal),
    ),
    

    【讨论】:

    • 非常感谢!这完美!我还不能投票给你的帖子,因为我只有 1 个代表点,但是一旦我有 15 个代表点,我会确保回来投票给这个帖子。再次感谢。
    • 如果您觉得有用,请采纳答案
    • 我没有意识到你可以做到这一点,但我只是接受了你的回答。再次感谢!
    猜你喜欢
    • 2020-06-30
    • 2021-12-26
    • 2020-11-12
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 2020-07-08
    • 2021-08-16
    • 1970-01-01
    相关资源
    最近更新 更多