【问题标题】:Can't get button press to work in flutter无法按下按钮以颤动
【发布时间】:2018-05-16 00:30:07
【问题描述】:

好吧,我对扑/飞镖还是很陌生,所以放轻松。我只是想制作一个非常简单的应用程序,当您按下按钮时,一些文本会更新告诉您按下按钮的次数。我不知道为什么这段代码不起作用。该按钮出现,但当您按下它时没有任何反应。

import 'package:flutter/material.dart';

class Homepage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Column(
      children: <Widget>[],
    );
  }
}

class Buttonz extends StatefulWidget {
  @override
  _ButtonBeingPressed createState() => new _ButtonBeingPressed();
}

class _ButtonBeingPressed extends State<Buttonz> {
  int _timesPressed = 0;
  _buttonWasPressed() {
    setState(() {
      _timesPressed++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Column(children: <Widget>[
     new Center(
          child: new Row(
        children: <Widget>[
          new Text(
              'The button was pressed ' + _timesPressed.toString() + "         
 times"),
          new RaisedButton(
            onPressed: _buttonWasPressed(),
           child: new Row(
              children: <Widget>[new Text("Press meh")],
            ),
          ),
        ],
      ))
    ]);
  }
}

【问题讨论】:

  • 在列中设置中心是没有用的。而是使用居中的列。

标签: dart flutter


【解决方案1】:

您的问题是您没有将回调传递给RaisedButton,而是调用了您的回调。

new RaisedButton(
  onPressed: _buttonWasPressed(), // invokes function
  child: new Row(children: <Widget>[new Text("Press meh")]),
);

要将回调传递给另一个小部件,您有两种选择:

通过撕纸

 new RaisedButton(
   onPressed: _buttonWasPressed, // no `()`,
   child: ...
 )

传递一个闭包

 new RaisedButton(
   onPressed: () {
     // do something.
   },
   ..
 )

【讨论】:

  • 成功了。我现在明白了。非常感谢您的帮助!
【解决方案2】:

在某些情况下,堆栈中的小部件可能会发生这种情况。

这个Widget有可能被另一个Widget覆盖,所以无法点击。

【讨论】:

  • 如果发生了,你将如何解决?因为它现在发生在我身上,tq
  • @MNFS 在我的情况下也会发生。我只是确保外部小部件没有填充内部小部件。
【解决方案3】:

添加了一个材质应用程序并稍微重新连接了 RaisedButton。我想这就是你连接onPressed 的方式。

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(home: new Buttonz());
  }
}

class Buttonz extends StatefulWidget {
  @override
  _ButtonBeingPressed createState() => new _ButtonBeingPressed();
}

class _ButtonBeingPressed extends State<Buttonz> {
  int _timesPressed = 0;
  _buttonWasPressed() {
    setState(() {
      _timesPressed++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return  new Column(
      children: <Widget>[
        new Text(
            'The button was pressed $_timesPressed times'),
        new RaisedButton(
          child: const Text('Press meh'),
          onPressed: () {
            _buttonWasPressed();
          },
        ),
      ],
    );
  }
}

【讨论】:

    【解决方案4】:

    你的按钮应该是这样的:

    new RaisedButton( 
        child: const Text('Press meh'), 
        onPressed: _buttonWasPressed, 
    ),
    

    如果这不起作用,请尝试使用flutter clean 清理您的flutter 项目,然后在调试设备上重新安装该应用程序。

    【讨论】:

      猜你喜欢
      • 2022-01-13
      • 2021-10-24
      • 2020-12-16
      • 2020-04-07
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多