A checkbox is a material design widget that allows users to select one or more options from a list. It is a simple but effective way for users to interact with your app and provide feedback by tapping a small square box.
How to create an OutlinedButton in Flutter
Key takeaways:
Flutter is a versatile toolkit for building cross-platform applications using a single codebase, making it an attractive option for developers focused on creating responsive user interfaces.
An outlined button in Flutter features a border with a transparent background, primarily used for secondary actions, offering a visually distinct yet subtle means for user interaction.
The
OutlinedButtonwidget, part of the Flutter material library, simplifies the creation of outlined buttons.The implementation steps of
OutlinedButtonin Flutter are as follows:
Begin by setting up a new Flutter project.
Incorporate the
OutlinedButtonwithin the main application, configuring properties such as border color, width, and shape, along with anonPressedcallback to handle button presses.Customize the button’s appearance by setting attributes like border width and color, and using a Text widget for labeling.
Flutter is a powerful toolkit created by Google to build fast mobile, web, and desktop applications using just one Codebase. It offers an easy-to-use and flexible development environment, making it a popular choice for developers to create beautiful and responsive user interfaces across multiple platforms.
What is an outlined button?
An outlined button in Flutter displays a border around its parameter, typically with a transparent background and text inside the border where the border gives the appearance of the button. These buttons are commonly used for secondary actions in the application interface where their appearance is less prominent than solid filled buttons but still provides affordance for interaction.
The OutlinedButton widget
Outlined buttons in Flutter are typically created using the OutlinedButton widget, which is a part of the Flutter material library. The OutlinedButton is essentially a TextButton with an outlined border.
Creating a OutlinedButton
Follow the steps mentioned below to create a segmented button in the Flutter application:
1- Create a Flutter project
Let’s start by creating a new Flutter project. For this, open the terminal and execute the following commands:
flutter create outlinedbuttoncd outlinedbutton
2- Create the user interface
Let’s create a simple OutlinedButton inside the MyApp class in the main.dart file. We will give it a border of width: 2.0 and blue color. We'll also give it a shape of RoundedRectangularBorder. We also have a onPressed parameter, which will contain the code executed whenever the button is pressed. Here, we are just printing “Outlined Button pressed” whenever the button is pressed. Here is the code:
OutlinedButton(onPressed: () {// Add your onPressed action hereprint('Outlined Button pressed');},style: OutlinedButton.styleFrom(side: const BorderSide(width: 2.0, color: Colors.blue),shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),),child: const Text('Outlined Button',style: TextStyle(fontSize: 16.0,color: Colors.blue,),),)
3- Complete implementation
Following is the complete code of the Flutter application with the implementation of a OutlinedButton:
IyBUaGlzIGZpbGUgY29uZmlndXJlcyB0aGUgYW5hbHl6ZXIsIHdoaWNoIHN0YXRpY2FsbHkgYW5hbHl6ZXMgRGFydCBjb2RlIHRvCiMgY2hlY2sgZm9yIGVycm9ycywgd2FybmluZ3MsIGFuZCBsaW50cy4KIwojIFRoZSBpc3N1ZXMgaWRlbnRpZmllZCBieSB0aGUgYW5hbHl6ZXIgYXJlIHN1cmZhY2VkIGluIHRoZSBVSSBvZiBEYXJ0LWVuYWJsZWQKIyBJREVzIChodHRwczovL2RhcnQuZGV2L3Rvb2xzI2lkZXMtYW5kLWVkaXRvcnMpLiBUaGUgYW5hbHl6ZXIgY2FuIGFsc28gYmUKIyBpbnZva2VkIGZyb20gdGhlIGNvbW1hbmQgbGluZSBieSBydW5uaW5nIGBmbHV0dGVyIGFuYWx5emVgLgoKIyBUaGUgZm9sbG93aW5nIGxpbmUgYWN0aXZhdGVzIGEgc2V0IG9mIHJlY29tbWVuZGVkIGxpbnRzIGZvciBGbHV0dGVyIGFwcHMsCiMgcGFja2FnZXMsIGFuZCBwbHVnaW5zIGRlc2lnbmVkIHRvIGVuY291cmFnZSBnb29kIGNvZGluZyBwcmFjdGljZXMuCmluY2x1ZGU6IHBhY2thZ2U6Zmx1dHRlcl9saW50cy9mbHV0dGVyLnlhbWwKCmxpbnRlcjoKICAjIFRoZSBsaW50IHJ1bGVzIGFwcGxpZWQgdG8gdGhpcyBwcm9qZWN0IGNhbiBiZSBjdXN0b21pemVkIGluIHRoZQogICMgc2VjdGlvbiBiZWxvdyB0byBkaXNhYmxlIHJ1bGVzIGZyb20gdGhlIGBwYWNrYWdlOmZsdXR0ZXJfbGludHMvZmx1dHRlci55YW1sYAogICMgaW5jbHVkZWQgYWJvdmUgb3IgdG8gZW5hYmxlIGFkZGl0aW9uYWwgcnVsZXMuIEEgbGlzdCBvZiBhbGwgYXZhaWxhYmxlIGxpbnRzCiAgIyBhbmQgdGhlaXIgZG9jdW1lbnRhdGlvbiBpcyBwdWJsaXNoZWQgYXQKICAjIGh0dHBzOi8vZGFydC1sYW5nLmdpdGh1Yi5pby9saW50ZXIvbGludHMvaW5kZXguaHRtbC4KICAjCiAgIyBJbnN0ZWFkIG9mIGRpc2FibGluZyBhIGxpbnQgcnVsZSBmb3IgdGhlIGVudGlyZSBwcm9qZWN0IGluIHRoZQogICMgc2VjdGlvbiBiZWxvdywgaXQgY2FuIGFsc28gYmUgc3VwcHJlc3NlZCBmb3IgYSBzaW5nbGUgbGluZSBvZiBjb2RlCiAgIyBvciBhIHNwZWNpZmljIGRhcnQgZmlsZSBieSB1c2luZyB0aGUgYC8vIGlnbm9yZTogbmFtZV9vZl9saW50YCBhbmQKICAjIGAvLyBpZ25vcmVfZm9yX2ZpbGU6IG5hbWVfb2ZfbGludGAgc3ludGF4IG9uIHRoZSBsaW5lIG9yIGluIHRoZSBmaWxlCiAgIyBwcm9kdWNpbmcgdGhlIGxpbnQuCiAgcnVsZXM6CiAgICAjIGF2b2lkX3ByaW50OiBmYWxzZSAgIyBVbmNvbW1lbnQgdG8gZGlzYWJsZSB0aGUgYGF2b2lkX3ByaW50YCBydWxlCiAgICAjIHByZWZlcl9zaW5nbGVfcXVvdGVzOiB0cnVlICAjIFVuY29tbWVudCB0byBlbmFibGUgdGhlIGBwcmVmZXJfc2luZ2xlX3F1b3Rlc2AgcnVsZQoKIyBBZGRpdGlvbmFsIGluZm9ybWF0aW9uIGFib3V0IHRoaXMgZmlsZSBjYW4gYmUgZm91bmQgYXQKIyBodHRwczovL2RhcnQuZGV2L2d1aWRlcy9sYW5ndWFnZS9hbmFseXNpcy1vcHRpb25zCg==
Note: You can find your application running at the given link once you run the code widget above.
Code explanation
Lines 16–20: We have created an
OutlinedButtonwhich, when pressed, calls theonPressedcallback function. This will print “Outlined Button pressed” to the console.Lines 21–25: We are styling our
OutlinedButtonwhich configures the appearance of the button. Here, it sets a blue border with a width of 2.0 and a rounded rectangular shape.Lines 26–32: Here, we have created a
Textwidget inchildwith the text “Outlined Button” in a blue color.
Conclusion
Creating an OutlinedButton in Flutter is a simple yet effective way to enhance user interaction in mobile and web applications. By leveraging the built-in OutlinedButton widget, developers can easily incorporate secondary action buttons that are visually appealing and functional.
We offer various courses if you aim to continue your journey in learning Flutter!
Frequently asked questions
Haven’t found what you were looking for? Contact Us
What is Flutter Checkbox widget?
What is SegmentedButton in Flutter?
Can we create an extended floating action button in Flutter?
Free Resources