我刚刚开始掌握Flutter 的诀窍,但我想不出如何设置按钮的启用状态.

在文档中,它说将onPressed设置为NULL来禁用按钮,并给它一个值来启用它.如果按钮在整个生命周期中继续处于相同状态,则可以这样做.

我得到的印象是,我需要创建一个自定义的Stateful小部件,它将允许我以某种方式更新按钮的启用状态(或onPressed回调).

所以我的问题是我该怎么做?这似乎是一个非常简单的要求,但我在文档中找不到任何关于如何做到这一点的信息.

谢谢你.

推荐答案

我想你可能想在你的按钮上引入一些帮助函数,以及一个有状态的小部件,以及一些要关闭的属性.

  • 使用StatefulWidget/State并创建一个变量来保存您的条件(例如isButtonDisabled)
  • 最初将其设置为true(如果这是您想要的)
  • 当呈现按钮时,don't directly set the 100将值转换为null或某些函数onPressed: () {}
  • Instead,则使用三元或辅助函数(example below)有条件地设置它
  • isButtonDisabled作为该条件的一部分进行判断,并返回null或某个函数.
  • 当按下按钮时(或当您想禁用按钮时),使用setState(() => isButtonDisabled = true)翻转条件变量.
  • Ffltter将使用新状态再次调用build()方法,并且按钮将使用null按下处理程序呈现并被禁用.

下面是使用Flutter 计数器项目的更多上下文.

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,
    );
  }
}

在本例中,我使用内联三元组有条件地设置TextonPressed,但将其提取到函数中可能更合适(您也可以使用同样的方法更改按钮的文本):

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();
      };
    }
  }

Flutter相关问答推荐

使用Bg晃动背景图像边界

创建多个具有适当填充和对齐的按钮的最佳方法

获取屏幕大小的滚动视图与动态大小?

无效字符(位于字符68处)flutter—update—... europe-west1.firebasedatabase.app/

如何在WidgetRef引用外部的函数中使用Riverpod刷新数据

如何在Flutter 中不从getX库中初始化GetxController中的变量

Flutter -如何将导航轨道标签靠左对齐?

如何将Will POP示波器转换为POP数据抖动的POP示波器

带有图标抖动的自定义下拉按钮

Riverpod 2.0-如何使用FutureOr和AutoDisposeSyncNotificationer处理api状态代码?

从future 列表到流列表Flutter Firebase

如何从flutter中不同类别的列表中获取所有产品

如何在flutter中加载并显示adx锚定的自适应广告?

Dart:未捕获错误:RangeError(索引):索引超出范围:索引应小于 7:7

'type' Icon '不是类型' IconData '的子类型

Flutter屏幕适配 - 在模拟器和真实手机上文本尺寸不同

无法使用 bloc 更新 ListView.builder 的特定时间

底部工作表顶部的一块 Flutter

Flutter 如何使用 ClipPath 编辑容器的顶部?

如何在 Flutter 中将列小部件添加到行小部件?