How to Disable a Button in Flutter Using Dart

Flutter May 30, 2023

Buttons are fundamental to any user interface, allowing users to interact with an application. In some cases, you may need to disable a button to prevent users from interacting with it temporarily. In this tutorial, we will explore how to disable a button in Flutter using the Dart programming language.

flutter_disabled_button

1.  Disabling a Button in Flutter

To disable a button in Flutter, we will utilize the onPressed property of the button widget. By setting the onPressed property to null, we can effectively disable the button. Let's take a look at the example code snippet:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyButton(),
    );
  }
}

class MyButton extends StatefulWidget {
  const MyButton({super.key});

  @override
  State createState() => _MyButtonState();
}

class _MyButtonState extends State {
  bool isButtonEnabled = false;

  void toggleButtonState() {
    setState(() {
      isButtonEnabled = !isButtonEnabled;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Disable Button Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: isButtonEnabled
              ? () => ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(
                      duration: Duration(seconds: 1),
                      backgroundColor: Colors.green,
                      content: Text(
                        'I am clicked!',
                        textAlign: TextAlign.center,
                      ),
                    ),
                  )
              : null,
          child: const Text('My Button'),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: toggleButtonState,
        child: isButtonEnabled
            ? const Icon(Icons.block)
            : const Icon(Icons.check_circle_outlined),
      ),
    );
  }
}

In this code, we define a MyButton widget that extends a StatefulWidget. Within the widget's state, we have a boolean variable isButtonEnabled determining whether the button is enabled or disabled. Initially, it is set to false.

The toggleButtonState function is called when the FloatingActionButton (button on the bottom right) is pressed. Inside this function, we use setState to update the state of isButtonEnabled to the opposite boolean value. This effectively toggles the button's state between enabled and disabled.

disabled_and_enable_button_in_flutter

The button itself is defined within the body of the Scaffold widget. We check the value of isButtonEnabled in the onPressed property of the ElevatedButton. If isButtonEnabled is true, we assign an anonymous function to onPressed, which shows a SnackBar message when the button is pressed. If isButtonEnabled is false, we set onPressed to null, disabling the button.

2. Conclusion

Disabling a button in Flutter using Dart is a straightforward process. By setting the onPressed property to null, you can effectively disable a button and prevent user interaction. This technique can be useful in various scenarios, such as when you need to disable a button temporarily during a specific application state or until certain conditions are met.

I hope this tutorial has been helpful in understanding how to disable a button in Flutter using Dart. Happy coding!

Tags