How To Use Flutter RaisedButton OnPressed

flutter raisedbutton onpressed

In this post, I will be explaining how to use Flutter RaisedButton OnPressed using proper Flutter example. I will be using step by step explanation on how to use Flutter RaisedButton OnPressed so you can have a strong grip on how to use Flutter RaisedButton OnPressed. Let’s not keep you waiting anymore and start implementing Flutter RaisedButton OnPressed.

What is Flutter RaisedButton OnPressed?

Flutter RaisedButton OnPressed is used to perform some action when the user click the Flutter RaisedButton. Let’s now understand how to implement Flutter RaisedButton OnPressed using a proper Flutter example code.

Implementing Flutter RaisedButton OnPressed

To implement Flutter RaisedButton OnPressed, we have to use the on pressed constructor which takes a function. See the below code:

RaisedButton(
          onPressed: () {},
                    child: Text(
                      'Flutter Raised Button',
                    ),
                  )
In the above code, you can see that we have passed an empty function to the Flutter RaisedButton onPressed constructor. By clicking on it, we won’t see any change as we have passed an empty function to it.
Let’s now define some action in that function. We will pass a Flutter snack bar to the function’s body so whenever the Flutter RaisedButton is pressed then the function will be triggered which will show Flutter snack bar. See the below code:
 RaisedButton(
            onPressed: () {
                      ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(content: Text('Raised button pressed')));
                    },
                    child: Text(
                      'Flutter Raised Button',
                    ),
                  )

flutter raisedbutton onchanged

As you can see in the above image, we have pressed the Flutter RaisedButton and it shows the Flutter snack bar. You can define any other action in it like navigation to some other pages or validation etc. You just have to define that action in the body of the function as done in the above code.

Now you have a complete practical understanding of how to use Flutter RaisedButton OnPressed. Comment down if you still have any doubts regarding the usage of Flutter RaisedButton OnPressed. I would love to answer.

I also have prepared a beautiful custom Flutter RaisedButton design in which Flutter RaisedButton OnPressed is also implemented. The complete source code is given in the below section.

Beautiful Flutter RaisedButton Design Source Code

flutter raisedbutton onchanged

import 'package:flutter/material.dart';
void main() {
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Homepage(),
    );
  }
}
class Homepage extends StatefulWidget {
  @override
  State<Homepage> createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      body: Center(
          child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 40),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  RaisedButton(
                    onPressed: () {
                      ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(content: Text('Raised button pressed')));
                    },
                    color: Colors.brown,
                    padding: EdgeInsets.symmetric(vertical: 20, horizontal: 20),
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20).copyWith(
                            topLeft: Radius.circular(0),
                            bottomRight: Radius.circular(0))),
                    child: Text(
                      'Flutter Raised Button OnPressed Implemented',
                      style: TextStyle(
                          color: Colors.white, fontWeight: FontWeight.w500),
                    ),
                  )
                ],
              ))),
    ));
  }
}

Conclusion

Finally, you now have a good understanding of how to use the Flutter RaisedButton OnPressed. I welcome your valuable feedback on this post. Please feel free to also explore the other articles in which I have covered Flutter widgets, Flutter app development, Flutter animations, Flutter templates with source code, and many more. Thanks for reading.

2 thoughts on “How To Use Flutter RaisedButton OnPressed”

  1. I don’t know whеther it’s just mе or if everybody else еxperiencing problems wіth your site.
    It appears as thⲟugh some of the written text within your content ɑre runnіng off the screen. Can somebody else please ϲomment and let me know if this iѕ happening to them as well?
    This may be a issue ѡith my browsеr because I’ve һad this happen previouѕly.
    Thаnk you

Leave a Comment

Your email address will not be published.