■ 스톱워치(Stopwatch)를 만드는 방법을 보여준다.
▶ main.dart
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
import 'dart:async'; import 'package:flutter/material.dart'; void main() => runApp(TestApplication()); class TestApplication extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Test Application', theme: ThemeData( primarySwatch: Colors.blue, ), home: MainPage(), ); } } class MainPage extends StatefulWidget { MainPage({Key key}) : super(key: key); @override _MainPageState createState() => _MainPageState(); } class _MainPageState extends State<MainPage> { Timer _timer; int _timeCount = 0; bool _isRunning = false; List<String> _lapTimeList = []; @override void dispose() { _timer?.cancel(); super.dispose(); } Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('스톱워치'), ), body: _buildBody(), bottomNavigationBar: BottomAppBar( child: Container( height: 50.0, ), ), floatingActionButton: FloatingActionButton( child: _isRunning ? Icon(Icons.pause) : Icon(Icons.play_arrow), onPressed: () => setState(() { _clickPlayButton(); }), ), floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, ); } Widget _buildBody() { int secondCount = _timeCount ~/ 100; String hundredthCount = '${_timeCount % 100}'.padLeft(2, '0'); return Center( child: Padding( padding: const EdgeInsets.only(top: 30), child: Stack( children: <Widget>[ Column( children: <Widget>[ Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.end, children: <Widget>[ Text( '$secondCount', style: TextStyle(fontSize: 50.0), ), Text('$hundredthCount'), ], ), Container( width: 100, height: 200, child: ListView( children: _lapTimeList.map((time) => Text(time)).toList(), ), ) ], ), Positioned( left: 10, bottom: 10, child: FloatingActionButton( backgroundColor: Colors.deepOrange, onPressed: _clickResetButton, child: Icon(Icons.rotate_left), ), ), Positioned( right: 10, bottom: 10, child: RaisedButton( onPressed: () { setState(() { _recordLapTime('$secondCount.$hundredthCount'); }); }, child: Text('랩타임'), ), ) ], ), ), ); } void _start() { _timer = Timer.periodic(Duration(milliseconds: 10), (timer) { setState(() { _timeCount++; }); }); } void _pause() { _timer?.cancel(); } void _clickPlayButton() { _isRunning = !_isRunning; if (_isRunning) { _start(); } else { _pause(); } } void _clickResetButton() { setState(() { _isRunning = false; _timer?.cancel(); _lapTimeList.clear(); _timeCount = 0; }); } void _recordLapTime(String time) { _lapTimeList.insert(0, '${_lapTimeList.length + 1}등 $time'); } } |