How to Disable a Button in Flutter?
Last Updated :
28 Apr, 2025
Sometimes we need to make the button disabled, let us consider, you created a form to take the user input and a form submit button. Then you want that, the button must be disabled until the user fills all the fields of the form then immediately enable the button.
Sample Output is given below what we are going to implement in this article.
Approach:
We know that every button in flutter has a property on pressed. If we give assign it to null then the button becomes disabled.
OutlinedButton(
onPressed: null,
child: Text('Disabled Button'),
),
Let's create a simple application that contains two buttons, one is the enabled button, and the second one disabled button.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Adding Material Package
Add the material package that gives us the important methods and then call the runApp method in the main function that will call our application.
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
Step 3: Creating a Stateless Widget
Now we have to make a stateless widget RunMyApp Because our application does not go to change its state and then return the MaterialApp widget which allows us the set the title and theme and many more.
class RunMyApp extends StatelessWidget {
const RunMyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(home:);
}
}
Step 4: Configuring Properties
Give the home property and there can be a scaffold widget that has the property of AppBar and body. AppBar allows us to give the title of App Bar, color, leading, and trailing icon.
home: Scaffold(
appBar: AppBar(title: Text('Disable Button'),),
body: Center(
),
),
Step 5: Creating an Outlined Button
Create the Outlined Button and give text as a child, to make the disabled button you have to provide null to on pressed property of the button.
OutlinedButton(
onPressed: null,
child: Text('Disabled Button'),
),
Complete code:
Dart
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatelessWidget {
const RunMyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: Scaffold(
appBar: AppBar(
title: Text('Disable Button'),
),
body: Center(
child: Column(
children: [
SizedBox(
height: 250,
),
OutlinedButton(
onPressed: () {},
child: Text('Enabled Button'),
),
SizedBox(
height: 50,
),
OutlinedButton(
onPressed: null,
child: Text('Disabled Button'),
),
],
),
),
),
);
}
}
Output:
Disabled Button
Similar Reads
How to Enable/Disable Button in Android? The Enable/Disable Feature is used in many Android apps so the basic use of that feature is to prevent the user from clicking on a button until they will add all the required fields that have been asked. We are considering an example of email and password if the user has entered the email and passwo
3 min read
How to Disable Back Press Button in Android? Physical keys on Android let users lock the phone, switch off the device, control volume, go back, go home and display background applications. Between all the keys, the back button is used the most in applications for navigating between activities. However, if the navigation stage reaches the start
2 min read
How to Change Floating Action Button Color in Flutter? Every Android application has the floatingActionButton in it. There is a possibility that there are multiple screens in the application and have floatingActionButton on that screens, then we need to give the colors property to every floatingActionButton. But Flutter resolves this by changing the the
2 min read
Alert Dialog box in Flutter Alert Dialog box informs the user about the situation that requires acknowledgment. Alert Box is a prompt that takes user confirmation. The very basic use of the alert box is used when we close the app, usually, we are notified with a prompt whether we want to exit or not. That's an alert box.The be
2 min read
FlatButton Widget in Flutter FlatButton is the material design widget in a flutter. It is a text label material widget that performs an action when the button is tapped. Let's understand with the help of examples. Disclaimer: As of May 2021 the FlatButton class in flutter is deprecated. TextButton class should be used instead.
3 min read
Flutter - Extended Icon Button In Flutter there are many prebuilt Button - Material Button, Text Button, Elevated Button, etc. In this article, we are going to implement the Extended Icon Button, which can be customized as per user need. We can give color to the button. A sample video is given below to get an idea about what we a
2 min read
Flutter - Circular Icon Button In this article, we will implement how to make a circular icon button in Flutter. A sample image is given below to get an idea about what we are going to do in this article. Â Step by Step Implementation Step 1: Create a New Project in Android Studio To set up Flutter Development on Android Studio pl
2 min read
Flutter - Toggle Buttons A toggle button allows the user to change a setting between two states. You can add a basic toggle button to your layout with the ToggleButton object. A sample video is given below to get an idea about what we are going to do in this article.Demo Video:SyntaxList Of Booleans to Know Which Selected:3
4 min read
How to Create Buttons with Rounded Corners in Flutter? Flutter is a UI toolkit developed by Google. It is being utilized by big tech companies like Alibaba, Airbnb, and Google itself to build apps that serve billions of users around the globe. In this article, we will be seeing how to make Rounded Buttons in Flutter. Approach: We will create the buttons
2 min read
Flutter - Gradient Button Buttons are the building block of any Android Application, Buttons are helping in to perform specific tasks like Navigating from one screen to another screen, Showing the Toast, Sign-in, Sign-up buttons, and many more. But giving the effects of the colors to the Button makes the Button more pretty.
3 min read