【问题标题】:onPressed not disabling the buttononPressed 不禁用按钮
【发布时间】:2020-05-08 00:26:58
【问题描述】:

我尝试在this article 中实施解决方案,但它对我不起作用。当我单击按钮时,它应该被禁用。此外,当我单击按钮并热重新加载应用程序时,它会禁用按钮。

我也尝试使用函数,但在访问小部件变量时遇到了困难。

bool _isButtonDisabled = false;
MaterialButton(
  color: Colors.blue,
  textColor: Colors.white,
  disabledColor: Colors.grey,
  disabledTextColor: Colors.black,
  child: Text('Login'),
  shape: RoundedRectangleBorder(
      borderRadius: new BorderRadius.circular(4)),
  onPressed: _isButtonDisabled ? null : () async {
    _isButtonDisabled = true;

    // Login Stuff
  }
)

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    要将更改应用到 UI,您需要在 setState() 方法中进行

        setState(() {
          _isButtonDisabled = true;
        });
    

    完整示例:

    
    class MyWidget extends StatefulWidget {
      @override
      _MyWidgetState createState() => _MyWidgetState();
    }
    
    class _MyWidgetState extends State<MyWidget> {
      bool _isButtonDisabled = false;
    
      @override
      Widget build(BuildContext context) {
        return MaterialButton(
          color: Colors.blue,
          textColor: Colors.white,
          disabledColor: Colors.grey,
          disabledTextColor: Colors.black,
          child: Text('Login'),
          shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(4)),
          onPressed: _isButtonDisabled
              ? null
              : () async {
                  setState(() {
                    _isButtonDisabled = true;
                  });
    
                  // Login Stuff
                },
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2020-11-06
      • 2021-11-27
      • 2019-04-20
      • 1970-01-01
      • 2015-12-01
      相关资源
      最近更新 更多