diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index 775f97f..ce0508c 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'package:flutter/material.dart'; class HomeScreen extends StatefulWidget { @@ -9,10 +10,38 @@ class HomeScreen extends StatefulWidget { class _HomeScreenState extends State { String originalText = "이것과 동일하게 입력하시오"; - bool isAnswer = false; + + int totalSeconds = 0; + bool isTimerRunning = false; + late Timer timer; + + bool isSubmitted = false; + String answerResult = ""; final textController = TextEditingController(); + void onTick(Timer timer) { + setState(() { + totalSeconds = totalSeconds + 1; + }); + } + + void onStartTyping() { + if (isSubmitted == true) { + isSubmitted = false; + } + if (isTimerRunning == false) { + isTimerRunning = true; + totalSeconds = 0; + } else { + return; + } + timer = Timer.periodic( + const Duration(seconds: 1), + onTick, + ); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -56,6 +85,7 @@ class _HomeScreenState extends State { ), TextFormField( controller: textController, + onTap: onStartTyping, cursorColor: Colors.white, style: const TextStyle( color: Colors.white, @@ -69,27 +99,13 @@ class _HomeScreenState extends State { border: OutlineInputBorder(), ), onFieldSubmitted: (String text) { - setState(() { - if (originalText == textController.text) { - isAnswer = true; - } else { - isAnswer = false; - } - }); + submitAnswer(); }, ), Center( child: IconButton( iconSize: 50, - onPressed: () { - setState(() { - if (originalText == textController.text) { - isAnswer = true; - } else { - isAnswer = false; - } - }); - }, + onPressed: submitAnswer, icon: const Icon( Icons.keyboard, color: Colors.white, @@ -97,13 +113,18 @@ class _HomeScreenState extends State { ), ), Visibility( - visible: isAnswer, - child: const Text( - '정답입니다!', - style: TextStyle( - color: Colors.white, - ), - )) + visible: isSubmitted, + child: Text( + answerResult, + style: const TextStyle( + color: Colors.white, + ), + ), + ), + Text( + '$totalSeconds', + style: const TextStyle(color: Colors.white, fontSize: 50), + ) ], ), ), @@ -118,4 +139,18 @@ class _HomeScreenState extends State { ), ); } + + void submitAnswer() { + setState(() { + timer.cancel(); + isSubmitted = true; + isTimerRunning = false; + if (originalText == textController.text) { + answerResult = "정답입니다!"; + } else { + answerResult = "틀렸습니다."; + } + textController.text = ""; + }); + } }