Last active
May 2, 2020 15:38
-
-
Save stegrams/e7df8ed8a42ef99e54f69f1ad422103d to your computer and use it in GitHub Desktop.
Random ball numbers that start and stop interactively.
This file contains hidden or 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 'package:flutter/material.dart'; | |
import 'dart:async'; | |
import 'dart:math'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('Random Roulette'), | |
), | |
body: MyWidget(), | |
), | |
); | |
} | |
} | |
class MyWidget extends StatefulWidget { | |
@override | |
_MyWidgetState createState() => _MyWidgetState(); | |
} | |
class _MyWidgetState extends State<MyWidget> { | |
int number; | |
Timer timer; | |
Timer createTimer() { | |
Random random = Random(); | |
return Timer.periodic( | |
Duration(milliseconds: 20), | |
(t) => setState( | |
() => number = random.nextInt(32), | |
), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
Wrap( | |
direction: Axis.horizontal, | |
runAlignment: WrapAlignment.center, | |
spacing: 10, | |
children: <Widget>[ | |
BallNumber(number: number == null ? 0 : Random(number + 12).nextInt(10)), | |
BallNumber(number: number == null ? 0 : Random(number + 78).nextInt(10)), | |
BallNumber(number: number == null ? 0 : Random(number + 25).nextInt(10)), | |
BallNumber(number: number == null ? 0 : Random(number + 43).nextInt(10)), | |
], | |
), | |
RaisedButton( | |
child: Text(timer?.isActive ?? false ? 'Stop' : 'Start'), | |
onPressed: () => setState( | |
() => timer?.isActive ?? false | |
? timer.cancel() | |
: timer = createTimer(), | |
), | |
), | |
], | |
), | |
); | |
} | |
} | |
class BallNumber extends StatelessWidget { | |
final int number; | |
const BallNumber({Key key, this.number}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
padding: EdgeInsets.all(20), | |
decoration: BoxDecoration( | |
shape: BoxShape.circle, | |
color: Colors.green, | |
), | |
child: Text( | |
'$number', | |
style: TextStyle( | |
fontSize: 40, | |
fontWeight: FontWeight.bold, | |
color: Colors.white, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment