Open In App

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.

Disclaimer: As of May 2021 the RaisedButton class in flutter is deprecated. ElevatedButton class should be used instead. The later class will eventually be removed from flutter SDK, so it is suggested to move to the newer class. Click here to see the migration guide.



List of replaced classes:

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:

Implementation:

The main.dart file:






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 MaterialApp(
      title: 'Raised Button',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}
 
class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);
 
  @override
// ignore: library_private_types_in_public_api
  _MyHomePageState createState() => _MyHomePageState();
}
 
class _MyHomePageState extends State<MyHomePage> {
  String istapped = '';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('GeeksforGeeks'),
        backgroundColor: Colors.green,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            // RaisedButton has been deprecated
            // We can use Elevated button achieve the same results
            RaisedButton(
              //     disabledColor: Colors.red,
              // disabledTextColor: Colors.black,
              padding: const EdgeInsets.all(20),
              textColor: Colors.white,
              color: Colors.green,
              onPressed: () {
                setState(() {
                  istapped = 'Button tapped';
                });
              },
              child: const Text('Enabled Button'),
            ),
            const SizedBox(
              height: 20,
            ),
            // ElevatedButton
            ElevatedButton(
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.green),
                    padding:
                        MaterialStateProperty.all(const EdgeInsets.all(20)),
                    textStyle: MaterialStateProperty.all(
                        const TextStyle(fontSize: 14, color: Colors.white))),
                onPressed: () {
                  setState(() {
                    istapped = 'Button tapped';
                  });
                },
                child: const Text('Enabled Button')),
            const SizedBox(height: 20),
            Text(
              istapped,
              textScaleFactor: 2,
            )
          ],
        ),
      ),
      backgroundColor: Colors.lightBlue[50],
    );
  }
}

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:

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:

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:

Output explanation:


Article Tags :