Skip to content

Instantly share code, notes, and snippets.

@mono0926
Last active May 18, 2023 05:33
Show Gist options
  • Save mono0926/77bdfa6bae817467874e04ead942f5da to your computer and use it in GitHub Desktop.
Save mono0926/77bdfa6bae817467874e04ead942f5da to your computer and use it in GitHub Desktop.
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