How do I disable a Button in Flutter?

后端 未结 8 2141
不思量自难忘°
不思量自难忘° 2020-12-04 17:19

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 onP

相关标签:
8条回答
  • 2020-12-04 17:36

    For a specific and limited number of widgets, wrapping them in a widget IgnorePointer does exactly this: when its ignoring property is set to true, the sub-widget (actually, the entire subtree) is not clickable.

    IgnorePointer(
        ignoring: true, // or false
        child: RaisedButton(
            onPressed: _logInWithFacebook,
            child: Text("Facebook sign-in"),
            ),
    ),
    

    Otherwise, if you intend to disable an entire subtree, look into AbsorbPointer().

    0 讨论(0)
  • 2020-12-04 17:38

    This is the easiest way in my opinion:

    RaisedButton(
      child: Text("PRESS BUTTON"),
      onPressed: booleanCondition
        ? () => myTapCallback()
        : null
    )
    
    0 讨论(0)
  • 2020-12-04 17:39

    The simple answer is onPressed : null gives a disabled button.

    0 讨论(0)
  • 2020-12-04 17:42

    Setting

    onPressed: null // disables click
    

    and

    onPressed: () => yourFunction() // enables click
    
    0 讨论(0)
  • 2020-12-04 17:49

    Enable and Disable functionality is same for most of the widgets.

    Ex, button , switch, checkbox etc.

    Just set the onPressed property as shown below

    onPressed : null returns Disabled widget

    onPressed : (){} or onPressed : _functionName returns Enabled widget

    0 讨论(0)
  • 2020-12-04 17:50

    I think you may want to introduce some helper functions to build your button as well as a Stateful widget along with some property to key off of.

    • Use a StatefulWidget/State and create a variable to hold your condition (e.g. isButtonDisabled)
    • Set this to true initially (if that's what you desire)
    • When rendering the button, don't directly set the onPressed value to either null or some function onPressed: () {}
    • Instead, conditionally set it using a ternary or a helper function (example below)
    • Check the isButtonDisabled as part of this conditional and return either null or some function.
    • When the button is pressed (or whenever you want to disable the button) use setState(() => isButtonDisabled = true) to flip the conditional variable.
    • Flutter will call the build() method again with the new state and the button will be rendered with a null press handler and be disabled.

    Here's is some more context using the Flutter counter project.

    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => new _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      bool _isButtonDisabled;
    
      @override
      void initState() {
        _isButtonDisabled = false;
      }
    
      void _incrementCounter() {
        setState(() {
          _isButtonDisabled = true;
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text("The App"),
          ),
          body: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Text(
                  'You have pushed the button this many times:',
                ),
                new Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.display1,
                ),
                _buildCounterButton(),
              ],
            ),
          ),
        );
      }
    
      Widget _buildCounterButton() {
        return new RaisedButton(
          child: new Text(
            _isButtonDisabled ? "Hold on..." : "Increment"
          ),
          onPressed: _isButtonDisabled ? null : _incrementCounter,
        );
      }
    }
    

    In this example I am using an inline ternary to conditionally set the Text and onPressed, but it may be more appropriate for you to extract this into a function (you can use this same method to change the text of the button as well):

    Widget _buildCounterButton() {
        return new RaisedButton(
          child: new Text(
            _isButtonDisabled ? "Hold on..." : "Increment"
          ),
          onPressed: _counterButtonPress(),
        );
      }
    
      Function _counterButtonPress() {
        if (_isButtonDisabled) {
          return null;
        } else {
          return () {
            // do anything else you may want to here
            _incrementCounter();
          };
        }
      }
    
    0 讨论(0)
提交回复
热议问题