Can't get button press to work in flutter

2019-02-26 00:26发布

问题:

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

回答1:

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:

Pass a tear-off

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

Pass a closure

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


回答2:

Added a Material App and rewired the RaisedButton a little. I think it was how you had onPressed wired up.

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


回答3:

Your button should be like this.:

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

If this doesn't work, then try to clean your flutter project with flutter clean and then reinstalling the app on debug device.



回答4:

Event listeners like onPressed: take only callbacks(including anonymous functions). So, you need to replace _buttonWasPressed() with _buttonWasPressed.

Note: I am observing in flutter beta-3, _buttonWasPressed() gives a compile error.



标签: dart flutter