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'))
![[Solved] How To Use Flutter Material Button OnPressed 3 flutter material button onpressed](https://letmeflutter.com/wp-content/uploads/2022/10/IMG_20221024_142955-300x88.jpg)
![[Solved] How To Use Flutter Material Button OnPressed 4 flutter materialbutton onpressed](https://letmeflutter.com/wp-content/uploads/2022/10/IMG_20221024_143011-300x88.jpg)
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.
Flutter Material Button OnPressed Implementation Source Code
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.