Home > OS >  Store and retrieve colors in firestore flutter
Store and retrieve colors in firestore flutter

Time:04-02

I want to be able to store and use user selected text and background colors in firestore. I've tried storing them as strings and ints but the problem I run into is trying to use future< int> in Color() isn't accepted and future< String> isn't accepted in the "color:" parameter.

Right now I'm getting the hex value and storing it in firestore:

String hexColor() {
    late String c;
    if (BayTextColorController.selected.value == "Amber") {
      c = '0xFFFFAB40';
    } else if (BayTextColorController.selected.value == "Black") {
      c = '0xFF000000';
    }
    return c;
  }

How I've tried using ints:

    Future<int> bay56TextColor(DocumentSnapshot snapshot) async {
      String s = await snapshot.get('bay56TextColor');
      late int c = int.parse(s);
      return c;
    }

I'm trying to color the user submitted text from this future builder:

FutureBuilder<dynamic>(
       future: bay56(snapshot.data!),
       builder: (context, snapshot) {
                              
       if (snapshot.hasData) {
          return Text(
            ' '   snapshot.data.toString()   ' ',
               style: TextStyle(
                  color: Colors.black,  //Color goes here
                  backgroundColor: Colors.white));  //and here
                              }

Is there a way to incorporate multiple futures into one future builder?

I should note that my text is from one field and I'm saving the colors in another field in the same document.

CodePudding user response:

Store color in this format.

#FF6F00

Get color using this method

class HexColor extends Color {
  static int _getColorFromHex(String hexColor) {
    hexColor = hexColor.toUpperCase().replaceAll("#", "");
    if (hexColor.length == 6) {
      hexColor = "FF"   hexColor;
    }
    return int.parse(hexColor, radix: 16);
  }

  HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}

Example

color: HexColor(color code is here)

  • Related