How do I disable a Button in Flutter

0 votes

I'm just starting to get the hang of Flutter, but I'm having trouble figuring out how to set the enabled state of a button.

From the docs, it says to set onPressed to null to disable a button, and give it a value to enable it. This is fine if the button continues to be in the same state for the lifecycle.

I get the impression I need to create a custom Stateful widget that will allow me to update the button's enabled state (or onPressed callback) somehow.

So my question is how would I do that? This seems like a pretty straightforward requirement, but I can't find anything in the docs on how to do it.

Thanks.

Mar 9, 2023 in Android by sarit
• 1,830 points
19,648 views

1 answer to this question.

0 votes

To disable a button in Flutter, you can set the onPressed property to null, which will prevent the button from being pressed. To enable the button, you can set the onPressed property to a non-null value.

To dynamically enable or disable a button based on some condition, you can use a StatefulWidget. Here's an example:

import 'package:flutter/material.dart';

class MyButton extends StatefulWidget {
  @override
  _MyButtonState createState() => _MyButtonState();
}

class _MyButtonState extends State<MyButton> {
  bool _isEnabled = true;

  void _toggleEnabled() {
    setState(() {
      _isEnabled = !_isEnabled;
    });
  }

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: _isEnabled ? _toggleEnabled : null,
      child: Text('My Button'),
    );
  }
}

In this example, we have a MyButton widget that has a boolean _isEnabled property that determines whether the button is enabled or disabled. The _toggleEnabled method toggles the _isEnabled property, and calls setState to rebuild the widget with the new value.

In the build method, we use the _isEnabled property to conditionally set the onPressed property of the ElevatedButton. If _isEnabled is true, we set the onPressed property to _toggleEnabled, which will enable the button and toggle the _isEnabled property when the button is pressed. If _isEnabled is false, we set the onPressed property to null, which will disable the button.

You can use this MyButton widget in your app like any other widget:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: MyButton(),
        ),
      ),
    );
  }
}

This example is just one way to enable/disable a button dynamically. There are many other ways to achieve the same result depending on your specific use case.

To know more about Flutter, join our Flutter Course today.

answered Mar 10, 2023 by vinayak

Related Questions In Android

0 votes
0 answers

How do I turn a C# object into a JSON string in .NET?

I have classes like these: class MyDate { ...READ MORE

Sep 20, 2022 in Android by Edureka
• 13,750 points
1,009 views
0 votes
1 answer

How can I add a border to a widget in Flutter?

To add a border to a widget ...READ MORE

answered Mar 1, 2023 in Android by vishalini
14,906 views
0 votes
1 answer

How do I use hexadecimal color strings in Flutter?

To use a hexadecimal color string in ...READ MORE

answered Mar 1, 2023 in Android by vishalini
16,468 views
0 votes
1 answer

How do you set the value of a TextField in Flutter?

To supply an initial value to a ...READ MORE

answered Mar 9, 2023 in Android by pooja
18,022 views
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

save the code and it will be ...READ MORE

answered Jun 8, 2022 in Others by anonymous

edited Mar 5, 2025 4,786 views
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
2,035 views
0 votes
1 answer

flutter run: No connected devices

Hi@akhtar, To prepare to run and test your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
5,221 views