Can't get button press to work in flutter

后端 未结 4 1013
再見小時候
再見小時候 2021-01-18 06:54

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

相关标签:
4条回答
  • 2021-01-18 07:05

    in some cases, this can occur with a widget in the stack.

    It is possible that the Widget is overwritten with another Widget, so it cannot be clicked.

    0 讨论(0)
  • 2021-01-18 07:07

    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();
              },
            ),
          ],
        );
      }
    }
    
    0 讨论(0)
  • 2021-01-18 07:15

    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.
       },
       ..
     )
    
    0 讨论(0)
  • 2021-01-18 07:22

    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.

    0 讨论(0)
提交回复
热议问题