Home > Enterprise >  flutter elevated button color not changing
flutter elevated button color not changing

Time:03-10

I am not able to change the background of flutter button. please help.

the error that VS code shows is "Invalid constant value.", underlining the MaterialStateProperty.all(Colors.red) line

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

// This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
  return const MaterialApp(
     home: Home(),
  );
  }
}

class Home extends StatelessWidget {
 const Home({Key? key}) : super(key: key);

 @override
 Widget build(BuildContext context) {
   return Scaffold(
      appBar: AppBar(
      title: const Text('My first  Name'),
      centerTitle: true,
      backgroundColor: Colors.red[300],
      ),
     body: const Center(
       child: ElevatedButton(
          onPressed: null,
          child: Text('click me'),
          style: ButtonStyle(
             backgroundColor: MaterialStateProperty.all(Colors.red),
          )
       ),
     ),
   );
 }
}

CodePudding user response:

Try below code hope its help to you. Remove const keyword

       Center(
              child: ElevatedButton(
                onPressed: null,
                child: Text('click me'),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.red),
                ),
              ),
            ),

Other way:

      ElevatedButton(
                onPressed: () {},
                child: Text('click me'),
                style: ElevatedButton.styleFrom(
                  primary: Colors.red,
                ),
              ),

CodePudding user response:

May be this one help you

ElevatedButton(
                  style: ButtonStyle(
                      textStyle: MaterialStateProperty.all(TextStyle(
                          color: Colors.white,
                          backgroundColor: Colors.green))),
                  onPressed: () {
                    print('pressed');
                  },
                  child: Text('PRESS'),
                )

CodePudding user response:

ElevatedButton(
              style: ElevatedButton.styleFrom(
                primary: Colors.blueAccent,
              ),
              onPressed: () {},
              child: const Text(
                "Button",
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
  • Related