无法按动按钮才能正常工作

编程入门 行业动态 更新时间:2024-10-25 22:31:19
本文介绍了无法按动按钮才能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好吧,我是扑扑/飞镖的新手,所以对我轻松一点。我只是在尝试制作一个非常简单的应用,在该应用中,当您按下按钮时,会有一些文本更新告诉您按下按钮的次数。我不知道为什么这段代码不起作用。出现该按钮,但按此按钮无任何反应。

Ok I'm pretty new to flutter/ dart so go easy on me. I'm just trying to make a very simple app where when you press a button some text updates telling you how many times you have pressed the button. I have no idea why this code doesn't work. The button appears but nothing happens when you press it.

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

推荐答案

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

Your problem is that you didn't pass a callback to RaisedButton, you invoked your callback.

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

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

To pass a callback to another widget you have two choices:

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

通过闭包

Pass a closure

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

更多推荐

无法按动按钮才能正常工作

本文发布于:2023-11-27 05:33:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636805.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:才能正常   按钮   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!