如何获取按钮的对象[颤动]

时间:2018-04-25 19:07:42

标签: dart key flutter

我创建了一个RaisedButton.Now我想知道我可以将密钥传递给它并在函数中访问它的对象,还是有任何其他方法来访问Raisebutton,然后将其存储在变量中。

2 个答案:

答案 0 :(得分:0)

澄清实际想要执行的内容将非常有用。

但是,我想我可以猜到问题是什么 - 混淆了有关如何有条不紊的状态有效。

像RaisedButton这样的东西在Flutter中工作的一般方式是你将state放在StatefulWidget上面的某个地方,这就构建了RaisedButton。如果你想改变raiseButton,你可以在setState(() => ?? = ??);内的StatefulWidget上设置一个变量,并在StatefulWidget的构建函数中使用它来决定用RaisedButton构建哪些参数。

这是一个简短的例子,每次按下它时都会按下按钮。

class Raising extends StatefulWidget {
  final String text;

  Raising({@required this.text});

  @override
  State<StatefulWidget> createState() => RaisingState();
}

class RaisingState extends State<Raising> {
  double elevation = 0.0;

  @override
  Widget build(BuildContext context) => new RaisedButton(
        child: new Text(widget.text),
        elevation: elevation,
        onPressed: () => setState(() => elevation += 1),
      );
}

答案 1 :(得分:0)

Flutter与传统Android开发不同。这里的一切都是小工具,小工具有自己的状态。

根据Flutter documentation

  

如果要禁用按钮,只需将null传递给   onPressed of RaisedButton。如果你想启用它通过一个   函数,你可以传递一个像(){}这样的空函数。

检查以下示例以了解它。

void main() {
  runApp(new ButtonDemoNew());
}

class ButtonDemoNew extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _ButtonStateDemo();
  }
}

class _ButtonStateDemo extends State<ButtonDemoNew> {
  bool enabled = false;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
      title: "Button Demo",
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text("Button Demo"),
        ),
        body: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new RaisedButton(
              onPressed: enabled ? () {} : null,
              child: new Text("Demo Button"),
            ),
            new RaisedButton(
              onPressed: () {
                setState(() {
                  enabled = enabled ? false : true;
                });
              },
              child: new Text("Enable / Disable"),
            )
          ],
        ),
      ),
    );
  }
}