Flutter Raised Button Example

6
45417
Flutter raised button example code
flutter RaisedButton sample code

Buttons are the basic UI component of any framework. In this tutorial, we will do a Flutter Raised Button Example. Flutter’s basic motto is to create stunning UI.  There are 7 different types of buttons in Flutter. Here you can learn about all those flutter’s buttons

flutter raisedbutton example code- A complete tutorial
Flutter raised button example.

Let’s get back to our Flutter Raised Button example. RaisedButton is part of Material UI. To use Flutter RaisedButton Widget this is all the widget code you need to create simple button.

RaisedButton(child: Text("Rock & Roll"),
                onPressed: _changeText,
                color: Colors.red,
                textColor: Colors.yellow,
                padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                splashColor: Colors.grey,
              )

RaisedButton Properties

Raised button has a single constructor but a long list of attributes. I am just mentioning the basic one we have used.

  • onPressed: onPressed function is called when we click on the button.
  • color: Color set the button color. Our button color is red.
  • textColor: This attribute is used to give the color to button’s text.
  • padding: Padding to the text inside the button.
  • splashColor: A color effect that we get when we click on the button.

You may have noticed onPressed we are calling a function _changeText. This function is basically changing the text when we click on the button. 

_changeText() {
    setState(() {
      if (msg.startsWith('F')) {
        msg = 'I have learned FlutterRaised example ';
      } else {
        msg = 'Flutter RaisedButton example';
      }
    });
  }

In _changeText function we are using a function called setState(). It let the framework knows that internal state of the object has been changed.

As we are pretty much clear about flutter Raised button. It is time to show you the complete code of this example.

Flutter Raised Button Example Code

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(title: "RaisedButton", home: MainActivity()));

class MainActivity extends StatefulWidget {
  @override
  _MainActivityState createState() => _MainActivityState();
}

class _MainActivityState extends State {
  String msg = 'Flutter RaisedButton example';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.yellow,
      appBar: AppBar(
        title: Text('Raised Button'),
      ),
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                msg,
                style: TextStyle(fontSize: 20, fontStyle: FontStyle.italic),
              ),
              RaisedButton(
                child: Text("Rock & Roll"),
                onPressed: _changeText,
                color: Colors.red,
                textColor: Colors.yellow,
                padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                splashColor: Colors.grey,
              )
            ],
          ),
        ),
      ),
    );
  }

  _changeText() {
    setState(() {
      if (msg.startsWith('F')) {
        msg = 'I have learned FlutterRaised example ';
      } else {
        msg = 'Flutter RaisedButton example';
      }
    });
  }
}

 If you don’t know how to setup Flutter on your system. Please read how to install flutter- Super Easy 

6 COMMENTS

  1. why method name is used as _changeText() ?

    In my case, if i just pass, onPressed : _changeText, i’m getting error. I had to pass it like as follows. Though i don’t know the reason why we should pass it like that. Can you help in understanding it.
    onPressed: () {
    changeText;
    },

    • yes, onpressed is a function in your case it call _changeText but as you said that I don’t copy all it’s mean that you don’t copy _chngeText function due to this it does not work. thanks

LEAVE A REPLY

Please enter your comment!
Please enter your name here