Skip to content

Instantly share code, notes, and snippets.

@rayliverified
Created April 20, 2020 22:48
Show Gist options
  • Save rayliverified/38311b87e4b3c76329812077c82323b4 to your computer and use it in GitHub Desktop.
Save rayliverified/38311b87e4b3c76329812077c82323b4 to your computer and use it in GitHub Desktop.
Flutter.dev Example 2 - Fibonacci
import 'package:flutter/material.dart';
void main() async {
final numbers = FibonacciNumbers();
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text('Fibonacci List'),
),
body: FibonacciListView(numbers),
),
),
);
}
class FibonacciNumbers {
final cache = {0: BigInt.from(1), 1: BigInt.from(1)};
BigInt get(int i) {
if (!cache.containsKey(i)) {
cache[i] = get(i - 1) + get(i - 2);
}
return cache[i];
}
}
class FibonacciListView extends StatelessWidget {
final FibonacciNumbers numbers;
FibonacciListView(this.numbers);
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (context, i) {
return ListTile(
title: Text('${numbers.get(i)}'),
onTap: () {
final snack = SnackBar(
content: Text('${numbers.get(i)} is '
'#$i in the Fibonacci sequence!'),
);
Scaffold.of(context).showSnackBar(snack);
},
);
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment