jnjosjk0965 / LearnFlutter

Flutter를 학습해요
0 stars 0 forks source link

Stateful Widget, Timer, Duration #2

Open jnjosjk0965 opened 7 months ago

jnjosjk0965 commented 7 months ago

main.dart

import 'package:flutter/material.dart'; import 'package:toonflix/screens/home_screen.dart';

void main() { runApp(const App()); }

class App extends StatelessWidget { const App({super.key});

@override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( colorScheme: ColorScheme.fromSwatch( backgroundColor: const Color(0xFFE7626C), ), textTheme: const TextTheme( displayLarge: TextStyle( color: Color(0xff232b55), ), ), cardColor: const Color(0xFFF4EDDB), ), home: const HomeScreen(), ); } }

jnjosjk0965 commented 7 months ago

home_screen.dart

import 'dart:async';

import 'package:flutter/material.dart';

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

@override State createState() => _HomeScreenState(); }

class _HomeScreenState extends State { static const int pomodorosCycle = 1500; int totalSeconds = pomodorosCycle; bool isRunning = false; late Timer timer; int totalPomodoros = 0;

void onTick(Timer timer) { if (totalSeconds == 1) { setState(() { totalPomodoros = totalPomodoros + 1; isRunning = false; totalSeconds = pomodorosCycle; }); timer.cancel(); } else { setState(() { totalSeconds = totalSeconds - 1; }); } }

void onStartPressed() { timer = Timer.periodic(const Duration(seconds: 1), onTick); setState(() { isRunning = true; }); }

void onPausePressed() { timer.cancel(); setState(() { isRunning = false; }); }

void onResetPressed() { if (totalSeconds < pomodorosCycle) { timer.cancel(); setState(() { isRunning = false; totalSeconds = pomodorosCycle; }); } }

String format(int seconds) { var duratoin = Duration(seconds: seconds); return duratoin.toString().substring(2, 7); }

@override Widget build(BuildContext context) { return Scaffold( backgroundColor: Theme.of(context).colorScheme.background, body: Column( children: [ Flexible( flex: 1, child: Container( alignment: Alignment.bottomCenter, child: Text( format(totalSeconds), style: TextStyle( color: Theme.of(context).cardColor, fontSize: 89, fontWeight: FontWeight.w600, ), ), ), ), Flexible( flex: 3, child: Center( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ IconButton( iconSize: 120, color: Theme.of(context).cardColor, onPressed: onResetPressed, icon: const Icon(Icons.restart_alt_outlined)), IconButton( iconSize: 120, color: Theme.of(context).cardColor, onPressed: isRunning ? onPausePressed : onStartPressed, icon: Icon(isRunning ? Icons.pause_circle_outline : Icons.play_circle_outline), ), ], ), ), ), Flexible( flex: 1, child: Row( children: [ Expanded( child: Container( decoration: BoxDecoration( color: Theme.of(context).cardColor, borderRadius: const BorderRadius.vertical(top: Radius.circular(40)), ), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( "Pomodoros", style: TextStyle( fontSize: 20, color: Theme.of(context).textTheme.displayLarge!.color, fontWeight: FontWeight.w600, ), ), Text( "$totalPomodoros", style: TextStyle( fontSize: 40, color: Theme.of(context).textTheme.displayLarge!.color, fontWeight: FontWeight.w600, ), ), ], ), ), ), ], ), ), ], ), ); } }