Home > other >  Giving color from Colors class as a parameter in Flutter
Giving color from Colors class as a parameter in Flutter

Time:01-29

class colorSymbol extends StatelessWidget {
  final Colors color;
  
  colorSymbol(Colors this.color,  {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Icon(
      Icons.accessibility,
      color: ,
      size: 64.0,
    );
  }
}

The code simply return a accessibility icon from Icons and I want this widget to change its color which is given by user. I created a variable from Colors class called color but I cannot move on. Thanks in advance

CodePudding user response:

You like to have Color instead of Colors.

class colorSymbol extends StatelessWidget {
  final Color color;
  const colorSymbol(this.color, {super.key}) ;

CodePudding user response:

You should change your parameter type from Colors to Color :

    import 'package:flutter/material.dart';
    
    class colorSymbol extends StatelessWidget {
      final Color color;
      
      colorSymbol(Color this.color,  {Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Icon(
          Icons.accessibility,
          color: color,
          size: 64.0,
        );
      }
    }
  • Related