保存多种颜色数据

Save multiple color data

我开始 create/develop 一个项目,但在某个时候我卡住了,无法弄清楚。希望大家帮忙!

现在我正在按如下方式更改容器的颜色;

InkWell(
        onTap: (){
           setState(() {
               if(_color == null)
               {
                _color = Colors.blue;
               }
               else if(_color == Colors.blue)
               {
                _color = null;
               }
               });
               },
        onDoubleTap: (){
           setState(() {
           if(_color == null)
           {
           _color = Colors.red;
           }
           else if(_color == Colors.red)
           {
           _color = null;
           }
           });
           },
            child: Container(
               height: _height*0.04,
               child: Container(decoration: BoxDecoration(color: _color),)),),

我想将此颜色保存为 firestore 中的用户信息或将数据保留在 SharedPreferences 中。

另外,我使用的容器数量太多了。我不知道如何 select / 将它们一一获取。我需要你的帮助尽快。谢谢

您可以在点击后存储颜色值。

onTap: () async {
  setState(() {
    if (_color == null) {
      _color = Colors.blue;
    } else if (_color == Colors.blue) {
      _color = null;
    }
  });

  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setInt('colorKey', _color.value);
},

下次启动应用程序时,您需要从 SharedPreferences

中读取颜色
SharedPreferences prefs = await SharedPreferences.getInstance();
final int colorValue = prefs.getInt('colorKey');
if (colorValue != null) {
  _color = Color(colorValue);
}