优先使用颤动来节省色彩 [英] Sharedpreferencs with flutter to save color

查看:59
本文介绍了优先使用颤动来节省色彩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望容器是一种颜色,它来自sharedpreference或类似的东西!是否有一个小部件来解决这个问题!或者我可以使用sharedpreference来做到这一点!!如果我能!如何?

I want a container to be of a color, that comes from sharedpreference or something like that! Is there a widget to solve this!! Or can I just do this with sharedpreference!! If I can! How?

推荐答案

随风飘扬的 SharedPreferences 插件,您可以只能保存String, int, StringList, double, Bool.

With flutter SharedPreferences plugin you can only save String, int, StringList, double, Bool.

一种解决方法是将颜色的RGBO值保存在SharedPreferences中,这将适用于Android和iOS.

A work around would be to save the RGBO value of the color in SharedPreferences, this would work for Android and iOS.

第1步.安装插件

pubspec.yaml

将SharedPreferences添加到您的pubspec.yaml文件.通过点击此处来检查最新版本.

Add SharedPreferences to your pubspec.yaml file. Check the last version by clicking here.

dependencies:
  flutter:
    sdk: flutter

  shared_preferences: ^0.5.3+4

第2步.将RGBO值保存在SharedPreferences中

  void saveColor(int r, int g, int b, double opacity) async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setInt('r', r);
    prefs.setInt('g', g);
    prefs.setInt('b', b);
    prefs.setDouble('o', opacity);
  }

第3步.获取值并创建颜色

getColor() async {
    final prefs = await SharedPreferences.getInstance();
    final r = prefs.getInt('r');
    final g = prefs.getInt('g');
    final b = prefs.getInt('b');
    final opacity = prefs.getDouble('o');
    return Color.fromRGBO(r, g, b, opacity);
  }

这篇关于优先使用颤动来节省色彩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆