DIGITALSQUAD
DIGITALSQUAD

Reputation: 3534

How to change ElevatedButton text color in elevatedButtonTheme?

I'm trying to change ElevatedButton text color in elevatedButtonTheme property in theme but cannot change. I know that TextStyle in the child of Text can change the color of Text, but I prefer to define in elevatedButtonTheme.

import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Demo Home Page with 2.0'),
      theme: ThemeData(
        primaryColor: HexColor('#003f71'),
        accentColor: HexColor('#e09e36'),
        scaffoldBackgroundColor: HexColor('#003f71'),
        textTheme: TextTheme(bodyText2: TextStyle(fontSize: 16.0), button: TextStyle(fontSize: 16.0)),
        elevatedButtonTheme:
            ElevatedButtonThemeData(style: ElevatedButton.styleFrom(minimumSize: Size(1, 45), primary: HexColor('#e09e36'), textStyle: TextStyle(fontSize: 16.0, color: Colors.black))),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        alignment: Alignment.center,
        margin: const EdgeInsets.all(15.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              margin: const EdgeInsets.symmetric(vertical: 15.0),
              child: FractionallySizedBox(
                alignment: Alignment.center,
                widthFactor: 1.0,
                child: ElevatedButton(onPressed: () {}, child: Text('ElevatedButton')),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

enter image description here

Upvotes: 37

Views: 37821

Answers (5)

RAMESH- -S
RAMESH- -S

Reputation: 1

SizedBox(
            height: 60,
            width: 120,
            child: ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const ProgressScreen(),
                  ),
                );
              },
              child: const SizedBox(
                  child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                    Text(
                      "0",
                      style:
                          TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.end,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'PROGRESS',
                          style: TextStyle(
                              fontSize: 13, fontWeight: FontWeight.bold),
                        ),
                      ],
                    ),
                  ])),
            )),

Upvotes: 0

Gil Cohen
Gil Cohen

Reputation: 351

Actually, I saw the opposite of Mihai's answer happening:

ElevatedButton(
    style: ElevatedButton.styleFrom(
        foregroundColor: Colors.white, // text color
        backgroundColor: Colors.black), // background color
  ),

Upvotes: 13

Mihai
Mihai

Reputation: 1289

Since primay and on primary are deprecated, here is the new way to define the button color and the button text color:

ElevatedButton(
      style: ElevatedButton.styleFrom(
        foregroundColor: Colors.white, // change background color of button
        backgroundColor: Colors.purple, // change text color of button
      ),
      child: Text('Your Text Here'),
      onPressed: () {},
    );

Upvotes: 5

Amit Maurya
Amit Maurya

Reputation: 686

Here is code snippet if you want to use theme then here it is :

MaterialApp(
        theme: ThemeData(
          elevatedButtonTheme: ElevatedButtonThemeData(
              style: ElevatedButton.styleFrom(
            onPrimary: Colors.yellow,
          )),
        ),
        home: MyWidget());

without setting text theme you can change color of text like this.

Container(
                    width: MediaQuery.of(context).size.width * 0.6,
                    child: ElevatedButton(
                      onPressed: () {},
                      style: ElevatedButton.styleFrom(
                        foregroundColor: Colors.pinkAccent,//change background color of button
                        backgroundColor: Colors.yellow,//change text color of button
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(25),
                        ),
                        elevation: 15.0,
                      ),
                      child: Padding(
                        padding: const EdgeInsets.all(15.0),
                        child: Text(
                          'Proceed to Pay',
                          style: TextStyle(fontSize: 20),
                        ),
                      ),
                    ),
                  ),

Upvotes: 43

vksssd
vksssd

Reputation: 67

`Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
                        foregroundColor: Colors.white,//change background color of button
                        backgroundColor: Colors.blue,//change text color of button
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(25),
                        ),
                        elevation: 15.0,
                      ),
        child: Text('Answer 1'),
        onPressed: selectHandler,
      ),
    );
  }`

primary and onprimary are not in use

Upvotes: 1

Related Questions