Raised Button widget in Flutter

RaisedButton is the material design button based on a Material widget that elevates when pressed upon in flutter. It is one of the most widely used buttons in the flutter library. Let’s understand this button with the help of an example.

Constructors:

Syntax:
RaisedButton({Key key, 
@required VoidCallback onPressed, 
VoidCallback onLongPress, 
ValueChanged<bool> onHighlightChanged, 
MouseCursor mouseCursor, 
ButtonTextTheme textTheme, 
Color textColor, 
Color disabledTextColor, 
Color color, 
Color disabledColor, 
Color focusColor, 
Color hoverColor, 
Color highlightColor, 
Color splashColor, 
Brightness colorBrightness, 
double elevation, 
double focusElevation, 
double hoverElevation, 
double highlightElevation, 
double disabledElevation, 
EdgeInsetsGeometry padding, 
VisualDensity visualDensity, 
ShapeBorder shape, 
Clip clipBehavior: Clip.none, 
FocusNode focusNode, 
bool autofocus: false, 
MaterialTapTargetSize materialTapTargetSize, 
Duration animationDuration, 
Widget child})

Properties:

  • child: the widget to be displayed.
  • shape: the shape of the raised button.
  • color: the color of the button.
  • padding: padding inside the button.
  • disabledColor: the color of the button when disabled.
  • splashColor: splash color of the button.
  • textColor: the color of the text.
  • onLongPress: the callback function when the button is long pressed.
  • onPressed: the callback function when the button is tapped.

Implementation:

The main.dart file:

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

import 'package:flutter/material.dart';
  
void main() {
  runApp(MyApp());
}
  
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Raised Button',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}
  
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
  
class _MyHomePageState extends State<MyHomePage> {
  String istapped = '';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GeeksforGeeks'),
        backgroundColor: Colors.green,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              //     disabledColor: Colors.red,
              // disabledTextColor: Colors.black,
              padding: const EdgeInsets.all(20),
              textColor: Colors.white,
              color: Colors.green,
              onPressed: () {
                setState(() {
                  istapped = 'Button tapped';
                });
              },
              child: Text('Enabled Button'),
            ),
            Text(
              istapped,
              textScaleFactor: 2,
            )
          ],
        ),
      ),
      backgroundColor: Colors.lightBlue[50],
    );
  }
}

chevron_right


Output:

If the properties are defined as below:

RaisedButton(
          disabledColor: Colors.red,
          disabledTextColor: Colors.black,
          onPressed: null,
          child: Text('Disabled Button'),
        ),

The following design changes can be observed:



disabled raisebutton

If the properties are defined as below:

RaisedButton(
          padding: const EdgeInsets.all(20),
          textColor: Colors.white,
          color: Colors.green,
          onPressed: ()=>null,
          child: Text('Enabled Button'),
        ),

The following design changes can be observed:

enabled raisebutton

If the properties are defined as below:

RaisedButton(
              padding: const EdgeInsets.all(20),
              textColor: Colors.white,
              color: Colors.green,
              onPressed: (){
                setState(() {
                  istapped='Button tapped';
                });
              },
              child: Text('Enabled Button'),
            ),
            Text(istapped,textScaleFactor: 2,)
          ),

The following design changes can be observed:

tapping on raisedbutton

Explanation:

  • Create RaisedButton and wrap it with Center widget.
  • Give the child of RaisedButton as a Text widget.
  • Perform onPressed function when the button is tapped.
  • Perform optional onLongPress function when button is long pressed.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.