Can't get button press to work in flutter

后端 未结 4 1017
再見小時候
再見小時候 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: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 {
      int _timesPressed = 0;
      _buttonWasPressed() {
        setState(() {
          _timesPressed++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return  new Column(
          children: [
            new Text(
                'The button was pressed $_timesPressed times'),
            new RaisedButton(
              child: const Text('Press meh'),
              onPressed: () {
                _buttonWasPressed();
              },
            ),
          ],
        );
      }
    }
    

提交回复
热议问题