반응형
Flutter 플러터 타이머 간단 구현
코드 작성
import 'dart:async';
import 'package:flutter/material.dart';
class TimerPage extends StatefulWidget {
const TimerPage({Key? key}) : super(key: key);
@override
State<TimerPage> createState() => _TimerPageState();
}
class _TimerPageState extends State<TimerPage> {
int _seconds = 0;
bool _isRunning = false;
late Timer _timer;
void _startTimer() {
_isRunning = true;
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
setState(() {
_seconds++;
});
});
}
void _stopTimer() {
_isRunning = false;
_timer.cancel();
}
void _resetTimer() {
setState(() {
_seconds = 0;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('$_seconds', style: TextStyle(fontSize: 72)),
SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _isRunning ? null : _startTimer,
child: Text('Start'),
),
SizedBox(width: 16),
ElevatedButton(
onPressed: _isRunning ? _stopTimer : null,
child: Text('Stop'),
),
SizedBox(width: 16),
ElevatedButton(
onPressed: _resetTimer,
child: Text('Reset'),
),
],
),
],
),
),
);
}
}
참고할만한 글
반응형
'개발 > Flutter' 카테고리의 다른 글
플러터 Flutter For문 Switch문 간단 구현 (0) | 2023.02.18 |
---|---|
플러터 Flutter if else문 간단 구현법 (0) | 2023.02.18 |
플러터 Flutter 메모장 구현하기 Notepad (0) | 2023.01.29 |
플러터 Flutter ToDo List 구현하기 (0) | 2023.01.29 |
Flutter 플러터 URL 주소 열기 간단 구현 url launcher (0) | 2022.12.25 |
Flutter 플러터 화면 전환 애니메이션 삭제 간단 사용법 Navigator Animation (0) | 2022.12.25 |
Flutter 플러터 백 버튼 무시하기 WillPopScope (0) | 2022.12.25 |
Flutter 플러터 랜덤 숫자 간단 사용법 Random (0) | 2022.12.24 |
댓글