summaryrefslogtreecommitdiff
path: root/lib/playground_page.dart
blob: 3c090ac064dfcef05afe4b0be49d86b699a616c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';
import 'app_bar.dart';
import 'button.dart';
import 'value_display.dart';

class PlaygroundPage extends StatefulWidget {
  const PlaygroundPage({super.key});

  @override
  State<PlaygroundPage> createState() => _PlaygroundPageState();
}

class _PlaygroundPageState extends State<PlaygroundPage> {
  int _counter = 0;
  int _elapsedMilliseconds = 0;
  final stopwatch = Stopwatch();

  void increment() {
    setState(() {
      ++_counter;
      if (stopwatch.isRunning) {
        stopwatch.stop();
        _elapsedMilliseconds = stopwatch.elapsedMilliseconds;
        stopwatch.reset();
      }
      else {
        stopwatch.start();
        _elapsedMilliseconds = 0;
      }
    });
  }

  void clear() {
    setState(() {
      if (stopwatch.isRunning) {
        return;
      }
      _counter = 0;
      _elapsedMilliseconds = 0;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        const AppBar(),
        Expanded(
          child: Center(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget> [ 
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Button(onPressed: increment, buttonText: 'Increment'),
                    const SizedBox(height: 8),
                    Button(onPressed: clear, buttonText: 'Clear'),
                  ],
                ),
                const SizedBox(width: 16), 
                ValueDisplay(label: 'Count', value: _counter),
                const SizedBox(width: 16),
                ValueDisplay(label: 'Elapsed (ms)', value: _elapsedMilliseconds),
              ],
            ),
          ),
        ),
      ],
    );
  }
}