[Solved] How To Use Flutter Material Button OnPressed

flutter material button onpressed

In this tutorial, we’ll learn how to properly use Flutter material button onPressed with the help of an easy Flutter example.

After reading this post, you’ll have a detailed practical knowledge of how to use Flutter material button onPressed with ease.

Introduction: Flutter Material Button OnPressed

As the name suggests, it’s used to trigger some actions after the material button is clicked by the user. Let’s go through a proper example to demonstrate its usage.

Implementing Flutter Material Button OnPressed (Easy Example Code)

We first have to define a simple Flutter material button widget. Then we will use its required onPressed constructor which takes a function. We can specify any action we want in the body of that function.

To demonstrate how it works, we’ll be using a Flutter snackbar widget inside that function’s body. So whenever the user clicks on the material button, the snackbar widget will appear on the screen. See below code in which this theory is practically implemented.

MaterialButton(
           onPressed: () {
                 ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                       content: Text('Material button is pressed')));
                    },
                    color: Colors.blue,
                    textColor: Colors.white,
                    child: Text('Material button'))
flutter material button onpressed
As you can see that we’ve specified a snackbar inside the onPressed function. We also have passed the Flutter text widget to the child constructor of Flutter material Button class to make the button looks a bit professional.
Let’s now press the button and see if we get the snackbar on our screen.
flutter materialbutton onpressed

You can see here that after the button was pressed, the onPressed function was triggered and as a result, the snackbar widget was shown on the screen.

You can specify any action in the body of onPressed function, like navigation to some other screen, validation or addition etc.

So this is how we can use the Flutter material button onPressed with ease. Hope you like this post.

Don’t hesitate to ask if you still have any questions related to the implementation of Flutter material button onPressed function. We’d be very glad to answer all your questions.

Below section includes the complete source code of the above implemented Flutter material button onPressed function.

Flutter Material Button OnPressed Implementation Source Code

flutter material button onpressed

flutter materialbutton onpressed

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(
                // flutter material button onpressed implementation
                child: MaterialButton(
                    onPressed: () {
                      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                          content: Text('Material button is pressed')));
                    },
                    color: Colors.blue,
                    textColor: Colors.white,
                    child: Text('Material button')))));
  }
}

Conclusion

To conclude this tutorial, hope now you’ve a detailed practical knowledge of how to properly use Flutter material button onPressed. I’d love to have your feedback on this post.

We’d also love to see you visit my other tutorials on Flutter app development and Python programming as well. Thank you for reading this post.

Leave a Comment

Your email address will not be published.