Design Converter
Education
Software Development Executive - II
Last updated on Apr 23, 2024
Last updated on Apr 23, 2024
Flutter is a portable UI toolkit designed to create natively compiled applications for mobile, web, and desktop from a single codebase. A vital part of this toolkit is the Flutter FlatButton. This flat button was a fundamental component in creating user-friendly apps with easy navigation.
However, FlatButton was first deprecated and replaced in Flutter v1.20. In this blog, we will gain an understanding of the Flutter FlatButton replacement and learn how to create more intuitive and visually appealing apps with it.
A FlatButton is just a collection of the numerous basic material button widgets. This widget was usually used to create buttons with only text (child text). It didn't have an elevation, unlike the RaisedButton. And on press, it didn't introduce any shadows, its original button’s appearance remained the same. However, it did receive an ink splash when tapped indicating the press event. But as mentioned before, Flutter FlatButton has been deprecated.
During a Flutter update, it was confirmed that FlatButton has been deprecated. Instead of FlatButton, it was suggested to use TextButton for the replacement of Flutter FlatButton.
The new Flutter FlatButton replacement - the Text button is more flexible, giving the developer more control over the button’s visual properties. For example, the new API doesn’t defer to a large set of widget parameters and properties for a button’s visual attributes.
Instead, it provides us with a single ButtonStyle object to configure the appearance of buttons, emphasizing the button's visual properties. Most of the ButtonStyle properties are defined with MaterialStateProperty to represent different values depending on the button's state, defining overrides of the button's default visual properties. This approach reduces a considerable volume of boilerplate code associated with the button's appearance.
1TextButton( 2 style: ButtonStyle( 3 backgroundColor: MaterialStateProperty.all<Color>(Colors.red), 4 ), 5 onPressed: () {}, 6 child: const Text('Flat Button'), 7);
As you can see, all the customizable properties are defined within the style parameter, specifying the appearance of buttons now by a ButtonStyle object instead of a large set of widget parameters.
The widget build buildcontext context is a common pattern in Flutter. The BuildContext context argument is passed to a widget's build method each time Flutter needs to render that widget. It provides a handle to the widget's location in the widget tree and establishes a direct line of communication to the parent widget. This context plays a vital role in our replacement of Flutter FlatButton.
Asynchrony is important for any button widget. It keeps the user interface responsive. While you're waiting for a lengthy computation to complete or a large chunk of data to load, the text button remains interactive.
Here's a basic example of using the buildcontext context:
1TextButton( 2 onPressed: () { 3 Scaffold.of(context).showSnackBar( 4 SnackBar( 5 content: const Text('Button pressed'), 6 duration: const Duration(seconds: 3), 7 ), 8 ); 9 }, 10 child: const Text('Show SnackBar'), 11 style: ButtonStyle( 12 backgroundColor: MaterialStateProperty.resolveWith<Color>( 13 (Set<MaterialState> states) { 14 if (states.contains(MaterialState.pressed)) 15 return Colors.lightBlue; 16 return null; // Use the component's default. 17 }, 18 ), 19 ), 20);
The onPressed function uses the context provided by the widget build BuildContext context.
When creating your Flutter FlatButton replacement, it’s essential to define button styles. Buttons in Flutter are highly customizable offering complete control over their visual aesthetics through individual style properties.
You can specify the appearance of buttons in your app by styling the button widgets, each offering a variety of properties for customization. The ButtonStyle properties can be used to specify the button’s appearance, allowing for detailed customization including setting a custom foreground color using the styleFrom method. For example, you can specify the ‘foregroundColor’ to set the button’s color or the ‘backgroundColor’ to set the background color.
Here’s an example of a button widget styled with custom colors:
1TextButton( 2 style: TextButton.styleFrom( 3 primary: Colors.white, 4 backgroundColor: Colors.teal, 5 onSurface: Colors.grey, 6 ), 7 onPressed: () {}, 8 child: Text('Define Button Styles'), 9)
In this snippet of code, the style attribute makes use of the TextButton.styleFrom method that allows us to easily define standard visual properties of the button, including the ability to set a custom foreground color for a more personalized appearance.
Custom overlay colors are an integral aspect of user interaction with buttons. They provide visual feedback to user actions and can enhance the overall aesthetics of your UI design.
With the introduction of Flutter FlatButton replacement - TextButton, applying custom overlay colors has transformed.
In the given code snippet, we use the Overlay_Color property to create a custom overlay color that responds to user actions:
1TextButton( 2 onPressed: () { }, 3 style: ButtonStyle( 4 overlayColor: MaterialStateProperty.resolveWith<Color>( 5 (Set<MaterialState> states) { 6 if (states.contains(MaterialState.hovered)) 7 return Colors.blue.withOpacity(0.04); 8 if (states.contains(MaterialState.focused) ..add(MaterialState.pressed)) 9 return Colors.blue.withOpacity(0.12); 10 return null; // Defer to the widget's default. 11 } 12 ), 13 ), 14 child: const Text('Button with Overlay Color'), 15)
In this snippet, the overlay color changes depending on the state of the button - whether it is being hovered over or pressed.
Disabled buttons are crucial for avoiding interactions under certain conditions in an application. Therefore, having noticeable custom disabled colors help users to recognize a button's current state easily.
In the revamped Flutter FlatButton replacement, you can define disabled colors as follows:
1TextButton( 2 onPressed: null, 3 style: TextButton.styleFrom( 4 primary: Colors.black, 5 onSurface: Colors.grey, 6 ), 7 child: const Text('Disabled Button'), 8)
In this code snippet, when the onPressed callback is null, the button gets disabled, showing the 'onSurface'-color.
Every aspect of the button's visual properties - from basic colors to custom shapes can be shaped as per the developer’s choice, providing the ability to craft aesthetically pleasing and interactive mobile applications.
Building your own Flutter FlatButton replacement is no Herculean task. Flutter offers the mere power to modify all distinct parts of a singular button, something absent in the original FlatButton.
To start with, let’s create a basic TextButton:
1TextButton( 2 onPressed: () {}, 3 child: const Text('Text Button'), 4)
The above code creates a text button with a callback that gets called when the button is pressed. This is the most basic form of a TextButton, often utilizing a text widget as its child to display the button's label.
However, Flutter allows you to customize this button extensively, including the pressed outline color to ensure it matches the original buttons appearance for a cohesive look across your application.
Here’s an example of a styled TextButton:
1TextButton( 2 style: TextButton.styleFrom( 3 backgroundColor: Colors.blueGrey, 4 primary: Colors.white, 5 textStyle: const TextStyle(fontSize: 20), 6 side: MaterialStateProperty.resolveWith<BorderSide>( 7 (Set<MaterialState> states) { 8 if (states.contains(MaterialState.pressed)) 9 return BorderSide( 10 color: Colors.blue, // Custom pressed outline color 11 width: 2, 12 ); 13 return BorderSide(); // Default BorderSide 14 }, 15 ), 16 ), 17 onPressed: () {}, 18 child: const Text('Styled Text Button'), 19)
In this example, we utilize the style TextButton.styleFrom function to set the background colors, primary color (which affects the text color), and a textstyle object for the child text.
Hence, with the new Flutter FlatButton replacement, you have the power to create customized buttons by modifying visual properties to fit your application’s theme.
When dealing with the new Flutter FlatButton Replacement, there are common pitfalls to avoid:
Ignoring the Material Design guidelines: Flutter is heavily inspired by Material Design. Ignoring the prescribed guidelines can lead to poor user experiences.
Over-customization: While customization is a strong point of Flutter, overdoing it can lead to inconsistent UI and ultimately, poor user experience.
Not utilizing the Style API: The new ButtonStyle properties offer a vast range of attributes to customize button widget according to your preference. It’s important to leverage it.
Ignoring disabled state colors: Not reflecting the state of a button clearly can confuse the user. Hence, make sure to define disabled colors appropriately.
With the Flutter FlatButton being deprecated and replaced by a new set of button widgets, developers now have complete control over the button's visual properties. From custom shapes to background colors, overlay color to disabled colors, every aspect of a button can now be styled. Plus, these changes align better with the continuous evolution of other package:flutter material.dart components.
By adapting to the new styling APIs, you'll create mobile applications that offer advanced and visually pleasing user interactions. So don't be afraid: embrace the new ‘Flutter FlatButton replacement’ and give your visuals a truly remarkable upgrade.
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.