Last active
May 18, 2023 05:33
-
-
Save mono0926/77bdfa6bae817467874e04ead942f5da to your computer and use it in GitHub Desktop.
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/material.dart'; | |
import 'package:google_fonts/google_fonts.dart'; | |
void main() { | |
runApp( | |
const App(), | |
); | |
} | |
class App extends StatelessWidget { | |
const App({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark(), | |
home: const HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({super.key}); | |
@override | |
State<HomePage> createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
final _fizzBuzzList = <(int, String)>[]; | |
late final Timer _timer; | |
@override | |
void initState() { | |
super.initState(); | |
_timer = Timer.periodic(const Duration(seconds: 1), (_) { | |
final fizzBuzz = _fizzBuzz(_fizzBuzzList.length + 1); | |
setState(() { | |
_fizzBuzzList.insert(0, fizzBuzz); | |
}); | |
}); | |
} | |
@override | |
void dispose() { | |
_timer.cancel(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: ListView( | |
children: [ | |
for (final (number, message) in _fizzBuzzList) | |
ListTile( | |
title: Text( | |
'${number.toString().padLeft(6)}: $message', | |
style: GoogleFonts.robotoMono(), | |
), | |
dense: true, | |
), | |
], | |
), | |
); | |
} | |
} | |
(int, String) _fizzBuzz(int number) => switch (number) { | |
_ when number % 15 == 0 => (number, 'FizzBuzz'), | |
_ when number % 3 == 0 => (number, 'Fizz'), | |
_ when number % 5 == 0 => (number, 'Buzz'), | |
_ => (number, '$number'), | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment