Mobile app developers lean heavily on the Flutter framework for a variety of developmental exploits. Rightfully so, it makes designing mobile applications a smoother affair. One of the key widgets that make the life of a developer easy is the Flutter Elevated button, a successor of the RaisedButton widget. This button doesn't just make your site or app accessible; it enhances the user interface's aesthetic appeal as well.
Before proceeding to the next section and understanding more about the style parameter and other details, let's first understand why we need elevated buttons in Flutter.
Elevated buttons add a certain amount of depth to your app's interface, thanks to the button's filled background. The hovering effect in Elevated Buttons asserts the zone of interactivity. This comes from the material widget philosophy that an elevation change signifies a state change. In other words, when pressed, the button's elevation increases, rendering a "raised" effect or a 3D illusion, thus the name 'Elevated' button. Let's look closely at the basics of how to create an elevated button in Flutter.
In Flutter, we use the ElevatedButton widget for crafting these buttons. This widget has a simple syntax spanned across three required widget properties. These include a labeled child displayed inside the button, an onPressed function allowing a response to taps, and of course, the style.
An example of a basic configuration of an ElevatedButton widget can be as follows:
1 ElevatedButton( 2 child: const Text('Click Me'), 3 onPressed: () {}, 4 style: style, 5 ); 6
This example creates a simple button with the label 'Click Me'. However, the onPressed callback and the style property are set to default values, which doesn't make much sense in a real-world application. You'll generally want to provide specific implementations for both to work properly. The onPressed function would have your logic for button press events, and the style parameter decides how your button looks. But how do we customize the look of a Flutter Elevated button?
Customizing the look of an ElevatedButton in Flutter is a breeze with the 'style' parameter. Your Flutter Elevated Button can assume a totally different look and feel by just tweaking the style.
What if you want to change the button's color, foreground color, or even its shape? You got it! All these customizations fall under the style parameter. In fact, this is where the ElevatedButton.styleFrom static method comes in handy. Generally, this is the most convenient way for simple stylings.
Here's how you change the text color, fill color or background color, and elevation of a button:
1 ElevatedButton( 2 onPressed: () {}, 3 style: ElevatedButton.styleFrom( 4 primary: Colors.yellow, // background color 5 onPrimary: Colors.black, // text color 6 elevation: 5, // button's elevation when it's pressed 7 ), 8 child: const Text('Styled Button'), 9 ); 10
To change the button shape, you use the shape property, like so:
1 ElevatedButton( 2 onPressed: () {}, 3 style: ElevatedButton.styleFrom( 4 shape: RoundedRectangleBorder( 5 borderRadius: BorderRadius.circular(20), // button's shape 6 ), 7 ), 8 child: const Text('Rounded Button'), 9 ); 10
An Elevated button in Flutter can be in different states - it could be pressed, hovered, focused, or even disabled. Each of these states can exhibit a different style. You can manage the style by accessing the 'style' property that returns a ButtonStyle object.
The style object lets you design specific attributes for each state, like backgroundColor for the normal state, or shadowColor when the button is pressed. In the same way, you can also define the foreground color for hover, focus, and pressed states using properties like foregroundColor, overlayColor, shadowColor, and a bunch more.
However, remember that state-related color properties of ButtonStyle take MaterialStateProperty<Color>
as their value. So, instead of directly passing in a color, you should provide an object of MaterialStateProperty<Color>
. You can create this object using the MaterialStateProperty.resolveWith method.
For example, this is how you can change the background color of an ElevatedButton when pressed:
1 ElevatedButton( 2 onPressed: () {}, 3 style: ButtonStyle( 4 backgroundColor: MaterialStateProperty.resolveWith<Color>( 5 (Set<MaterialState> states) { 6 if (states.contains(MaterialState.pressed)) 7 return Colors.green; 8 return null; // Use the component's default. 9 }, 10 ), 11 ), 12 child: const Text('Button with Pressed State'), 13 ); 14
The onPressed function is arguably the most critical function of an elevated button. It decides what action to undertake when the Elevated button is pressed. If the onPressed function is null, the button will be disabled and will not respond to touch. Typically, in a real-world application, the onPressed function may contain navigation logic, form submit logic, or even API call functions.
Here's an example:
1 ElevatedButton( 2 onPressed: () { 3 print('Button Pressed'); 4 }, 5 child: const Text('Button with OnPressed'), 6 ); 7
In the above example, when the user presses the button, it calls the print function with the message 'Button Pressed'. You can replace this with any piece of code you need to be triggered upon a button press.
Elevated buttons also commonly serve as triggers for dialogue boxes, alerts, and more complex workflows. They are truly a versatile bunch!
The onPressed function is just one small part of what an elevated button can do. But how about making our Elevated Button tailored precisely to the user's device size?
While building a Flutter application, considering the responsiveness of your design can be quite a challenge. However, Flutter provides a handy trick to make Elevated Buttons adaptable to different screen sizes — MediaQuery. Responsive design enriches user experiences as your app appears seamlessly on a plethora of screens.
To implement responsive design with the Elevated Button in Flutter, wrap your ElevatedButton widget inside a LayoutBuilder widget. This widget provides us with a BoxConstraints object which we can then use with MediaQuery to set width and height that adapts to different screens.
Here's an example:
1 LayoutBuilder( 2 builder: (BuildContext context, BoxConstraints constraints) { 3 return ElevatedButton( 4 onPressed: () {}, 5 child: const Text('Responsive Button'), 6 style: ElevatedButton.styleFrom( 7 shape: RoundedRectangleBorder( 8 borderRadius: BorderRadius.circular(25), 9 ), 10 minimumSize: Size(constraints.maxWidth, 50), // Make it responsive 11 padding: EdgeInsets.symmetric(horizontal: 16), 12 textStyle: TextStyle(fontSize: 14), 13 ), 14 ); 15 }, 16 ); 17
In this example, our ElevatedButton's width will always match the maximum width provided by the parent widget, thus offering a responsive design.
The journey into the depths of the Flutter button doesn't end here. In the following sections, we will look at some advanced customizations and common issues developers may face with the Flutter Elevated Button.
While the styleFrom static method of ElevatedButton can do a lot in terms of styling, sometimes you just need that extra power in your hands to give a touch of uniqueness to your application. For more nuanced control over the Elevated Button's style, you can use the ButtonStyle class.
With ButtonStyle, you can change more than just background color and shape. It allows you to change shadow color, elevation when pressed, padding, visual density, and even add animations when the button is pressed.
I have kept the code for this section at the end because only proceed to generate code if you're utterly sure about it. The following example is a simple demonstration of how an elevated button can 'flutter' on being clicked:
1 ElevatedButton( 2 onPressed: () {}, 3 child: const Text('Fluttering Button'), 4 style: ButtonStyle( 5 padding: MaterialStateProperty.all<EdgeInsets>(EdgeInsets.all(15)), 6 backgroundColor: MaterialStateProperty.all<Color>(Colors.green), 7 shadowColor: MaterialStateProperty.all<Color>(Colors.grey), 8 elevation: MaterialStateProperty.resolveWith<double>( 9 (Set<MaterialState> states) { 10 if (states.contains(MaterialState.pressed)) return 10; 11 return 5; // default elevation 12 }, 13 ), 14 shape: MaterialStateProperty.all<RoundedRectangleBorder>( 15 RoundedRectangleBorder( 16 borderRadius: BorderRadius.circular(25), 17 ), 18 ), 19 animationDuration: Duration(milliseconds: 200) 20 ), 21 ); 22
In the next section, let's explore some potential solutions to common challenges you might encounter when working with the Flutter Elevated Button.
As you begin working with the Elevated Button, you might encounter a few bumps. But don't worry! Here, we present some common challenges with their solutions:
An ElevatedButton widget requires a non-null onPressed callback. If that is not provided, the button will be considered disabled and won't show. Ensure you've placed a function inside the onPressed parameter.
The color of an ElevatedButton could be defined in two places: the ElevatedButton.styleFrom or via ThemeData in the parent Material widget. It's important to ensure that the color values in both places correspond to what you want to achieve. Also, keep in mind that you can't use the color property directly in an ElevatedButton. For example, the following would NOT work,
1 ElevatedButton( 2 onPressed: () {}, 3 child: const Text('This will not work will not work'), 4 color: Colors.green, // This will throw an error. 5 ); 6
You should instead use the primary property of styleFrom or backgroundColor of ButtonStyle.
Ensure that your ElevatedButton is wrapped inside a LayoutBuilder widget and you calculate the width based upon the constraints provided by the latter.
The world of Flutter development is incomplete without the Flutter Elevated Button. Elevated buttons are a fantastic tool in a Flutter mobile developer's toolkit. From their prowess in controlling user interactivity to the ability to boost your application's aesthetics, Elevated buttons play an integral role. However, merely dropping an Elevated Button into your code is not going to cut it.
From changing the Elevated Button style to working with different states and actions of the Flutter Elevated Button, there is a lot more subtlety and depth to these buttons than meets the eye. Properly understanding and mastering these aspects of Elevated Buttons can heighten your Flutter code, benefiting both you and your Flutter app users.
Keep creating, keep fluttering, and remember that practice makes perfect. If you want to learn more about Elevated Button styling or Flutter in general, refer to the official documentation. Happy coding!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.