Home > OS >  Flutter does not accept Color const
Flutter does not accept Color const

Time:12-03

i created a file with the colors that i want to use in my project, but when i try to used, flutter does not acept the variables as a correct color.

This is the colors file:

import 'package:flutter/material.dart';

class ColorSkh {
     static Color innovation = const Color.fromARGB(255, 59, 18, 45);
     static Color fresh = const Color.fromARGB(255, 172, 155, 163);
     static Color hightech = const Color.fromARGB(255, 0, 128, 157);
     static Color sophisticated = const Color.fromARGB(255, 230, 231, 232);
     static Color sk = const Color.fromARGB(255, 191, 205, 217);
     static Color white = const Color.fromARGB(255, 255, 255, 255);
}

And this is how i am using it



import 'package:analytic_skh/ui/color_skh.dart';
.
.
.


SizedBox(
                *height: height * 0.10,
                child: Align(
                    alignment: Alignment.centerLeft,
                    child: ElevatedButton.icon(
                      onPressed: onPressed,
                      icon: const Icon(Icons.settings),
                      label: const Text('Settings',
                          style:
                              TextStyle(backgroundColor: ColorSkh.innovation)),
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.white,
                      ),
                    )),
              )*




but i get the error that this is a invalid constant

CodePudding user response:

Change your static colors to the const like below.

class ColorSkh {
  static const Color innovation = Color.fromARGB(255, 59, 18, 45);
  static const Color fresh = Color.fromARGB(255, 172, 155, 163);
  static const Color hightech = Color.fromARGB(255, 0, 128, 157);
  static const Color sophisticated = Color.fromARGB(255, 230, 231, 232);
  static const Color sk = Color.fromARGB(255, 191, 205, 217);
  static const Color white = Color.fromARGB(255, 255, 255, 255);
}

CodePudding user response:

You need to remove the color from the Text widget since it ca be constant anymore because those color variables are static and not final, so try this:

SizedBox(
            *height: height * 0.10,
            child: Align(
                alignment: Alignment.centerLeft,
                child: ElevatedButton.icon(
                  onPressed: onPressed,
                  icon: const Icon(Icons.settings),
                  label: Text('Settings',
                      style:
                          TextStyle(backgroundColor: ColorSkh.innovation)),
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.white,
                  ),
                )),
          )*
  • Related