Skip to content

Test timer #1

@mohammadlatifalikhah

Description

@mohammadlatifalikhah

import 'dart:async';

import 'package:flutter/material.dart';

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

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

// This widget is the root of your application.
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

final String title;

@OverRide
State createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
bool isActive = false;
int second = 0;
Timer? timer;

void secondconter() {
if (isActive) {
setState(() {
second += 1;
});
}
}

@OverRide
Widget build(BuildContext context) {
timer = Timer.periodic(
Duration(seconds: 1),
(timer) {
secondconter();
});

int sec = second % 60;
int min = ((second % 3600) / 60).toInt();
int hour = ( second / 3600).toInt();

return Scaffold(
  appBar: AppBar(
      backgroundColor: Colors.blue,
      title: Text(
        "Timer",
        style: TextStyle(color: Colors.black, fontSize: 40),
      )),
  body: Container(
      color: Colors.black,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                "$hour",
                style: TextStyle(color: Colors.white, fontSize: 50),
              ),
              Text(
                ":",
                style: TextStyle(color: Colors.white, fontSize: 50),
              ),
              Text(
                "$min",
                style: TextStyle(color: Colors.white, fontSize: 50),
              ),
              Text(
                ":",
                style: TextStyle(color: Colors.white, fontSize: 50),
              ),
              Text(
                "$sec",
                style: TextStyle(color: Colors.white, fontSize: 50),
              )
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    isActive=true;
                  });
                },
                child: Text('Start'),
              ),
              SizedBox(
                width: 30,
              ),
              ElevatedButton(
                onPressed: () {
                 setState(() {
                   isActive=false;
                   second=0;
                 });
                },
                child: Text('Stop'),
              ),
            ],
          )
        ],
      )),
);

}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions