MakerHyeon

[Flutter] 뽀모도로앱 본문

Flutter

[Flutter] 뽀모도로앱

유쾌한고등어 2023. 3. 24. 21:10

● 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(
        // backgroundColor
        colorScheme: ColorScheme.fromSwatch(
          backgroundColor: const Color(0xFFE7626c),
        ),
        textTheme: const TextTheme(
          displayLarge: TextStyle(
            color: Color(0xFF232B55),
          ),
        ),
        cardColor: const Color(0xFFF4EDDB),
      ),
      home: const HomeScreen(),
    );
  }
}

● home_screen.dart

import 'package:flutter/material.dart';
import 'dart:async';

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

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

class _HomeScreenState extends State<HomeScreen> {
  static const twentyFiveMinutes = 1500;

  int totalSeconds = twentyFiveMinutes;
  bool isRunning = false;
  int totalPomodoros = 0;
  // late ; property를 당장 초기화 하지 않아도 됨을 뜻함
  late Timer timer;

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

    setState(() {
      totalSeconds -= 1;
    });
    timer.cancel();
  }

  void onStartPressed() {
    // Timer ; 정해진 간격에 한번씩 함수를 실행
    // 매초마다 onTick 실행
    timer = Timer.periodic(
      const Duration(seconds: 1),
      onTick,
    );
    setState(() {
      isRunning = true;
    });
  }

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

  void onStopPressed() {
    timer.cancel();
    setState(() {
      totalSeconds = twentyFiveMinutes;
      totalPomodoros = 0;
      isRunning = false;
    });
  }

  String format(int seconds) {
    var duration = Duration(seconds: seconds);
    //print(duration.toString().split(".").first.substring(2, 7));
    return duration.toString().split(".").first.substring(2, 7);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).colorScheme.background,
      body: Column(
        children: [
          // * Flexible은 하드코딩 되는 값을 만들게 해준다.
          // 하나의 박스가 얼마나 공간을 차지할 지 비율을 정할 수 있다.
          // UI를 비율에 기반해서 더 유연하게 만들 수 있게 해준다.
          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: Column(
              children: [
                IconButton(
                  iconSize: 120,
                  color: Theme.of(context).cardColor,
                  onPressed: isRunning ? onPausePressed : onStartPressed,
                  icon: Icon(isRunning
                      ? Icons.pause_circle_outline
                      : Icons.play_circle_outline),
                ),
                IconButton(
                  onPressed: onStopPressed,
                  icon: const Icon(Icons.restart_alt),
                  iconSize: 90,
                  color: Theme.of(context).cardColor,
                ),
              ],
            ),
          ),
          Flexible(
            flex: 1,
            child: Row(
              children: [
                // 끝까지 확장
                Expanded(
                  child: Container(
                    decoration: BoxDecoration(
                      color: Theme.of(context).cardColor,
                      borderRadius: const BorderRadius.only(
                        topLeft: Radius.circular(50),
                        topRight: Radius.circular(50),
                      ),
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          'Pomodoros',
                          style: TextStyle(
                            fontSize: 20,
                            fontWeight: FontWeight.w600,
                            color:
                                Theme.of(context).textTheme.displayLarge!.color,
                          ),
                        ),
                        Text(
                          '$totalPomodoros',
                          style: TextStyle(
                            fontSize: 58,
                            fontWeight: FontWeight.w600,
                            color:
                                Theme.of(context).textTheme.displayLarge!.color,
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

 

'Flutter' 카테고리의 다른 글

[Flutter] App bar icon button  (0) 2023.04.03
[Flutter] 간단한 화면 구성 연습 # 1  (0) 2023.04.03
[Flutter] 기본 구조 template  (0) 2023.03.28
[Flutter] WebtoonCards  (0) 2023.03.25
[Flutter] Scaffold,생성자  (0) 2023.02.27
Comments