Open In App

Flutter - ElevatedButton Widget

Last Updated : 04 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Elevated Button is a Flutter component included inside the material package, i.e., "package:flutter/material.dart". The main characteristic of these buttons is the slight elevation in their surface towards the screen when tapped by the user. In simple language, elevated buttons are un-deprecated raised buttons with no explicitly defined button styling. Elevated Buttons cannot be styled, i.e., you cannot modify the color of the button, font size, text style, etc, explicitly like raised buttons. This class was launched in version 1.22 of Flutter. You can pass text or icons as a child to them. To handle the styling of the Elevated Button, a ButtonStyle class is used, which allows the styling of a button according to requirements. 

Constructor for Elevated Button

ElevatedButton.icon({
Key? key,
required VoidCallback? onPressed,
VoidCallback? onLongPress,
ValueChanged<bool>? onHover,
ValueChanged<bool>? onFocusChange,
ButtonStyle? style,
FocusNode? focusNode,
bool? autofocus,
Clip? clipBehavior,
required Widget icon,
required Widget label
})

Parameters

Elevated Button offers two important parameters:

1. child: This represents the button's label.

ElevatedButton(
child: const Text('Raised Button'),

),

2. onPressed: This represents the action to be executed when the button is tapped

onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => const NewScreen())),

Here, when the button is tapped, a navigation action to NewScreen is executed. 

Properties of Elevated Button

Property

Description

autofocus

Gives a boolean value corresponding to the initial focus of the button

clipBehaviour

Decides whether the content is clipped or not

focusNode

Represents the focus node of the widget

ButtonStyle

Decides the styling of the button

onLongPress

The action to be executed when the button is long-pressed by the user

enabled

Gives a boolean value for the activity of the button

hashcode

Decides the hashcode of the button

Key

Controls how one widget replaces another widget in the tree

onFocusChanged

A method to be executed on changing the focus of the button

onHover

Actin is to be executed when the user hovers over the button

Methods Provided in an Elevated Button

Method

Description

createElement()

Create a StatefulElement to manage the button's location in the widget tree

createState()

Creates the mutable state for this widget at a given location in the tree

build(BuildContext context)

Describes the part of the user interface

createElement()

Creates a StatelessElement to manage this widget's location in the tree

debugDescribeChildren()

Returns a list of DiagnosticsNode objects describing this node's children

debugFillProperties()

Add additional properties associated with the node

noSuchMethod()

Invoked when a non-existent method or property is accessed

toDiagnosticsNode()

Returns a debug representation of the object that is used by debugging tools 

toString()

A string representation of this object

toStringDeep()

Returns a string representation of this node and its descendants

toStringShallow()

Returns a one-line detailed description of the object

toStringShort()

A short, textual description of this widget


Styling a Button

The styling of an elevated button is quite different from the rest of the buttons. You have to use ButtonStyle as a style parameter. After that, pass ElevatedButtonThemeData is passed as the button theme to ThemeData. Styling for the elevated button is done using ElevatedButton.styleFrom.

Specific styling for a button is explicitly done using the ButtonStyle parameter as given below:

Dart
ElevatedButton(
  child: Text('Elevated Button'),
  style: ElevatedButton.styleFrom(
    backgroundColor: Colors.green,
    foregroundColor: Colors.white,
  ),
  onPressed: () {},
),


Output:

elevatedbutton1


Changing Text Style

Passing values to the textStyle property of the button helps in altering the text styling. 

Dart
ElevatedButton(
  child: Text('Elevated Button'),
  style: ElevatedButton.styleFrom(
    backgroundColor: Colors.green,
    foregroundColor: Colors.white,
    textStyle: const TextStyle(
      color: Colors.white,
      fontSize: 10,
      fontStyle: FontStyle.normal,
    ),
  ),
  onPressed: () {},
),


Output:

elevatedbutton2


Changing Border

Using BorderSide as a parameter to the side property of the Elevated Button helps in modifying the border. 

Dart
ElevatedButton(
  child: Text('Elevated Button'),
  style: ElevatedButton.styleFrom(
    backgroundColor: Colors.green,
    foregroundColor: Colors.white,
    side: BorderSide(color: Colors.yellow, width: 5),
    textStyle: const TextStyle(
        color: Colors.white, fontSize: 25, fontStyle: FontStyle.normal),
  ),
  onPressed: () {},
),


Output:

elevatedbutton3


Changing Shape and Shadow

The shape can be changed by passing OutlinedBorder as a parameter to the shape property and shadow can be handled by giving color to shadowColor property. Shadow color is visible only when the button is tapped.

Shape:

Dart
ElevatedButton(
  child: Text('Elevated Button'),
  style: ElevatedButton.styleFrom(
    backgroundColor: Colors.green,
    foregroundColor: Colors.white,
    // side: BorderSide(color: Colors.yellow, width: 5),
    textStyle: const TextStyle(
      color: Colors.white,
      fontSize: 25,
      fontStyle: FontStyle.normal,
    ),
    shape: BeveledRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(10)),
    ),
  ),
  onPressed: () {},
),


Output:

elevatedbutton4


Shadow:

Dart
ElevatedButton(
  child: Text('Elevated Button'),
  style: ElevatedButton.styleFrom(
    backgroundColor: Colors.green,
    foregroundColor: Colors.white,
    // side: BorderSide(color: Colors.yellow, width: 5),
    textStyle: const TextStyle(
        color: Colors.white, fontSize: 25, fontStyle: FontStyle.normal),
    shape: BeveledRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(10))),
    shadowColor: Colors.lightBlue,
  ),
  onPressed: () {},
),


Output:


Let's understand the above explanation with the help of the code given below.

Implementation

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

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

class HomeApp extends StatefulWidget {
  HomeApp({Key? key}) : super(key: key);

  @override
  State<HomeApp> createState() => _HomeAppState();
}

class _HomeAppState extends State<HomeApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
            appBar: AppBar(
              backgroundColor: Colors.green,
              foregroundColor: Colors.white,
              title: const Text('GeeksforGeeks'),
            ),
            body: const FirstScreen()));
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: ElevatedButton(
          child: Text('Elevated Button'),
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.green,
            foregroundColor: Colors.white,
            // side: BorderSide(color: Colors.yellow, width: 5),
            textStyle: const TextStyle(
                color: Colors.white, fontSize: 25, fontStyle: FontStyle.normal),
            shape: BeveledRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(10))),
            shadowColor: Colors.lightBlue,
          ),
          onPressed: () => Navigator.of(context)
              .push(MaterialPageRoute(builder: (context) => const NewScreen())),
        ),
      ),
    );
  }
}

class NewScreen extends StatefulWidget {
  const NewScreen({Key? key}) : super(key: key);

  @override
  State<NewScreen> createState() => _NewScreenState();
}

class _NewScreenState extends State<NewScreen> {
  TextEditingController textEditingController = TextEditingController();

  @override
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.green,
        foregroundColor: Colors.white,
        title: const Text('New Screen'),
      ),
      body: Center(child: Text('This is your new screen')),
    );
  }
}


Output:



Next Article
Article Tags :

Similar Reads