Site icon AndroidRide

3 Ways To Change Elevated Button Color In Flutter

Do you want to change color of Elevated Button?

Then you are in the right place.

So let’s start with a simple example.

Most of the time, this is the one you really needed.

How To Change Elevated Button Color?

change elevatedbutton color


ElevatedButton(
                  child: const Text('ElevatedButton'),
                  onPressed: () {},
                  style: ElevatedButton.styleFrom(
                    primary: Colors.deepOrangeAccent,
                    //onPrimary: Colors.black,
                  ),
                ),

How To Change Elevated Button Color Using Theme?

elevated button color change using theme widget
Sometimes you need to set same color and text color for a group of ElevatedButtons used in a form or section. You don’t need to use style property for each one. In these situations, You can use Theme widget.


Theme(
                  data: ThemeData(
                    elevatedButtonTheme: ElevatedButtonThemeData(
                      style: ElevatedButton.styleFrom(
                        onPrimary: Colors.white,
                        primary: Colors.green,
                      ),
                    ),
                  ),
                  child: Column(
                    children: [
                      ElevatedButton(
                        child: const Text('ElevatedButton 1'),
                        onPressed: () {},
                      ),
                     const SizedBox(
                        height: 10,
                      ),
                      ElevatedButton(
                        child: const Text('ElevatedButton 2'),
                        onPressed: () {},
                      ),
                    ],
                  ),
                )

How To Change Elevated Button Color Using Theme – App level


return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter ElevatedButton Color Change Example',
      theme: ThemeData(
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
            primary: Colors.red,
          ),
        ),
      ),

Complete Source Code

elevated button color flutter

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter ElevatedButton Color Change Example',
      theme: ThemeData(
        //primarySwatch: Colors.red,
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
            primary: Colors.red,
          ),
        ),
      ),
      home: SafeArea(
        child: Scaffold(
          appBar: AppBar(
            title: const Text('Flutter ElevatedButton Color Change Example'),
          ),
          body: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
        //It uses applevel theme color
        ElevatedButton(
                  child: const Text('ElevatedButton'),
                  onPressed: () {},
                ),
//Here It uses own style color by overriding app level color.
                ElevatedButton(
                  child: const Text('ElevatedButton'),
                  onPressed: () {},
                  style: ElevatedButton.styleFrom(
                    primary: Colors.deepOrangeAccent,
                    onPrimary: Colors.black,
                  ),
                ),
//Here, Theme provides color for the ElevatedButton.
                Theme(
                  data: ThemeData(
                    elevatedButtonTheme: ElevatedButtonThemeData(
                      style: ElevatedButton.styleFrom(
                        primary: Colors.green,
                      ),
                    ),
                  ),
                  child: Column(
                    children: [
                      ElevatedButton(
                        child: const Text('ElevatedButton 1'),
                        onPressed: () {},
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      ElevatedButton(
                        child: const Text('ElevatedButton 2'),
                        onPressed: () {},
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

If you like this post, please share it with your family and friends.
Thanks.

Exit mobile version