'color property for ElevatedButton Widget throws an error

Here's my code:

color is underined red.

body: Row(
        children: <Widget>[
          Text('Hi Mom!'),
          ElevatedButton(
              onPressed: () {  },
              color: Colors.deepPurple,
              child: Text('Hi Dad!'),
          ),
        ],
      ),

It shows the error:

The named parameter 'color' isn't defined.

I also did flutter clean and Invalidate Caches and Restart, doesn't seem to work.

flutter --version:

Flutter 2.10.1 • channel stable • https://github.com/flutter/flutter.git
Framework • revision db747aa133 (11 days ago) • 2022-02-09 13:57:35 -0600
Engine • revision ab46186b24
Tools • Dart 2.16.1 • DevTools 2.9.2


Solution 1:[1]

Try below code hope its help to you. Refer ElevatedButton here

ElevatedButton(
      style: ElevatedButton.styleFrom(
        primary: Colors.deepPurple,
      ),
      onPressed: () {},
      child: Text('Hi Dad!'),
    ),

Your result-> enter image description here

Solution 2:[2]

Use this: MaterialStateProperty

The purpose of MaterialStateProperty is to make it possible to specify different styles for different states i.e.,

        TextButton(
            style: ButtonStyle(
                overlayColor: MaterialStateProperty.all(Colors.redAccent),
                shape: MaterialStateProperty.all(RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(12.0))),
                backgroundColor:
                    MaterialStateProperty.all(Colors.deepOrangeAccent)),
            onPressed: () {
            //
            },
            child: const Text(
              'Hiii',
              style: TextStyle(color: Colors.white),
            ))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ravindra S. Patil
Solution 2 Ujjawal Maurya