Created
June 26, 2020 17:25
-
-
Save RedBrogdon/ad7a1d22a53e7c39483b9afa2619d176 to your computer and use it in GitHub Desktop.
Timekeeper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/gestures.dart'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp( | |
MaterialApp( | |
home: MyApp(), | |
), | |
); | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
Timer timer; | |
int secondsLeft = 0; | |
int teamOneScore = 0; | |
int teamTwoScore = 0; | |
void _restartGame() { | |
setState(() { | |
secondsLeft = 60; | |
timer = Timer.periodic(Duration(seconds: 1), (t) { | |
setState(() { | |
secondsLeft--; | |
if (secondsLeft == 0) { | |
timer.cancel(); | |
} | |
}); | |
}); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: (secondsLeft != 0) | |
? Theme.of(context).scaffoldBackgroundColor | |
: Color(0xffff8080), | |
body: Row( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
GestureDetector( | |
onTap: () => _restartGame(), | |
child: SizedBox( | |
height: 150, | |
width: 175, | |
child: Center( | |
child: Text( | |
'$secondsLeft', | |
style: Theme.of(context) | |
.textTheme | |
.headline1 | |
.copyWith(fontWeight: FontWeight.bold), | |
), | |
), | |
), | |
), | |
Column(children: [ | |
SizedBox( | |
height: 100, | |
width: 50, | |
child: GestureDetector( | |
onTap: () { | |
setState(() => teamOneScore++); | |
}, | |
onLongPress: () { | |
setState(() => teamOneScore--); | |
}, | |
child: Center( | |
child: Text( | |
'$teamOneScore', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
), | |
), | |
), | |
SizedBox( | |
height: 50, | |
width: 50, | |
child: GestureDetector( | |
onTap: () { | |
setState(() => teamTwoScore++); | |
}, | |
onLongPress: () { | |
setState(() => teamTwoScore--); | |
}, | |
child: Center( | |
child: Text( | |
'$teamTwoScore', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
), | |
), | |
), | |
]) | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment