728x90
반응형
728x170
▶ main.dart
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');
}
}
728x90
반응형
그리드형(광고전용)
'Flutter' 카테고리의 다른 글
[FLUTTER] 할 일 관리 앱 만들기 (파이어베이스 연동) (0) | 2021.05.10 |
---|---|
[FLUTTER] 할 일 관리 앱 만들기 (0) | 2021.05.10 |
[FLUTTER] Timer 클래스 : periodic 팩토리 생성자 사용하기 (0) | 2021.05.02 |
[FLUTTER] BottomAppBar 클래스 사용하기 (0) | 2021.05.02 |
[FLUTTER] Positioned 클래스 사용하기 (0) | 2021.05.02 |
[FLUTTER] 비만도 계산기 만들기 (0) | 2021.05.01 |
[FLUTTER] Opacity 클래스 : opacity 속성을 사용해 불투명도 표시하기 (0) | 2021.05.01 |
[FLUTTER] ScaffoldState 클래스 : showSnackBar 메소드를 사용해 스낵바 표시하기 (0) | 2021.05.01 |
[FLUTTER] TextFormField 클래스 : validator 속성을 사용해 입력 값 검증하기 (0) | 2021.05.01 |
[FLUTTER] Form 클래스 : 입력 값 검증하기 (0) | 2021.05.01 |
댓글을 달아 주세요