Drakares
Drakares

Reputation: 3

The method 'withValues' isn't defined for the type 'Color'

i have error with withValues my flutter version is 3.24.5 my code:

import 'package:eClassify/ui/theme/theme.dart';
import 'package:flutter/material.dart';

enum AppTheme { dark, light }

final appThemeData = {
  AppTheme.light: ThemeData(
    // scaffoldBackgroundColor: pageBackgroundColor,
    brightness: Brightness.light,
    //textTheme
    useMaterial3: false,
    fontFamily: "Manrope",
    textSelectionTheme: const TextSelectionThemeData(
      selectionColor: territoryColor_,
      cursorColor: territoryColor_,
      selectionHandleColor: territoryColor_,
    ),
    switchTheme: SwitchThemeData(
      thumbColor: const MaterialStatePropertyAll(territoryColor_),
      trackColor: MaterialStateProperty.resolveWith((states) {
        if (states.contains(MaterialState.selected)) {
          return territoryColor_.withValues(alpha: 0.3);
        }
        return primaryColorDark;
      }),
    ),
    colorScheme: ColorScheme.fromSeed(
        error: errorMessageColor,
        seedColor: territoryColor_,
        brightness: Brightness.light),
  ),
  AppTheme.dark: ThemeData(
    brightness: Brightness.dark,
    useMaterial3: false,
    fontFamily: "Manrope",
    textSelectionTheme: const TextSelectionThemeData(
      selectionHandleColor: territoryColorDark,
      selectionColor: territoryColorDark,
      cursorColor: territoryColorDark,
    ),
    colorScheme: ColorScheme.fromSeed(
        error: errorMessageColor.withValues(alpha: 0.7),
        seedColor: territoryColorDark,
        brightness: Brightness.dark),
    switchTheme: SwitchThemeData(
        thumbColor: const MaterialStatePropertyAll(territoryColor_),
        trackColor: MaterialStateProperty.resolveWith((states) {
          if (states.contains(MaterialState.selected)) {
            return territoryColor_.withValues(alpha: 0.3);
          }
          return primaryColor_.withValues(alpha: 0.2);
        })),
  )
};

i have try to create extension method but still didn't work. ...........................................................................................................................................................................................................................................

Upvotes: 0

Views: 34

Answers (2)

Abion47
Abion47

Reputation: 24736

As an addition to the selected answer, you can also create an extension method for Color.withValues to get some forward compatibility. That way, when you upgrade to Flutter 3.27, you only need to delete the extension method instead of combing through your code upgrading each instance of withOpacity to withValues:

import 'dart:ui';

extension ColorWithValuesExt on Color {
  /// Returns a new color with the provided components updated.
  /// Each component (alpha, red, green, blue) represents a floating-point value; see 
  /// [Color.from](https://api.flutter.dev/flutter/dart-ui/Color/Color.from.html) for details and examples.
  Color withValues({double? alpha, double? red, double? green, double? blue, ColorSpace? colorSpace}) {
    final r = red == null ? this.red : (255 * red).round();
    final g = green == null ? this.green : (255 * green).round();
    final b = blue == null ? this.blue : (255 * blue).round();
    return Color.fromRGBO(r, g, b, alpha ?? opacity);
  }
}

Upvotes: 0

Peter Koltai
Peter Koltai

Reputation: 9789

This is because of a change introduced in Flutter version 3.27, see here, and you are using an earlier version in which withValues does not exist.

Either upgrade to the latest Flutter, or if you want to use your current version, instead of:

territoryColor_.withValues(alpha: 0.3)

use this:

territoryColor_.withOpacity(0.3)

Upvotes: 1

Related Questions